Architecture Overview
System Components
Claude-Mem operates as a Claude Code plugin with the following core components:- Plugin Hooks - Lifecycle events (lifecycle hooks for SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, PostToolUseFailure, Stop and SessionEnd, plus a Setup version-check), each run as a shell-free
claude-mem hook claude-code <event>command - Worker Service - Express HTTP API on a per-user port; processes observations via the Claude Agent SDK (or Gemini / OpenRouter)
- Database Layer - SQLite + FTS5 (and optional Chroma for semantic search)
- Search Tools - HTTP API + the
mem-searchskill / MCP server for progressive disclosure search - Viewer UI - React-based real-time memory stream served by the worker
Technology Stack
Data Flow
Memory Pipeline
- Input: Claude Code sends tool execution data via stdin to hooks
- Storage: Hooks write observations to SQLite database
- Processing: Worker service reads observations, processes via SDK
- Output: Processed summaries written back to database
- Retrieval: Next session’s context hook reads summaries from database
Search Pipeline
- User Query: User asks naturally: “What bugs did we fix?”
- MCP Tools Invoked: Claude recognizes intent and invokes MCP search tools
- HTTP API: MCP tools call HTTP endpoint (e.g.,
/api/search/observations) - SessionSearch: Worker service queries FTS5 virtual tables
- Format: Results formatted and returned via MCP
- Return: Claude presents formatted results to user
Session Lifecycle
Directory Structure
Component Details
1. Plugin Hooks
The plugin registers lifecycle hooks plus a Setup-phaseversion-check.js. Each lifecycle hook is one exec-form command, claude-mem hook claude-code <event>, with no shell. claude-mem is a small compiled launcher on PATH: it finds the plugin root and runs worker-service.cjs hook claude-code <event> under Bun; the worker process is the single dispatcher for all hook logic. Events:
- Setup →
version-check.js(installs missing plugin dependencies, then records the install marker). Still a bash entry, and Claude Code fires it only forclaude --init/--maintenance. - SessionStart →
claude-mem start(worker, async), thenhook claude-code context(context injection); at startup alsonode ${CLAUDE_PLUGIN_ROOT}/scripts/ensure-launcher.cjs(launcher self-heal) - UserPromptSubmit →
hook claude-code session-init - PreToolUse (matcher
Read) →hook claude-code file-context - PostToolUse (matcher
*) →hook claude-code observation - Stop →
hook claude-code summarize(summary generation) - SessionEnd →
hook claude-code session-end
bun install, and compiling the launcher into ~/.local/bin or %LOCALAPPDATA%\claude-mem\bin) is performed by npx claude-mem install / npx claude-mem update with a visible installer spinner. For plugin-marketplace installs, the startup self-heal places the launcher, and the Setup hook (on claude --init / --maintenance) runs bun install when the plugin’s dependencies are missing.
See Plugin Hooks for detailed hook documentation.
2. Worker Service
Express.js HTTP server on a per-user port (default37700 + (uid % 100), override via CLAUDE_MEM_WORKER_PORT) with:
- Search HTTP API endpoints
- Viewer UI HTTP/SSE endpoints
- Async observation processing via the Claude Agent SDK (or Gemini / OpenRouter)
- Real-time updates via Server-Sent Events
- Auto-managed by Bun
3. Database Layer
SQLite3 with bun:sqlite driver featuring:- FTS5 virtual tables for full-text search
- SessionStore for CRUD operations
- SessionSearch for FTS5 queries
- Location:
~/.claude-mem/claude-mem.db
4. Smart Search: mem-search Skill (Default)
Skill-based search with progressive disclosure providing 10 search operations:- Search observations, sessions, prompts (full-text FTS5)
- Filter by type, concept, file
- Get recent context, timeline, timeline by query
- API help documentation
- Skill frontmatter: ~250 tokens (loaded at session start)
- Full instructions: ~2,500 tokens (loaded on-demand when invoked)
- HTTP API endpoints instead of MCP tools
5. Viewer UI
React + TypeScript web interface served by the worker on its configuredCLAUDE_MEM_WORKER_PORT featuring:
- Real-time memory stream via Server-Sent Events
- Infinite scroll pagination with automatic deduplication
- Project filtering and settings persistence
- GPU-accelerated animations
- Self-contained HTML bundle (viewer.html)

