
Give Your Agent a Brain: Persistent Cross-Session Memory with mem0
Chris Harper
3 min read
Jul 27, 2026 · 04:04 UTC
mem0 gives any AI agent persistent, searchable cross-session memory in three lines of code -- add() stores conversation facts, search() retrieves relevant context before the next LLM call.
What you'll be able to do after this:
- Add persistent cross-session memory to any AI agent -- Claude, LangGraph, CrewAI, or a raw API loop -- in an afternoon
- Store structured facts from conversations and retrieve semantically relevant context before each LLM call, so the agent knows who it's talking to without re-reading transcripts
- Run mem0 fully offline with Ollama + Qdrant when you need zero cloud dependency
Resource: mem0 quickstart + GitHub: mem0ai/mem0 -- open-source memory layer with a hosted API and a local OSS mode using any LLM and vector store.
Why agents forget -- and why that's fixable
Your agent's context window resets after every conversation. Without memory, every session starts blank -- no user preferences, no prior context, no continuity. mem0 sits between your agent and the model: during a conversation it extracts structured facts from exchanges and stores them in a vector database. On the next call, it retrieves the most semantically relevant memories and injects them into the system prompt -- so the model answers with accumulated knowledge about the user.
Install
pip install mem0ai
Quickstart (hosted API)
from mem0 import MemoryClient
client = MemoryClient(api_key="m0-...") # API key from app.mem0.ai
# 1. Store a conversation exchange — mem0 extracts and indexes the facts
messages = [
{"role": "user", "content": "I'm building a RAG system on PostgreSQL with pgvector."},
{"role": "assistant", "content": "Got it! I'll keep that in mind."}
]
client.add(messages, user_id="alice")
# 2. Later: retrieve relevant context before the next LLM call
results = client.search("What database stack is this user on?", user_id="alice")
for m in results:
print(m["memory"], round(m["score"], 2))
# "Uses PostgreSQL with pgvector for RAG" 0.94
Wire it into an agent loop
The pattern is one retrieval before the LLM call, one store after:
import anthropic
from mem0 import MemoryClient
mem = MemoryClient(api_key="m0-...")
claude = anthropic.Anthropic()
def chat(user_message: str, user_id: str) -> str:
# 1. Pull relevant past context
memories = mem.search(user_message, user_id=user_id)
context = "
".join(m["memory"] for m in memories[:5])
system = f"Relevant context about this user:
{context}" if context else ""
# 2. Call the model
response = claude.messages.create(
model="claude-sonnet-5-20251022",
max_tokens=1024,
system=system,
messages=[{"role": "user", "content": user_message}],
)
reply = response.content[0].text
# 3. Store the exchange
mem.add([
{"role": "user", "content": user_message},
{"role": "assistant", "content": reply},
], user_id=user_id)
return reply
After five conversations, chat("what was that vector DB issue we looked at?", "alice") will return an answer grounded in prior sessions -- without re-reading any transcripts.
Local / offline mode
To run without cloud: point mem0 at Ollama and Qdrant:
ollama pull llama3.2 nomic-embed-text
docker run -p 6333:6333 qdrant/qdrant
from mem0 import Memory
m = Memory.from_config({
"llm": {"provider": "ollama", "config": {"model": "llama3.2"}},
"embedder": {"provider": "ollama", "config": {"model": "nomic-embed-text"}},
"vector_store": {"provider": "qdrant", "config": {"host": "localhost", "port": 6333}},
})
# add() and search() API is identical -- agent code doesn't change
Sources: mem0 quickstart · mem0 GitHub · Persistent memory for agents · State of AI Agent Memory 2026