CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
Give Your AI Agent a Memory: Persistent Context Across Sessions With Mem0

Give Your AI Agent a Memory: Persistent Context Across Sessions With Mem0

Chris Harper

3 min read

Aug 12, 2026 · 12:03 UTC

AI
Tutorial
Agents
Best Practices

Mem0 is an open-source memory layer that extracts, deduplicates, and surfaces facts from your agent conversations — add cross-session persistent memory to any Claude agent in 15 minutes.

What you'll be able to do after this:

  • Store and retrieve user-specific facts across any number of agent sessions
  • Integrate Mem0 with a Claude agent using m.add() and m.search() in under 20 lines
  • Self-host Mem0 with a local Qdrant vector store — no external API key required

Every stateless agent faces the same wall: the context window resets each session. Your user told it their preferred stack, their company's coding conventions, and their deadline — three conversations ago. Mem0 fixes this by sitting between your agent and each conversation, extracting discrete facts, deduplicating them against what's already stored, and surfacing the relevant ones at the start of every new session.

Install and first memory

pip install mem0ai

The open-source version uses a local Qdrant vector store and your LLM of choice. Swap in Claude Haiku 4.5 as the extraction LLM for cost-efficient memory operations:

from mem0 import Memory

config = {
    "llm": {
        "provider": "anthropic",
        "config": {
            "model": "claude-haiku-4-5-20251001",
            "api_key": "your-anthropic-key"
        }
    }
}

m = Memory.from_config(config)

# Add a conversation — Mem0 extracts and stores salient facts
result = m.add([
    {"role": "user",      "content": "I work on a Python monorepo with 4 microservices."},
    {"role": "assistant", "content": "Got it, I'll tailor examples to Python."}
], user_id="alice")

print(result)
# [{"event": "ADD", "memory": "Works on a Python monorepo with 4 microservices"}]

Retrieve relevant memories before each call

# In a new session — pull what's relevant before calling Claude
memories = m.search("what tech stack does this user prefer?", user_id="alice")
context = "\n".join(f"- {r['memory']}" for r in memories[:5])
print(context)  # - Works on a Python monorepo with 4 microservices

Wire Mem0 into a Claude agent

import anthropic
from mem0 import Memory

mem = Memory.from_config(config)
claude = anthropic.Anthropic()

def chat(user_message: str, user_id: str) -> str:
    # 1. Pull relevant memories
    memories = mem.search(user_message, user_id=user_id)
    ctx = "\n".join(f"- {r['memory']}" for r in memories[:5])

    # 2. Inject into system prompt
    system = f"Known context about this user:\n{ctx}" if ctx else "You are a helpful assistant."
    response = claude.messages.create(
        model="claude-sonnet-5-20260801",
        max_tokens=1024,
        system=system,
        messages=[{"role": "user", "content": user_message}]
    )
    answer = response.content[0].text

    # 3. Store the exchange so memory grows with each conversation
    mem.add([
        {"role": "user",      "content": user_message},
        {"role": "assistant", "content": answer}
    ], user_id=user_id)
    return answer

Self-host with Docker (Qdrant backend)

No external API keys for storage:

git clone https://github.com/mem0ai/mem0
cd mem0
docker-compose up -d   # Qdrant on :6333, Mem0 REST API on :8000

Then point the SDK at your local server: Memory(host="http://localhost:8000").

How Mem0 V3 keeps memory coherent

Mem0 V3 (April 2026) uses a single-pass extraction pipeline: each new fact from a conversation is checked against existing memories and emits ADD, UPDATE, DELETE, or NONE. After 100 conversations the store stays clean and relevant — not bloated with contradictions or duplicates. Retrieval hits 92.5 on the LoCoMo long-context memory benchmark.

Sources: Mem0 Python Quickstart — docs.mem0.ai · The Easiest Way to Add Persistent Memory — Mem0 blog · mem0ai/mem0 — GitHub