docs/integrations/claude-code.mdx
Claude Code forgets everything between sessions. This plugin fixes that. Install it, work normally, and Claude remembers what happened across sessions.
A Mem0 Platform account and API key (starts with m0-):
A Claude Code version that supports plugin agents, worktree isolation for agents, and the SubagentStart, SubagentStop, and PostToolUseFailure hook events.
Python 3.10+ and Git on your machine.
export MEM0_API_KEY='your-mem0-api-key'
claude plugin marketplace add mem0ai/mem0
claude plugin install mem0@mem0-plugins --scope user --config api_key="$MEM0_API_KEY"
unset MEM0_API_KEY
Restart Claude Code (or run /reload-plugins), open a Git repository, and work normally. That's it.
claude plugin marketplace update mem0-plugins # refresh the marketplace catalog
claude plugin update mem0@mem0-plugins --scope user # update the plugin (restart to apply)
claude plugin uninstall mem0@mem0-plugins # uninstall the plugin (keeps the marketplace)
Once installed, memory works without any action from you:
| Command | What it does |
|---|---|
/mem0:search | Search memories from earlier sessions. Supports --top-k <n>, --category <name>, and --scope <repo|dir|mine>. |
/mem0:status | Check if memory is working: config, capture state, pending flushes, API key validity. |
/mem0:forget | Delete your memories for this repo (shared project memory stays unless you pass --include-project-memory). |
/mem0:pause | Pause memory capture. |
/mem0:resume | Resume capture after a pause. |
/mem0:remember | Tell Claude to capture something specific in its reply. |
Categories for --category: project_knowledge, decisions_and_constraints, workflows, problems_and_fixes, results. Without it, all categories are searched.
After the automatic first-prompt search, Claude can also call search_memories with a specific question, and you can run /mem0:search yourself. Explicit searches return up to 3 results by default (configurable to 20). All results are capped at 4,000 characters.
mem0:sidekick is a Sonnet coding agent that runs in a separate Git worktree. Use it to offload investigation, implementation, testing, or review without burning main-session context.
Ask Mem0's sidekick to investigate and implement this in its separate worktree.
Review its result and send any corrections back to the same sidekick.
Changes stay in the sidekick's worktree until the main agent reviews and copies them over. By default the worktree branches from the repo's default branch. Set worktree.baseRef to "head" in your Claude settings to branch from the current commit instead. Uncommitted changes are not copied into the sidekick's worktree.
The plugin follows a simple cycle: capture during a session, extract memories in the background, recall in the next session.
<Frame> </Frame>Step by step:
Capture. Hooks save the main agent's activity locally: user messages, Claude's answers, changed files, and short test/build results. Subagent (sidekick) output is excluded. No model calls, no blocking.
Flush. After every five completed exchanges, a detached background worker sends that batch to Mem0. Large exchanges flush sooner. Ending or compacting the session flushes anything remaining. If the session sits idle, an auto-flush runs after five minutes (configurable with MEM0_CODE_IDLE_FLUSH_SECONDS). The timer resets on each new exchange. The worker survives Claude Code exiting.
Extract. Each flush sends a single add call with agent_id (the project identity), user_id (you), app_id (the repository), and run_id (the session). Mem0 classifies each extracted memory as shared project knowledge or a personal preference.
Recall. On the next session's first prompt, the plugin searches automatically and supplies up to five relevant memories. No model is called to write the query.
Each add call carries separate extraction instructions for project facts and personal facts. Mem0 sorts each memory into one of two buckets:
Shared project memory (keyed by agent_id, scoped by app_id): one namespace per repository. Stores conventions, decisions, constraints, commands that work, and commands that failed with what fixed them. Everyone on the repo reads and writes the same pool. Project memory never carries a user_id, so teammates' searches never mix in your preferences. Directory information is stored in metadata for directory-scoped searches.
Personal memory (keyed by user_id, scoped by app_id): your preferred tools, style, habits, and anything you asked to be remembered. Scoped to the repository by app_id, private to you.
Credentials are redacted before anything leaves your machine.
<Frame> </Frame>Every memory carries identifiers showing where it came from:
| Identifier | What it is | Example |
|---|---|---|
user_id | You (personal memory only) | Your Mem0 user ID |
agent_id | The project identity (shared memory only) | acme-payments-api |
app_id | The repository (scopes both lanes) | acme-payments-api |
run_id | The Claude Code session | The session ID |
A search returns the union of shared project memory and your personal preferences. The scope narrows the project part:
| Scope | What you get |
|---|---|
repo (default) | All project memory across every subdirectory, plus your preferences |
dir | Project memory from the directory you're in (and its children), plus your preferences |
mine | Your personal preferences only |
The dir scope is hierarchical: a parent directory sees everything in its children, but a child never sees the parent's memories.
Pass --run-id <session-id> to see only what one specific session recorded. Set the default scope with the search_scope setting or MEM0_CODE_SEARCH_SCOPE env var.
| Setting | Default | What it controls |
|---|---|---|
api_key | required | Mem0 Platform API key. |
user_id | local account name | User ID for memory storage. Resolved from: setting, MEM0_CODE_USER_ID, MEM0_USER_ID, MEM0_RESOLVED_USER_ID, $USER, %USERNAME%, then default. Set explicitly to share memories across machines. |
top_k | 3 | Max memories per explicit search (1 to 20). |
max_context_chars | 4000 | Max characters returned per search (1,000 to 10,000). |
search_scope | repo | Default scope: repo, dir, or mine. Also read from MEM0_CODE_SEARCH_SCOPE. |
Breaking update. Your memories carry over, most local config does not.
~/.mem0/project_map.json.MEM0_API_KEY, MEM0_USER_ID, MEM0_PROJECT_ID./mem0:search, /mem0:status, /mem0:forget, /mem0:pause, /mem0:resume, /mem0:remember.search_memories tool.~/.mem0/settings.json and per-project mem0.md files are no longer read.claude plugin marketplace update mem0-plugins
claude plugin update mem0@mem0-plugins --scope user
| Problem | Fix |
|---|---|
| Missing key | Reinstall with --config api_key="$MEM0_API_KEY" while the var is set. |
401 Unauthorized | API key is invalid or expired. Run /mem0:status to confirm. |
| No memory after ending a session | Extraction runs in the background. Wait a moment, then search again. |
| Sidekick won't start | Must be in a Git repo. Check that your Claude Code version supports plugin agents and worktrees. |
| Remove the plugin | claude plugin uninstall mem0@mem0-plugins |