Persistent memory compression system for Claude Code enabling context preservation across sessions with automatic observations, semantic search, and privacy controls
Claude-Mem is a persistent memory compression system for Claude Code that enables context preservation across multiple sessions. It automatically captures tool usage observations, generates semantic summaries, and makes them available to future sessions, maintaining project knowledge continuity.
CLAUDE.md files with activity timelineshttp://localhost:37777<private> tags exclude sensitive content from storage# In Claude Code terminal
/plugin marketplace add thedotmack/claude-mem
/plugin install claude-mem
Restart Claude Code after installation. Context from previous sessions will automatically appear.
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem
npm install
npm run build
npm run worker:start
npm run worker:status
# Check hook installation
cat plugin/hooks/hooks.json
# View worker logs
npm run worker:logs
# Test context injection
npm run test:context
| Hook | Timeout | Purpose |
|---|---|---|
| SessionStart | 120s | Inject context from recent sessions |
| UserPromptSubmit | 60s | Create session, save prompt |
| PostToolUse | 120s | Capture tool observations |
| Stop | 60s | Generate session summary |
The system records every tool interaction:
Captured observations produce:
AI Model Selection:
export CLAUDE_MEM_MODEL=sonnet # haiku, sonnet, opus
export CLAUDE_MEM_PROVIDER=claude # claude, gemini, openrouter
Provider-Specific:
# Gemini
export CLAUDE_MEM_GEMINI_API_KEY="your-key"
export CLAUDE_MEM_GEMINI_MODEL="gemini-2.5-flash-lite"
# OpenRouter
export CLAUDE_MEM_OPENROUTER_API_KEY="your-key"
export CLAUDE_MEM_OPENROUTER_MODEL="xiaomi/mimo-v2-flash:free"
Storage & Networking:
export CLAUDE_MEM_DATA_DIR=~/.claude-mem # Database location
export CLAUDE_MEM_WORKER_PORT=37777 # Service port
export CLAUDE_MEM_WORKER_HOST=127.0.0.1 # Service address
export CLAUDE_MEM_LOG_LEVEL=INFO # DEBUG, INFO, WARN, ERROR, SILENT
Context Injection:
export CLAUDE_MEM_CONTEXT_SESSION_COUNT=10 # Recent sessions to retrieve
export CLAUDE_MEM_CONTEXT_OBSERVATIONS=50 # Injected observations count
export CLAUDE_MEM_CONTEXT_FULL_COUNT=5 # Expanded observations
export CLAUDE_MEM_CONTEXT_FULL_FIELD=narrative # narrative or facts
Display Options:
export CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS=true
export CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS=true
export CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT=true
export CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY=false
export CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE=false
Tool Exclusions:
# Comma-separated list of tools to skip
export CLAUDE_MEM_SKIP_TOOLS="ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion"
Edit ~/.claude-mem/settings.json:
{
"CLAUDE_MEM_MODEL": "sonnet",
"CLAUDE_MEM_MODE": "code",
"CLAUDE_MEM_CONTEXT_OBSERVATIONS": 50,
"CLAUDE_MEM_FOLDER_CLAUDEMD_ENABLED": false
}
Or run interactive setup:
./claude-mem-settings.sh
search - Memory index lookup
Parameters:
query FTS5 search syntax
limit Results per page (default: 20)
offset Pagination offset
type Filter: bugfix, feature, decision, discovery, refactor, change
project Filter by project name
dateStart Filter from date
dateEnd Filter to date
orderBy Sort order
timeline - Chronological context retrieval
Parameters:
anchor Observation ID to center on
query Alternative to anchor
depth_before Observations before (default: 3)
depth_after Observations after (default: 3)
get_observations - Full detail fetching
Parameters:
ids Array of observation IDs (required)
# Boolean operators
"user auth" AND (JWT OR session) NOT deprecated
# Scoped searches
title:authentication
content:database
concepts:gotcha
# Phrase matching
"connection failed"
Achieves ~10x token savings through progressive disclosure:
Example:
User: "What bugs did we fix last week?"
Claude automatically:
1. Calls search(type: "bugfix", dateStart: "2025-01-09")
2. Reviews index results
3. Calls timeline(anchor: [relevant_id]) if context needed
4. Calls get_observations(ids: [...]) for specific details
A mode defines four aspects:
| Mode | Description |
|---|---|
code |
Default software development mode |
code--chill |
Fewer observations, only records painful-to-rediscover items |
email-investigation |
Email archive analysis, entity tracking |
code--es |
Spanish output |
code--fr |
French output |
code--de |
German output |
code--ja |
Japanese output |
code--zh |
Chinese output |
Via settings.json:
{
"CLAUDE_MEM_MODE": "code--es"
}
Via environment:
export CLAUDE_MEM_MODE="code--fr"
Uses -- separator notation:
code--es loads code config, then merges code--es overridesWrap sensitive content to exclude from storage:
<private>
Your sensitive content here
</private>
Behavior:
Sensitive Information:
<private>
Error: Database connection failed
Host: internal-db-prod.company.com
Port: 5432
User: admin_user
</private>
Temporary Context:
<private>
Deadline: Friday 5pm
Contact: john@company.com
</private>
Debug Output:
<private>
[DEBUG] Full stack trace...
[DEBUG] Memory dump...
</private>
# Check that private content was filtered
sqlite3 ~/.claude-mem/claude-mem.db "SELECT prompt_text FROM user_prompts LIMIT 5;"
sqlite3 ~/.claude-mem/claude-mem.db "SELECT narrative FROM observations LIMIT 5;"
Default: ~/.claude-mem/
claude-mem.db - SQLite database.worker.pid - Worker process IDlogs/worker-YYYY-MM-DD.log - Daily logssettings.json - Configuration-- Recent sessions
SELECT session_id, project, created_at, status
FROM sdk_sessions
ORDER BY created_at DESC LIMIT 10;
-- Search observations
SELECT title, type, created_at
FROM observations
WHERE project = 'my-project';
npm run worker:start # Start background service
npm run worker:stop # Stop service
npm run worker:restart # Restart service
npm run worker:status # Check status
npm run worker:logs # View logs
Sessions span multiple prompts:
/clear doesn't terminate session (continues with new prompt number)# Check port availability
lsof -i :37777
# Use alternative port
export CLAUDE_MEM_WORKER_PORT=38000
npm run worker:restart
# Verify hooks installed
cat plugin/hooks/hooks.json
# Check database has data
sqlite3 ~/.claude-mem/claude-mem.db "SELECT COUNT(*) FROM observations;"
# View worker logs
npm run worker:logs
# Reduce observations
export CLAUDE_MEM_CONTEXT_OBSERVATIONS=25
# Use chill mode
export CLAUDE_MEM_MODE="code--chill"
# Backup and recreate
cp ~/.claude-mem/claude-mem.db ~/.claude-mem/claude-mem.db.bak
rm ~/.claude-mem/claude-mem.db
npm run worker:restart
CLAUDE_MEM_CONTEXT_OBSERVATIONS based on project size