# Add Persistent Memory to Claude Code with Mem0 (5-Minute Setup)

Claude Code is a phenomenal piece of technology. But it is affected by the same problem every LLM is affected by, which is a lack of memory.

Every time you start a Claude Code session, you need to re-explain your project architecture, re-state your coding preferences, and re-describe bugs you have already fixed. This repetition wastes time and tokens. This guide walks you through adding a persistent memory layer to Claude Code using the Mem0 plugin, covering both CLI and desktop versions.

Mem0 offers two paths for adding memory to Claude Code:

- **Mem0 Platform MCP**: hosted cloud memory, zero local setup, managed retrieval.
- [**OpenMemory MCP**](/content/blog/introducing-openmemory-mcp/index.html): local-first, self-hosted memory that runs entirely on your machine with no cloud sync or external storage.

## Why Add Memory to Claude Code?

Without memory, Claude Code starts every session with zero context. You spend the first several minutes re-establishing what the project does, what patterns you follow, and what you already tried. With a persistent memory layer, Claude recalls that context automatically from prior sessions.

## How To Implement Memory In Claude Code With Mem0

The Mem0 plugin for Claude Code has three components:

1. **MCP Server**: Connects Claude Code to Mem0's cloud memory layer via the hosted HTTP MCP endpoint.
2. **Lifecycle Hooks**: Automatically captures learnings at key lifecycle points: session start, context compaction, task completion, and session end.
3. **SDK Skill**: Teaches the agent how to integrate the Mem0 SDK into your applications.

You can integrate persistent memory in Claude Code using the official [Mem0 MCP server](https://docs.mem0.ai/platform/mem0-mcp).

### Prerequisites

Before setting up Mem0, you need:

1. A Mem0 Platform account and API key:
   - Sign up at [**app.mem0.ai**](https://app.mem0.ai/get-api-key?utm_source=blog&utm_medium=get_key&utm_content=claude-code-memory).
   - Get your API key (starts with `m0-`).
2. Claude Code CLI or the Claude Cowork desktop app is installed.
3. Your API key is exported in your shell:
   
   ```
   export MEM0_API_KEY="m0-your-api-key"
   ```

The free tier includes 10,000 memories and 1,000 retrieval calls per month.

## Installation

### Option A: Plugin Marketplace (Recommended)

Install the plugin:

```bash
/plugin marketplace add mem0ai/mem0
/plugin install mem0@mem0-plugins
```

**Claude Cowork desktop app:** Open the Cowork tab, click **Customize** in the sidebar, click **Browse plugins**, and install Mem0.

After installation, restart your Claude Code session.

### Option B: MCP Only

If you only want the MCP tools:

```bash
npx mcp-add \
  --name mem0-mcp \
  --type http \
  --url "https://mcp.mem0.ai/mcp" \
  --clients "claude code"
```

### **Quick overview**

| Component | Plugin Install (Option A) | MCP Only (Options B/C) |
| --- | --- | --- |
| MCP Server (9 memory tools) | Yes | Yes |
| Lifecycle Hooks | Yes | No |
| Mem0 SDK Skill | Yes | No |
| Zero local dependencies | Yes | Yes |

## Mem0 MCP Tools Available After Setup

| 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 all its memories |
| `list_entities` | List users/agents/apps/runs stored in Mem0 |

## **Using the Async SDK in Your Code**

If you are building applications that use the Mem0 SDK directly:

```python
import asyncio
from mem0 import AsyncMemoryClient

client = AsyncMemoryClient(api_key="m0-your-api-key")

async def main():
    await client.add(
        messages=[{"role": "user", "content": "I prefer dark mode"}],
        user_id="user123"
    )
    results = await client.search(query="preferences", user_id="user123")
    print(results)

asyncio.run(main())
```

## **What Memory Looks Like in Practice**

### **Without Memory**: Debugging Authentication

**Session 1:** You explain that the auth system uses NextAuth with Google and email providers, that tokens expire after 24 hours, and that the refresh logic lives in `/lib/auth/refresh.ts`.

**Session 2:** You re-explain the entire auth setup.

### **With Memory**: Debugging Authentication

**Session 1:** The lifecycle hook prompts Claude to store key findings.

**Session 2:** When you ask, "Let's continue on the auth logic fix," Claude draws on stored context and asks directly: "Is this related to the token refresh edge case where refresh fails during active requests?"

## Mem0 as the Memory Layer

Mem0 uses a hybrid architecture: vector stores for semantic search, key-value stores for fast retrieval, and optional graph stores for relationship modeling.

For compliance requirements, Mem0 is SOC 2 Type II certified, GDPR compliant, and offers HIPAA compliance on Enterprise plans.

## Frequently Asked Questions

**Q: Why does Claude Code need persistent memory?**

Claude Code starts every session with zero context. A persistent memory layer eliminates the repetitive context-building phase.

**Q: How do I add memory to Claude Code?**

The recommended approach is the plugin marketplace.

**Q: Is Mem0 free to use?**

The free tier includes 10,000 memories and 1,000 retrieval calls per month.

**Q: Can I control what Claude remembers?**

Yes. Use natural language or MCP tools.
