Back to Mem0

Claude Code

docs/integrations/claude-code.mdx

2.0.209.3 KB
Original Source

Claude Code forgets everything between sessions. This plugin fixes that. Install it, work normally, and Claude remembers what happened across sessions.

Prerequisites

  1. A Mem0 Platform account and API key (starts with m0-):

  2. A Claude Code version that supports plugin agents, worktree isolation for agents, and the SubagentStart, SubagentStop, and PostToolUseFailure hook events.

  3. Python 3.10+ and Git on your machine.

Quick start

bash
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.

Managing the plugin

bash
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)

What you can do

Automatic memory

Once installed, memory works without any action from you:

  • Capture happens in the background as you work. Hooks save user messages, Claude's answers, changed files, and test/build results locally. Nothing calls a model or slows your session.
  • Recall happens automatically before Claude's first response in a new session. The plugin searches your memories with your prompt and injects up to five relevant ones.

Commands

CommandWhat it does
/mem0:searchSearch memories from earlier sessions. Supports --top-k <n>, --category <name>, and --scope <repo|dir|mine>.
/mem0:statusCheck if memory is working: config, capture state, pending flushes, API key validity.
/mem0:forgetDelete your memories for this repo (shared project memory stays unless you pass --include-project-memory).
/mem0:pausePause memory capture.
/mem0:resumeResume capture after a pause.
/mem0:rememberTell 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.

Search tool

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.

Sidekick agent

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.

text
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.

How it works

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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

Memory scoping

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>

Search scope

Every memory carries identifiers showing where it came from:

IdentifierWhat it isExample
user_idYou (personal memory only)Your Mem0 user ID
agent_idThe project identity (shared memory only)acme-payments-api
app_idThe repository (scopes both lanes)acme-payments-api
run_idThe Claude Code sessionThe session ID

A search returns the union of shared project memory and your personal preferences. The scope narrows the project part:

ScopeWhat you get
repo (default)All project memory across every subdirectory, plus your preferences
dirProject memory from the directory you're in (and its children), plus your preferences
mineYour 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.

Settings

SettingDefaultWhat it controls
api_keyrequiredMem0 Platform API key.
user_idlocal account nameUser 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_k3Max memories per explicit search (1 to 20).
max_context_chars4000Max characters returned per search (1,000 to 10,000).
search_scoperepoDefault scope: repo, dir, or mine. Also read from MEM0_CODE_SEARCH_SCOPE.

Upgrading from 0.2.x

Breaking update. Your memories carry over, most local config does not.

  • Memories carry over. Same user and repository scoping, including ~/.mem0/project_map.json.
  • Env vars still work. MEM0_API_KEY, MEM0_USER_ID, MEM0_PROJECT_ID.
  • Commands replaced. Old commands replaced by /mem0:search, /mem0:status, /mem0:forget, /mem0:pause, /mem0:resume, /mem0:remember.
  • MCP server replaced. Nine read/write tools replaced by the single read-only search_memories tool.
  • Local config ignored. ~/.mem0/settings.json and per-project mem0.md files are no longer read.
  • Old memories searchable, not by category. Normal search finds pre-upgrade memories, but category filters do not.
bash
claude plugin marketplace update mem0-plugins
claude plugin update mem0@mem0-plugins --scope user

Troubleshooting

ProblemFix
Missing keyReinstall with --config api_key="$MEM0_API_KEY" while the var is set.
401 UnauthorizedAPI key is invalid or expired. Run /mem0:status to confirm.
No memory after ending a sessionExtraction runs in the background. Wait a moment, then search again.
Sidekick won't startMust be in a Git repo. Check that your Claude Code version supports plugin agents and worktrees.
Remove the pluginclaude plugin uninstall mem0@mem0-plugins
<CardGroup cols={2}> <Card title="Mem0 MCP Setup" icon="puzzle-piece" href="/platform/mem0-mcp"> Detailed MCP configuration for all clients </Card> <Card title="Codex Integration" icon="/images/provider-icons/openai.svg" href="/integrations/codex"> Add Mem0 memory to OpenAI Codex workflows </Card> </CardGroup> <Snippet file="star-on-github.mdx" />