How it works
Claude Code CLI supports lifecycle hooks that LongMem uses to:- Auto-inject context when topics change (via
prompt.ts) - Capture tool observations for future recall (via
post-tool.ts) - Mark session boundaries for temporal organization (via
stop.ts)
Hook architecture
LongMem provides three hooks installed in~/.longmem/hooks/:
1. Prompt hook (hooks/prompt.ts)
Triggered: When the user submits a prompt
Purpose: Captures the prompt and auto-injects relevant memory context
- Auto-start: Ensures daemon is running before capturing
- Context injection: Returns relevant context when topic shifts
- Non-blocking: Always exits 0 (never blocks Claude Code)
2. Post-tool hook (hooks/post-tool.ts)
Triggered: After every tool execution
Purpose: Captures tool name, input, and output for future recall
- Selective capture: Skips meta-tools to avoid noise
- Full observation: Captures tool name, input, and output
- Dry-run support: Respects
LONGMEM_DRY_RUN=1
3. Stop hook (hooks/stop.ts)
Triggered: When the CLI session ends
Purpose: Signals session completion for temporal organization
Installation
Hooks are automatically installed byinstall.ts when you run:
- Detects your Claude Code CLI installation
- Copies hook scripts to
~/.longmem/hooks/ - Registers hooks in Claude Code config
- Makes hooks executable
Hook locations
Frominstall.ts:
~/.longmem/hooks/prompt.js~/.longmem/hooks/post-tool.js~/.longmem/hooks/stop.js
Manual hook configuration
If you need to configure hooks manually, add them to your Claude Code CLI config:Context injection behavior
When context is injected
The prompt hook uses topic change detection to decide when to inject context:- User submits a prompt
- Daemon analyzes prompt against recent session history
- If topic has shifted significantly → inject relevant context
- Otherwise → no injection (prevents token waste)
What context looks like
Injected context appears in the conversation like:Skipped tools
Both the prompt and post-tool hooks skip these tools to avoid noise:Session ID handling
Sessions are identified via environment variables:Verification
Check that hooks are working:- Hooks installed: 3/3
- Last prompt: [timestamp]
- Last observation: [timestamp]
Troubleshooting
Hooks not firing
Verify hook registration:hooks section.
Context not injecting
Enable debug logging:~/.longmem/logs/hook-prompt.log for errors.