Persistent memory system for Claude Code - seamlessly preserve context across sessions
You can install this plugin from any of these themed marketplaces. Choose one, add it as a marketplace, then install the plugin.
Choose your preferred installation method below
A marketplace is a collection of plugins. Every plugin gets an auto-generated marketplace JSON for individual installation, plus inclusion in category and themed collections. Add a marketplace once (step 1), then install any plugin from it (step 2).
One-time setup for access to all plugins
When to use: If you plan to install multiple plugins now or later
Step 1: Add the marketplace (one-time)
/plugin marketplace add https://claudepluginhub.com/marketplaces/all.json
Run this once to access all plugins
Step 2: Install this plugin
/plugin install claude-mem@all
Use this plugin's auto-generated marketplace JSON for individual installation
When to use: If you only want to try this specific plugin
Step 1: Add this plugin's marketplace
/plugin marketplace add https://claudepluginhub.com/marketplaces/plugins/claude-mem.json
Step 2: Install the plugin
/plugin install claude-mem@claude-mem
Persistent memory compression system for Claude Code
Claude-Mem seamlessly preserves context across sessions by automatically capturing tool usage observations, generating semantic summaries, and making them available to future sessions. This enables Claude to maintain continuity of knowledge about projects even after sessions end or reconnect.
BREAKING CHANGES - Please Read:
~/.claude-mem/
to ${CLAUDE_PLUGIN_ROOT}/data/
(inside plugin directory)npm run worker:start
neededSee CHANGELOG.md for complete details.
Claude-Mem is a Claude Code plugin that provides persistent memory across sessions. When you work with Claude Code on a project, claude-mem:
┌─────────────────────────────────────────────────────────────────┐
│ 1. Session Starts → Context Hook Fires │
│ Injects summaries from last 3 sessions into Claude's context │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────────┐
│ 2. User Types Prompt → UserPromptSubmit Hook Fires │
│ Creates SDK session in database, notifies worker service │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────────┐
│ 3. Claude Uses Tools → PostToolUse Hook Fires (100+ times) │
│ Sends observations to worker service for processing │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────────┐
│ 4. Worker Processes → Claude Agent SDK Analyzes │
│ Extracts structured learnings via iterative AI processing │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────────┐
│ 5. Claude Stops → Stop Hook Fires │
│ Generates final summary with request, status, next steps │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────────────┐
│ 6. Session Ends → Cleanup Hook Fires │
│ Marks session complete, ready for next session context │
└─────────────────────────────────────────────────────────────────┘
context-hook.js
): Queries database for last 3 sessions and injects contextnew-hook.js
): Creates/reuses SDK session, sends init signalsave-hook.js
): Sends tool observations to worker servicesummary-hook.js
): Triggers final summary generationcleanup-hook.js
): Marks session as completed/failedLong-running HTTP service (managed by PM2) that:
Endpoints:
POST /sessions/:id/init
- Initialize sessionPOST /sessions/:id/observations
- Queue tool observationsPOST /sessions/:id/summarize
- Generate summaryGET /sessions/:id/status
- Check statusDELETE /sessions/:id
- Clean up sessionGET /health
- Health checkUses Claude Agent SDK (@anthropic-ai/claude-agent-sdk
) to:
Claude-Mem includes a Model Context Protocol (MCP) server that exposes 6 specialized search tools for querying stored observations and sessions:
Search Tools:
search_observations
- Full-text search across observation titles, narratives, facts, and conceptssearch_sessions
- Full-text search across session summaries, requests, and learningsfind_by_concept
- Find observations tagged with specific conceptsfind_by_file
- Find observations and sessions that reference specific file pathsfind_by_type
- Find observations by type (decision, bugfix, feature, refactor, discovery, change)advanced_search
- Combined search with filters across observations and sessionsAll search results are returned in search_result
format with citations enabled, allowing Claude to reference specific observations and sessions from your project history using the claude-mem://
URI scheme.
Configuration: The MCP server is automatically registered via plugin/.mcp.json
and runs when Claude Code starts.
SQLite database (${CLAUDE_PLUGIN_ROOT}/data/claude-mem.db
) with tables:
Full-Text Search: The database includes FTS5 (Full-Text Search) virtual tables for fast searching across observations and summaries, powering the MCP search tools.
# Ensure Node.js 18+ is installed
node --version # Should be >= 18.0.0
Install directly from Claude Code using the plugin marketplace:
# Add the marketplace
/plugin marketplace add thedotmack/claude-mem
# Install the plugin
/plugin install claude-mem
The plugin will:
That's it! The plugin is ready to use. Start a new Claude Code session and you'll see context from previous sessions automatically loaded.
# Clone the repository
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem
# Install dependencies
npm install
# Build hooks and worker service
npm run build
# Worker service will auto-start on first Claude Code session
# Or manually start with:
npm run worker:start
# Verify worker is running
npm run worker:status
# Install from NPM (when published)
npm install -g claude-mem
# Worker service auto-starts on first hook execution
Verify Plugin Installation
Check that hooks are configured in Claude Code:
cat plugin/hooks/hooks.json
Data Directory Location
v4.0.0+ stores data in ${CLAUDE_PLUGIN_ROOT}/data/
:
${CLAUDE_PLUGIN_ROOT}/data/claude-mem.db
${CLAUDE_PLUGIN_ROOT}/data/worker.port
${CLAUDE_PLUGIN_ROOT}/data/logs/
For development/testing, you can override:
export CLAUDE_MEM_DATA_DIR=/custom/path
Check Worker Logs
npm run worker:logs
Test Context Retrieval
npm run test:context
Claude-Mem works automatically once installed. No manual intervention required!
Once claude-mem is installed as a plugin, six search tools become available in your Claude Code sessions:
Example Queries:
"Use search_observations to find all decisions about the build system"
→ Searches for observations with type="decision" and content matching "build system"
"Use find_by_file to show me everything related to worker-service.ts"
→ Returns all observations and sessions that read/modified worker-service.ts
"Use search_sessions to find what we learned about hooks"
→ Searches session summaries for mentions of "hooks" in learnings
"Use find_by_concept to show observations tagged with 'architecture'"
→ Returns observations tagged with the concept "architecture"
All results include:
claude-mem://observation/{id}
or claude-mem://session/{id}
URIsNote: v4.0+ auto-starts the worker on first session. Manual commands below are optional.
# Start worker service (optional - auto-starts automatically)
npm run worker:start
# Stop worker service
npm run worker:stop
# Restart worker service
npm run worker:restart
# View worker logs
npm run worker:logs
# Check worker status
npm run worker:status
# Run all tests
npm test
# Test context injection
npm run test:context
# Verbose context test
npm run test:context:verbose
# Build hooks and worker
npm run build
# Build only hooks
npm run build:hooks
# Publish to NPM (maintainers only)
npm run publish:npm
Context is stored in SQLite database. Location varies by version:
${CLAUDE_PLUGIN_ROOT}/data/claude-mem.db
(inside plugin)~/.claude-mem/claude-mem.db
(legacy)Query the database directly:
# v4.0+ uses ~/.claude-mem directory
sqlite3 ~/.claude-mem/claude-mem.db
# View recent sessions
SELECT session_id, project, created_at, status FROM sdk_sessions ORDER BY created_at DESC LIMIT 10;
# View session summaries
SELECT session_id, request, completed, learned FROM session_summaries ORDER BY created_at DESC LIMIT 5;
# View observations for a session
SELECT tool_name, created_at FROM observations WHERE session_id = 'YOUR_SESSION_ID';
Layer | Technology |
---|---|
Language | TypeScript (ES2022, ESNext modules) |
Runtime | Node.js 18+ |
Database | SQLite 3 with better-sqlite3 driver |
HTTP Server | Express.js 4.18 |
AI SDK | @anthropic-ai/claude-agent-sdk |
Build Tool | esbuild (bundles TypeScript) |
Process Manager | PM2 |
Testing | Node.js built-in test runner |
claude-mem/
├── src/
│ ├── bin/hooks/ # Entry point scripts for 5 hooks
│ │ ├── context-hook.ts # SessionStart
│ │ ├── new-hook.ts # UserPromptSubmit
│ │ ├── save-hook.ts # PostToolUse
│ │ ├── summary-hook.ts # Stop
│ │ └── cleanup-hook.ts # SessionEnd
│ │
│ ├── hooks/ # Hook implementation logic
│ │ ├── context.ts
│ │ ├── new.ts
│ │ ├── save.ts
│ │ ├── summary.ts
│ │ └── cleanup.ts
│ │
│ ├── servers/ # MCP servers
│ │ └── search-server.ts # MCP search tools server
│ │
│ ├── sdk/ # Claude Agent SDK integration
│ │ ├── prompts.ts # XML prompt builders
│ │ ├── parser.ts # XML response parser
│ │ └── worker.ts # Main SDK agent loop
│ │
│ ├── services/
│ │ ├── worker-service.ts # Express HTTP service
│ │ └── sqlite/ # Database layer
│ │ ├── Database.ts
│ │ ├── HooksDatabase.ts
│ │ ├── SessionSearch.ts # FTS5 search service
│ │ ├── migrations.ts
│ │ └── types.ts
│ │
│ ├── shared/ # Shared utilities
│ │ ├── config.ts
│ │ ├── paths.ts
│ │ └── storage.ts
│ │
│ └── utils/
│ ├── logger.ts
│ ├── platform.ts
│ └── port-allocator.ts
│
├── plugin/ # Plugin distribution
│ ├── .claude-plugin/
│ │ └── plugin.json
│ ├── .mcp.json # MCP server configuration
│ ├── hooks/
│ │ └── hooks.json
│ └── scripts/ # Built executables
│ ├── context-hook.js
│ ├── new-hook.js
│ ├── save-hook.js
│ ├── summary-hook.js
│ ├── cleanup-hook.js
│ ├── worker-service.cjs # Background worker
│ └── search-server.js # MCP search server
│
├── tests/ # Test suite
├── context/ # Architecture docs
└── ecosystem.config.cjs # PM2 configuration
Hook (stdin) → Database → Worker Service → SDK Processor → Database → Next Session Hook
Claude Request → MCP Server → SessionSearch Service → FTS5 Database → Search Results → Claude
search_observations
)search_result
blocks with citationsVariable | Default | Description |
---|---|---|
CLAUDE_PLUGIN_ROOT | Set by Claude Code | Plugin installation directory |
CLAUDE_MEM_DATA_DIR | ${CLAUDE_PLUGIN_ROOT}/data/ | Data directory override (dev only) |
CLAUDE_MEM_WORKER_PORT | 0 (dynamic) | Worker service port (37000-37999) |
NODE_ENV | production | Environment mode |
FORCE_COLOR | 1 | Enable colored logs |
v4.0.0+ Structure:
${CLAUDE_PLUGIN_ROOT}/data/
├── claude-mem.db # SQLite database
├── worker.port # Current worker port file
└── logs/
├── worker-out.log # Worker stdout logs
└── worker-error.log # Worker stderr logs
Legacy (v3.x):
~/.claude-mem/ # Old location (no longer used)
Hooks are configured in plugin/hooks/hooks.json
:
{
"SessionStart": {
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/context-hook.js",
"timeout": 180000
},
"UserPromptSubmit": {
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/new-hook.js",
"timeout": 60000
},
"PostToolUse": {
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/save-hook.js",
"timeout": 180000
},
"Stop": {
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/summary-hook.js",
"timeout": 60000
},
"SessionEnd": {
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/cleanup-hook.js",
"timeout": 60000
}
}
The MCP search server is configured in plugin/.mcp.json
:
{
"mcpServers": {
"claude-mem-search": {
"type": "stdio",
"command": "${CLAUDE_PLUGIN_ROOT}/scripts/search-server.js"
}
}
}
This registers the claude-mem-search
server with Claude Code, making the 6 search tools available in all sessions. The server is automatically started when Claude Code launches and communicates via stdio transport.
# Clone repository
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem
# Install dependencies
npm install
# Build all components
npm run build
The build process:
plugin/scripts/
plugin/scripts/search-server.js
plugin/scripts/worker-service.cjs
# Run all tests
npm test
# Run specific test file
node --test tests/session-lifecycle.test.ts
npm run build
to compilenpm run test:context
or start Claude Codenpm run worker:logs
src/hooks/your-hook.ts
src/bin/hooks/your-hook.ts
plugin/hooks/hooks.json
npm run build
src/services/sqlite/migrations.ts
src/services/sqlite/types.ts
src/services/sqlite/HooksDatabase.ts
src/sdk/prompts.ts
src/sdk/parser.ts
to handle new XML structurenpm test
Problem: Worker service not starting
# Check if PM2 is running
pm2 status
# Check worker logs
npm run worker:logs
# Restart worker
npm run worker:restart
# Full reset
pm2 delete claude-mem-worker
npm run worker:start
Problem: Port allocation failed
# Check if port file exists (v4.0+)
cat ${CLAUDE_PLUGIN_ROOT}/data/worker.port
# Manually specify port
CLAUDE_MEM_WORKER_PORT=37500 npm run worker:start
Problem: Hooks not firing
# Test context hook manually
echo '{"session_id":"test-123","cwd":"'$(pwd)'","source":"startup"}' | node plugin/scripts/context-hook.js
# Check hook permissions
ls -la plugin/scripts/*.js
# Verify hooks.json is valid
cat plugin/hooks/hooks.json | jq .
Problem: Context not appearing
# Check if summaries exist
sqlite3 ~/.claude-mem/claude-mem.db "SELECT COUNT(*) FROM session_summaries;"
# View recent sessions
npm run test:context:verbose
# Check database integrity
sqlite3 ~/.claude-mem/claude-mem.db "PRAGMA integrity_check;"
Problem: Database locked
# Close all connections
pm2 stop claude-mem-worker
# Check for stale locks
lsof ~/.claude-mem/claude-mem.db
# Backup and recreate (nuclear option)
cp ~/.claude-mem/claude-mem.db ~/.claude-mem/claude-mem.db.backup
rm ~/.claude-mem/claude-mem.db
npm run worker:start # Will recreate schema
Enable verbose logging:
# Set debug mode
export DEBUG=claude-mem:*
# View all logs
npm run worker:logs
Check correlation IDs to trace observations through the pipeline:
sqlite3 ~/.claude-mem/claude-mem.db "SELECT correlation_id, tool_name, created_at FROM observations WHERE session_id = 'YOUR_SESSION_ID' ORDER BY created_at;"
Contributions are welcome! Please:
git checkout -b feature/amazing-feature
)git commit -m 'Add amazing feature'
)git push origin feature/amazing-feature
)This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0).
Copyright (C) 2025 Alex Newman (@thedotmack). All rights reserved.
See the LICENSE file for full details.
For more information about AGPL-3.0, see: https://www.gnu.org/licenses/agpl-3.0.html
${CLAUDE_PLUGIN_ROOT}/data/
hookSpecificOutput
JSON formatBuilt with Claude Agent SDK | Powered by Claude Code | Made with TypeScript
4.0.2