docs/integrations/claude-code.mdx
Add persistent memory to Claude Code (CLI) and Claude Cowork (desktop app) with the Mem0 plugin. Your agent forgets everything between sessions — this plugin fixes that by connecting to Mem0's cloud memory layer via MCP, automatically capturing learnings at key lifecycle points, and retrieving relevant context before every response.
Before setting up Mem0 with Claude Code, ensure you have:
A Mem0 Platform account and API key:
m0-)Claude Code CLI or Claude Cowork desktop app installed
Your API key added to your shell profile (persists across sessions):
echo 'export MEM0_API_KEY="m0-your-api-key"' >> ~/.bashrc
source ~/.bashrc
Confirm it's set:
echo $MEM0_API_KEY
# Should print: m0-your-api-key
Install the full plugin including MCP server, lifecycle hooks, and SDK skill.
Add the Mem0 marketplace:
/plugin marketplace add mem0ai/mem0
Install the plugin:
/plugin install mem0@mem0-plugins
Claude Cowork desktop app: Open the Cowork tab, click Customize in the sidebar, click Browse plugins, and install Mem0.
Add the Mem0 MCP server directly with a single command:
npx mcp-add \
--name mem0-mcp \
--type http \
--url "https://mcp.mem0.ai/mcp" \
--clients "claude code"
This gives you the MCP tools but not the lifecycle hooks or SDK skill.
Add to your Claude Code MCP config (.mcp.json):
{
"mcpServers": {
"mem0": {
"type": "http",
"url": "https://mcp.mem0.ai/mcp/",
"headers": {
"Authorization": "Token ${MEM0_API_KEY}"
}
}
}
}
/mem0:onboardAfter installing the plugin, start a new Claude Code session and run:
/mem0:onboard
This runs the setup wizard which:
CLAUDE.md, AGENTS.md, .cursorrules)The onboarding is idempotent — safe to re-run anytime. It auto-triggers on first session in a new project, but you can always invoke it manually.
The plugin includes 17 skills accessible via /mem0: commands:
| Command | Description |
|---|---|
/mem0:remember | Store a memory verbatim — decisions, preferences, conventions |
/mem0:tour | Browse all memories grouped by category |
/mem0:peek | Quick search with compact one-liner results |
/mem0:stats | Session and project memory statistics |
/mem0:dream | Consolidate memories — merge duplicates, resolve contradictions |
/mem0:pin | Protect critical memories from pruning |
/mem0:forget | Delete memories by search or ID |
/mem0:health | Diagnose connectivity, API key, and read/write |
/mem0:export | Export memories to portable Markdown |
/mem0:import | Import memories from export file or MEMORY.md |
| Component | Plugin Install | MCP Only |
|---|---|---|
| MCP Server (9 memory tools) | Yes | Yes |
| Lifecycle Hooks | Yes | No |
| Mem0 SDK Skill | Yes | No |
Once installed, the following tools are available in every Claude Code session:
| Tool | Description |
|---|---|
add_memory | Save text or conversation history for a user/agent |
search_memories | Semantic search across memories with filters |
get_memories | List memories with filters and pagination |
get_memory | Retrieve a specific memory by ID |
update_memory | Overwrite a memory's text by ID |
delete_memory | Delete a single memory by ID |
delete_all_memories | Bulk delete all memories in scope |
delete_entities | Delete a user/agent/app/run entity and its memories |
list_entities | List users/agents/apps/runs stored in Mem0 |
When installed via the plugin marketplace, Mem0 hooks into Claude Code's lifecycle to automatically manage memory:
On every new session, the plugin prompts Claude to call search_memories to load relevant context from prior sessions. On resumed or post-compaction sessions, it adjusts the prompt accordingly.
Before processing each user message, the plugin searches Mem0 for memories relevant to the current prompt and injects them into context. Short prompts (< 20 characters) are skipped to minimize latency.
Before context compaction, the plugin prompts Claude to store a comprehensive session summary — including goals, accomplishments, decisions, modified files, and current state — so nothing is lost.
After each task completion, the plugin prompts Claude to extract and store key learnings: successful strategies, failed approaches, architectural decisions, and new conventions.
When Claude finishes responding, the plugin prompts for any unstored learnings and captures transcript state via the Mem0 REST API as a background safety net.
# Session 1: Working on a feature
You: Let's refactor the auth module to use JWT tokens instead of sessions.
# Claude searches memories, finds nothing relevant, proceeds with the work.
# After completing the task, Mem0 stores:
# - Decision: "Migrated auth from sessions to JWT tokens"
# - Files modified: auth/middleware.ts, auth/token.ts
# - User preference: "Prefers TypeScript, uses ESLint"
# Session 2 (days later): Related work
You: Add refresh token rotation to the auth system.
# Claude searches memories, retrieves the JWT migration context.
# Knows the file structure, decisions made, and user preferences.
# Continues seamlessly without re-explaining the codebase.
MEM0_API_KEY is set in your shell: echo $MEM0_API_KEY. If empty, add it to your shell profile (see Prerequisites)export MEM0_API_KEY="m0-..." to your ~/.zshrc (or ~/.bashrc) and run source ~/.zshrc