Skip to main content

How Claude-Mem Uses Hooks: A Lifecycle-Driven Architecture

Core Principle

Observe the main Claude Code session from the outside, process observations in the background, inject context at the right time.

The Big Picture

Claude-Mem is fundamentally a hook-driven system. Every piece of functionality happens in response to lifecycle events:
As of Claude Code 2.1.0 (ultrathink update), SessionStart hooks no longer display user-visible messages. Context is silently injected via hookSpecificOutput.additionalContext.
Key insight: Claude-Mem doesn’t interrupt or modify Claude Code’s behavior. It observes from the outside and provides value through lifecycle hooks.

Why Hooks?

The Non-Invasive Requirement

Claude-Mem had several architectural constraints:
  1. Can’t modify Claude Code: It’s a closed-source binary
  2. Must be fast: Can’t slow down the main session
  3. Must be reliable: Can’t break Claude Code if it fails
  4. Must be portable: Works on any project without configuration
Solution: External command hooks configured via settings.json

The Hook System Advantage

Claude Code’s hook system provides exactly what we need:

Lifecycle Events

SessionStart, UserPromptSubmit, PreToolUse (Read), PostToolUse, PostToolUseFailure, Stop, SessionEnd

Non-Blocking

Hooks run in parallel, don’t wait for completion

Context Injection

SessionStart and UserPromptSubmit can add context

Tool Observation

PostToolUse sees all tool inputs and outputs

The Hook Scripts

Claude-Mem registers hooks for 7 lifecycle events (SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, PostToolUseFailure, Stop, SessionEnd) plus a Setup hook. npx claude-mem install (and npx claude-mem repair) set up the runtime; the Setup hook runs version-check.js, which completes a plugin-marketplace install whose dependencies are missing and records the install marker. SessionStart runs worker-service start asynchronously on startup, resume, clear, and compact. The synchronous context hook runs on startup, clear, and compact and starts the worker itself when needed. Continue and resume restore the existing conversation without printing or injecting a new timeline, preserving its prompt cache. A third, startup-only SessionStart entry keeps the hook launcher on PATH.

How Every Hook Is Launched

Each runtime hook is one short exec-form command: command + args, with no shell key. Claude Code resolves claude-mem on PATH and spawns it directly, with no bash, no shell string and no per-call shell startup. On Windows that also means no Git Bash requirement and no console window from a bash → node chain.
The other events follow the same shape (session-init, file-context, observation for PostToolUseFailure, summarize, session-end). See plugin/hooks/hooks.json for the full file; it is generated by scripts/build-hooks.js, and the build rejects any runtime entry that has a shell key or a command other than claude-mem. The claude-mem launcher:
  • A small compiled binary (bun build --compile of plugin/scripts/claude-mem-launcher.cjs), placed by npx claude-mem install / update at ~/.local/bin/claude-mem on macOS and Linux, or %LOCALAPPDATA%\claude-mem\bin\claude-mem.exe on Windows, and added to PATH (shell rc file on POSIX, user Path on Windows).
  • Its only job: find the installed plugin root (CLAUDE_PLUGIN_ROOT, then the newest plugin cache version, then the marketplace copy), run worker-service.cjs with the same arguments under Bun, pass the hook’s stdin through, and exit 0 on any failure with one claude-mem: <reason>, continuing without memory line on stderr. It never exits 2, so it never blocks a prompt or a tool.
  • It is version-stable: it carries no claude-mem logic, so an update keeps working with the binary already on PATH, whether it is claude-mem’s own auto-update, npx claude-mem update or a marketplace update. claude-mem --version prints its protocol number.
  • It is not the npm package’s claude-mem CLI. A global npm claude-mem earlier on PATH shadows it; npx claude-mem doctor reports that.
Self-heal for marketplace-only installs. A marketplace install or update runs no installer code, and Claude Code puts a plugin’s bin/ only on the Bash tool’s PATH, not on the hook PATH. The startup-only SessionStart entry, node ${CLAUDE_PLUGIN_ROOT}/scripts/ensure-launcher.cjs, compiles and places the launcher when it is missing or older than the plugin needs. It is synchronous and always exits 0. When the launcher is current it prints nothing; when the memory hooks cannot work it prints one {"systemMessage": "..."} line to stdout, which Claude Code shows to the user (see below). It runs alongside the other SessionStart hooks, so if the launcher was missing, the other hooks in that first session fail with Executable not found in $PATH: "claude-mem", and that session can miss memory context. When the launcher is missing. Claude Code reports a missing hook executable as a non-blocking error on every hook call (Executable not found in $PATH: "claude-mem") and the session carries on without memory. When the launcher cannot be made active, session start shows:
See claude-mem: command not found in hooks.

Setup Hook: Version Check

Purpose: Make sure the loaded plugin can start its worker: install the runtime dependencies the plugin marketplace left out, and record the .install-version marker. Note: npx claude-mem install and npx claude-mem repair install Bun, uv and the plugin dependencies up front. The plugin marketplace (including claude plugin update) only extracts files. When: Only when Claude Code is started with claude --init, claude --init-only or claude -p --maintenance. Claude Code does not fire Setup on plugin install, plugin update or a normal session start, so for marketplace installs it is a manual repair path, not an automatic one. What it does:
  1. Checks that every dependency declared in the plugin’s package.json (plus the zod subpaths the worker loads) is present in its node_modules, not just that the folder exists.
  2. If any are missing, runs bun install --production in the plugin root (up to 120s), then checks again. A partial tree is kept, never deleted.
  3. If dependencies are still missing, prints claude-mem: plugin dependencies missing (…) - run: npx claude-mem@latest install, naming the modules (stderr; Codex receives it as SessionStart context).
  4. If the closure is complete and .install-version is missing, unreadable or from another version, writes the marker itself ({version, installedAt, bun}) instead of sending a working install to the installer.
  5. Always exits 0 — never blocks a session.
Configuration (simplified; the shipped entry is the one hook that still uses "shell": "bash", which first resolves the plugin root and Node):
Key Features:
  • ✅ Fast when the install is complete: a declared-dependency check, no install
  • ✅ Repairs marketplace installs on --init / --maintenance: installs missing dependencies and writes the marker
  • ✅ Names the missing modules when an install cannot be completed
  • ✅ Always exit 0 — never blocks a session
Source: plugin/scripts/version-check.js. npx claude-mem install / npx claude-mem repair do the same work ahead of time — they install Bun + uv globally, run bun install in the plugin cache, and write the .install-version marker — behind a visible clack spinner.

Hook 1: SessionStart - Context Injection

Purpose: Inject relevant context from previous sessions When: A new Claude Code session starts, or after clear or compact (runs alongside the async worker-start SessionStart entry). Skipped on continue and resume. What it does:
  1. Extracts project name from current working directory
  2. Queries SQLite for recent session summaries (last 10)
  3. Queries SQLite for recent observations (configurable, default 50)
  4. Formats as progressive disclosure index
  5. Outputs to stdout (automatically injected into context)
Key decisions:
  • ✅ Runs on startup, clear, and compact
  • ✅ 300-second timeout (allows for npm install if needed)
  • ✅ Progressive disclosure format (index, not full details)
  • ✅ Configurable observation count via CLAUDE_MEM_CONTEXT_OBSERVATIONS
  • ✅ The block is the memory timeline. It opens with one line, Access Claude-Mem Rules + Tools: ~/.claude-mem/RULES-AND-TOOLS.md, then any outage warning and the open work-state lists, then the timeline. The newest timeline entry ends the block, with no footer, so later updates can append rows to it.
  • ✅ How to read the timeline (format, legend, mode, how to fetch details) and how to use claude-mem’s tools (work state, memory search, notes) are in ~/.claude-mem/RULES-AND-TOOLS.md, written whenever the block is rendered and its text changed. The tool guidance is also the MCP server’s instructions.
  • ✅ The terminal banner opens with the trial lines and the same Rules + Tools line, then the timeline. A blank line after the newest entry, then the closing line: the viewer link and how many timeline lines are shown (View Observations Live @ http://localhost:37777 · 50 lines).
Codex compaction: A Codex compaction replaces the thread’s history, timeline included. The root thread gets the timeline back from SessionStart (source compact). Codex never sends SessionStart to a subagent thread, and PostCompact output cannot carry context, so the PostCompact hook marks the thread (~/.claude-mem/state/codex-compaction-pending/) and the thread’s next PostToolUse returns the timeline as additionalContext, once. A thread whose SessionStart already re-injected the timeline does not get it again. Codex subagents: Codex starts a subagent it spawns with SubagentStart, not SessionStart, so the SubagentStart hook gives the subagent the timeline. A forked subagent starts from a copy of its parent’s conversation, timeline included, so the hook skips it: it reads forked_from_id from the first line of the subagent’s rollout (transcript_path). A subagent whose rollout can’t be read gets the timeline. Trusting new Codex hooks: Codex runs a plugin hook only after you trust it, and it treats a new or changed hook as untrusted. After an update that adds or changes a claude-mem hook, Codex says at startup that hooks need review. Trust them, or those hooks won’t run. codex exec can’t ask, so trust them once in interactive Codex first. Output format:
Source: src/cli/handlers/context.ts, run as claude-mem hook claude-code context

Hook 2: UserPromptSubmit (New Session Hook)

Purpose: Initialize session tracking when user submits a prompt When: Before Claude processes the user’s message What it does:
  1. Reads user prompt and session ID from stdin
  2. Creates new session record in SQLite
  3. Saves raw user prompt for full-text search (v4.2.0+)
  4. Starts Bun worker service if not running
  5. Returns immediately (non-blocking)
Configuration:
Key decisions:
  • ✅ No matcher (runs for all prompts)
  • ✅ Creates session record immediately
  • ✅ Stores raw prompts for search (privacy note: local SQLite only)
  • ✅ Auto-starts worker service
  • ✅ Suppresses output (suppressOutput: true)
Database operations:
Source: src/cli/handlers/session-init.ts, run as claude-mem hook claude-code session-init

Hook 3: PostToolUse (Save Observation Hook)

Purpose: Capture tool execution observations for later processing When: Immediately after any tool completes successfully. A failed call (e.g. Bash exiting non-zero) arrives on PostToolUseFailure instead, which runs the same handler and records { error, is_interrupt } as the tool output, so memory also sees attempts that failed. What it does:
  1. Receives tool name, input, output from stdin
  2. Finds active session for current project
  3. Enqueues observation in observation_queue table
  4. Returns immediately (processing happens in worker)
Configuration:
Key decisions:
  • ✅ Matcher: * (captures all tools)
  • ✅ Non-blocking (just enqueues, doesn’t process)
  • ✅ Worker processes observations asynchronously
  • ✅ Parallel execution safe (each hook gets own stdin)
Database operations:
What gets queued:
Source: src/cli/handlers/observation.ts, run as claude-mem hook claude-code observation

Hook 4: Stop Hook (Summary Generation)

Purpose: Generate AI-powered session summaries during the session When: When Claude stops (triggered by Stop lifecycle event) What it does:
  1. Gathers session observations from database
  2. Sends to Claude Agent SDK for summarization
  3. Processes response and extracts structured summary
  4. Stores in session_summaries table
  5. Copies the turn’s deliverables (a deck, a report, a page the agent made for you) into ~/.claude-mem/deliverables/<project>/ and records them, so My Work links them on the request’s card
Configuration:
Key decisions:
  • ✅ Triggered by Stop lifecycle event
  • ✅ Multiple summaries per session (v4.2.0+)
  • ✅ Summaries are checkpoints, not endings
  • ✅ Uses Claude Agent SDK for AI compression
  • ✅ Deliverables are copied by the hook itself, from the transcript’s final turn (files its Write/Edit calls made outside the source tree, and files a work_state_write attached), before the worktree or scratch folder they live in can be deleted; the worker only records the copies. CLAUDE_MEM_DELIVERABLES_ENABLED=false turns it off.
Summary structure:
Source: src/cli/handlers/summarize.ts, run as claude-mem hook claude-code summarize

Hook 5: SessionEnd (Cleanup Hook)

Purpose: Mark sessions as completed when they end When: Claude Code session ends (not on /clear) What it does:
  1. Marks session as completed in database
  2. Allows worker to finish processing
  3. Performs graceful cleanup
Configuration:
Key decisions:
  • ✅ Graceful completion (v4.1.0+)
  • ✅ No longer sends DELETE to workers
  • ✅ Skips cleanup on /clear commands
  • ✅ Preserves ongoing sessions
Why graceful cleanup? Old approach (v3):
Problems:
  • Interrupted summary generation
  • Lost pending observations
  • Race conditions
New approach (v4.1.0+):
Benefits:
  • Worker finishes important operations
  • Summaries complete successfully
  • Clean state transitions
Source: src/cli/handlers/session-end.ts, run as claude-mem hook claude-code session-end

Hook Execution Flow

Session Lifecycle

Hook Timing

As of Claude Code 2.1.0 (ultrathink update), SessionStart hooks no longer display user-visible messages. Context is silently injected via hookSpecificOutput.additionalContext.

The Worker Service Architecture

Why a Background Worker?

Problem: Hooks must be fast (< 1 second) Reality: AI compression takes 5-30 seconds per observation Solution: Hooks enqueue observations, worker processes async

Bun Process Management

Technology: Bun (JavaScript runtime and process manager) Why Bun:
  • Auto-restart on failure
  • Fast startup and low memory footprint
  • Built-in TypeScript support
  • Cross-platform (works on macOS, Linux, Windows)
  • No separate process manager needed
Worker lifecycle:

Worker HTTP API

Technology: Express.js REST API on the worker’s per-user port (default 37700 + (uid % 100), override via CLAUDE_MEM_WORKER_PORT) Endpoints: Why HTTP API?
  • Language-agnostic (hooks can be any language)
  • Easy debugging (curl commands)
  • Standard error handling
  • Proper async handling

Design Patterns

Pattern 1: Fire-and-Forget Hooks

Principle: Hooks should return immediately, not wait for completion

Pattern 2: Queue-Based Processing

Principle: Decouple capture from processing
Benefits:
  • Parallel hook execution safe
  • Worker failure doesn’t affect hooks
  • Retry logic centralized
  • Backpressure handling

Pattern 3: Graceful Degradation

Principle: Memory system failure shouldn’t break Claude Code
Failure modes:
  • Launcher can’t find the plugin or Bun → One stderr line, exit 0, session continues without memory
  • claude-mem not on PATH → Claude Code shows a non-blocking hook error and continues
  • Database locked → Skip observation, log error
  • Worker crashed → Auto-restart via Bun
  • Network issue → Retry with exponential backoff
  • Disk full → Warn user, disable memory

Pattern 4: Progressive Enhancement

Principle: Core functionality works without memory, memory enhances it

Hook Debugging

Debug Mode

Enable detailed hook execution logs:
Output:

Common Issues

Symptoms: Hook command never runsDebugging:
  1. Check /hooks menu - is hook registered?
  2. Verify matcher pattern (case-sensitive!)
  3. Check the launcher is on PATH: command -v claude-mem && claude-mem --version (prints a protocol number), or run npx claude-mem doctor
  4. Look for Executable not found in $PATH: "claude-mem" in claude --debug output; see Troubleshooting
Symptoms: Hook execution exceeds timeoutDebugging:
  1. Check timeout setting (default 60s)
  2. Identify slow operation (database? network?)
  3. Move slow operation to worker
  4. Increase timeout if necessary
Symptoms: SessionStart hook runs but context missingDebugging:
  1. Check stdout (must be valid JSON or plain text)
  2. Verify no stderr output (pollutes JSON)
  3. Check exit code (must be 0)
  4. Look for npm install output (v4.3.1 fix)
Symptoms: PostToolUse hook runs but observations missingDebugging:
  1. Check database: sqlite3 ~/.claude-mem/claude-mem.db "SELECT * FROM observation_queue"
  2. Verify session exists: SELECT * FROM sdk_sessions
  3. Check worker status: npm run worker:status
  4. View worker logs: npm run worker:logs

Testing Hooks Manually


Performance Considerations

Hook Execution Time

Target: < 100ms per hook Actual measurements: Launcher overhead: the compiled claude-mem launcher starts in about 10ms, and runtime hooks spawn no shell. When the Setup hook is fast, and when it is not:
  • With a complete dependency tree it only checks that the declared modules resolve, and rewrites the .install-version marker if it is missing or from another version — no install.
  • Only an incomplete tree — a plugin-marketplace install or claude plugin update whose dependencies were never installed — runs bun install (up to 120s, inside the Setup hook’s 300s timeout). Setup only fires for claude --init / --maintenance, so this never lands on a normal session or the per-prompt path.
  • npx claude-mem install / npx claude-mem repair do the heavy lifting (Bun + uv, bun install inside the plugin cache) up front with a visible clack spinner, so installs made that way never pay it at session start.

Database Performance

Schema optimizations:
  • Indexes on project, created_at_epoch, claude_session_id
  • FTS5 virtual tables for full-text search
  • WAL mode for concurrent reads/writes
Query patterns:

Worker Throughput

Bottleneck: Claude API latency (5-30s per observation) Mitigation:
  • Process observations sequentially (simpler, more predictable)
  • Skip low-value observations (TodoWrite, ListMcpResourcesTool)
  • Batch summaries (generate every N observations, not every observation)
Future optimization:
  • Parallel processing (multiple workers)
  • Smart batching (combine related observations)
  • Lazy summarization (summarize only when needed)

Security Considerations

Hook Command Safety

Risk: Hooks execute arbitrary commands with user permissions Mitigations:
  1. Frozen at startup: Hook configuration captured at start, changes require review
  2. User review required: /hooks menu shows changes, requires approval
  3. Plugin isolation: ${CLAUDE_PLUGIN_ROOT} prevents path traversal
  4. Input validation: Hooks validate stdin schema before processing

Data Privacy

What gets stored:
  • User prompts (raw text) - v4.2.0+
  • Tool inputs and outputs
  • File paths read/modified
  • Session summaries
Privacy guarantees:
  • All data stored locally in ~/.claude-mem/claude-mem.db
  • No cloud uploads (API calls only for AI compression). Local installs send cmem.ai only your email and optional note (with the claude-mem version and OS) if you share them when an interactive terminal install asks, and a numbers-only usage summary (observation counts and token totals, no prompts, paths, or project names) when setup creates a sign-in link; nothing else is sent to cmem.ai unless you choose to upload your prompts during browser setup.
  • SQLite file permissions: user-only read/write
  • Product telemetry is opt-out and never carries prompts, code, or paths — see Telemetry

API Key Protection

Configuration:
  • Anthropic API key in ~/.anthropic/api_key or ANTHROPIC_API_KEY env var
  • Worker inherits environment from Claude Code
  • Never logged or stored in database

Key Takeaways

  1. Hooks are interfaces: They define clean boundaries between systems
  2. Non-blocking is critical: Hooks must return fast, workers do the heavy lifting
  3. Graceful degradation: Memory system can fail without breaking Claude Code
  4. Queue-based decoupling: Capture and processing happen independently
  5. Progressive disclosure: Context injection uses index-first approach
  6. Lifecycle alignment: Each hook has a clear, single purpose

Further Reading


The hook-driven architecture enables Claude-Mem to be both powerful and invisible. Users never notice the memory system working - it just makes Claude smarter over time.