Plugin Hooks
Claude-Mem integrates with Claude Code through 7 hook scripts across 5 lifecycle events that capture events and inject context.Hook Overview
| Hook Name | Purpose | Timeout | Script |
|---|---|---|---|
| SessionStart | Smart dependency installation | 300s | smart-install.js |
| SessionStart | Inject context from previous sessions | 300s | context-hook.js |
| SessionStart | Display first-time setup message | 10s | user-message-hook.js |
| UserPromptSubmit | Create/track new sessions | 120s | new-hook.js |
| PostToolUse | Capture tool execution observations | 120s | save-hook.js |
| Stop | Generate session summaries | 120s | summary-hook.js |
| SessionEnd | Mark sessions complete | 120s | cleanup-hook.js |
Hook Configuration
Hooks are configured inplugin/hooks/hooks.json:
1. SessionStart Hook - Smart Install (smart-install.js)
Purpose: Intelligently manage dependencies and ensure worker service is running.
Behavior:
- Checks if dependencies need installation using version marker (
.install-version) - Only runs npm install when:
- First-time installation (no node_modules)
- Version changed in package.json
- Critical dependency missing (e.g., better-sqlite3)
- Provides Windows-specific error messages for build tool issues
- Auto-starts PM2 worker service after installation
- Fast when already installed (~10ms vs 2-5 seconds)
scripts/smart-install.js
Key Features:
- Version caching prevents redundant installs
- Cross-platform compatible (Windows, macOS, Linux)
- Helpful error messages with troubleshooting steps
- Non-blocking worker startup
2. SessionStart Hook - Context Injection (context-hook.js)
Purpose: Inject context from previous sessions into Claude’s initial context.
Behavior:
- Retrieves last 10 session summaries with three-tier verbosity (v4.2.0)
- Retrieves last 50 observations (configurable via
CLAUDE_MEM_CONTEXT_OBSERVATIONS) - Returns context via
hookSpecificOutputin JSON format (fixed in v4.1.0) - Formats results as progressive disclosure index
src/hooks/context-hook.ts
3. SessionStart Hook - User Message (user-message-hook.js)
Purpose: Display helpful user messages during first-time setup or when viewing context.
Behavior:
- Shows first-time setup message when node_modules is missing
- Displays formatted context information with colors
- Provides tips for using claude-mem effectively
- Shows link to web viewer UI (http://localhost:37777)
- Exits with code 3 (informational, not error)
plugin/scripts/user-message-hook.js (minified)
Key Features:
- User-friendly first-time setup experience
- Visual context display with colors
- Links to new features (viewer UI)
- Non-intrusive messaging
4. UserPromptSubmit Hook (new-hook.js)
Purpose: Create new session records and initialize session tracking.
Behavior:
- Creates new session in database
- Initializes session tracking
- Saves raw user prompts for full-text search (as of v4.2.0)
- Sends init signal to worker service
src/hooks/new-hook.ts
5. PostToolUse Hook (save-hook.js)
Purpose: Capture tool execution observations.
Behavior:
- Fires after EVERY tool execution (Read, Write, Edit, Bash, etc.)
- Sends observations to worker service for processing
- Includes correlation IDs for tracing
- Filters low-value observations
src/hooks/save-hook.ts
6. Stop Hook (summary-hook.js)
Purpose: Generate session summaries when Claude stops.
Behavior:
- Triggers final summary generation
- Sends summarize request to worker service
- Summary includes: request, completed, learned, next_steps
src/hooks/summary-hook.ts
7. SessionEnd Hook (cleanup-hook.js)
Purpose: Mark sessions as completed (graceful cleanup as of v4.1.0).
Behavior:
- Marks sessions as completed
- Skips cleanup on
/clearcommands to preserve ongoing sessions - Allows workers to finish pending operations naturally
- Previously sent DELETE requests; now uses graceful completion
src/hooks/cleanup-hook.ts
Hook Development
Adding a New Hook
- Create hook implementation in
src/hooks/your-hook.ts - Add to
plugin/hooks/hooks.json - Rebuild with
npm run build
Hook Best Practices
- Fast execution: Hooks should complete quickly (< 1s ideal)
- Graceful degradation: Don’t block Claude if worker is down
- Structured logging: Use logger for debugging
- Error handling: Catch and log errors, don’t crash
- JSON output: Use
hookSpecificOutputfor context injection

