← labs | 02 | mem0
lab 02 | ~3 min

Add. Search. Three lines.

Mem0 is the drop-in memory layer. It wraps vector + graph + reranker behind one managed API. You add a fact, search for it later, and the layer handles dedup, conflict, and rerank for you. When this finishes you have persistent user-scoped memory in a Python REPL with no infra.

step 1

Install the package.

One pip install. The package on PyPI is mem0ai (the import path is mem0).

install
pip install mem0ai
step 2

Set the OpenAI key.

Mem0 calls an LLM to extract facts from raw text on add(). Default backend is OpenAI. Set the env var for your shell:

bash | zsh
export OPENAI_API_KEY="sk-..."
powershell
$env:OPENAI_API_KEY = "sk-..."
cmd.exe
set OPENAI_API_KEY=sk-...
step 3

Add, search.

Drop this into mem0_demo.py and run it. Three load-bearing lines. The rest is print.

mem0_demo.py
import sys
sys.stdout.reconfigure(encoding="utf-8")

from mem0 import Memory

m = Memory()
m.add("Rayyan prefers pnpm over npm", user_id="ray")

results = m.search("what package manager?", user_id="ray")
print(results)

Run it:

terminal
python mem0_demo.py
expected output
{'results': [{'id': '0c8f...',
              'memory': 'Prefers pnpm over npm',
              'score': 0.42,
              'metadata': None,
              'created_at': '2026-05-11T19:18:33.481Z',
              'user_id': 'ray'}]}

what just happened

Mem0 turned "Rayyan prefers pnpm over npm" into an extracted fact ("Prefers pnpm over npm"), embedded it, and stored it scoped to user_id="ray". The search() call embedded your query, ran nearest-neighbor + rerank, returned the hit. The score is a relevance number, not a confidence.

The library auto-creates a local store on first use. By default that lives in ~/.mem0/ with a SQLite + Qdrant-lite backend. For prod you point it at Pinecone, Qdrant, pgvector, or the Mem0 cloud.

going further | forget + multi-fact

Add a second fact, search, forget, search again:

extend the demo
m.add("Rayyan ships on Vercel", user_id="ray")
m.add("Rayyan codes from a Windows machine", user_id="ray")

all_mems = m.get_all(user_id="ray")
print(f"have {len(all_mems['results'])} memories for ray")

# forget by id
first_id = all_mems['results'][0]['id']
m.delete(memory_id=first_id)
print("deleted", first_id)

troubleshooting

ImportError on `from mem0 import Memory`. You probably ran pip install mem0 (wrong package). Uninstall it and install mem0ai instead.

openai.AuthenticationError. Env var didn't make it into the process. Quick check: python -c "import os; print(bool(os.getenv('OPENAI_API_KEY')))". Should print True.

First call hangs. Mem0 lazy-downloads the embedding model on first use. Ten-second pause on a fresh machine is normal. After that it's local + fast.