Migrating from Mem0 to Brain
The Problem: Why Migrate from Mem0?
Mem0 is a memory management layer for agents, but it has constraints:
- Semantic-only retrieval: Mem0 retrieves by semantic similarity. If you need "all memories from the last 24 hours" or "find memory type = 'decision'", you need custom post-retrieval filtering.
- Network latency: Mem0 Cloud adds 200–500ms per operation. For real-time agents, that's slow.
- Limited schema control: Memory is largely unstructured text. Structured metadata filtering is an afterthought.
- Single retrieval mode: One vector search. No hybrid (keyword + semantic), no temporal windows, no type filtering.
- Vendor lock-in: Mem0's API is proprietary; exporting history requires custom tooling.
Brain fixes these:
| Feature | Mem0 | Brain |
|---|---|---|
| Retrieval modes | Semantic only | Hybrid, temporal, lexical, type-filtered, graph |
| Latency (Cloud) | 200–500ms | 50–150ms |
| Metadata filtering | Limited | Full structured support |
| Schema control | Unstructured text + tags | Typed, extensible |
| Export/import | Difficult | Standard APIs |
| Open source | No | Yes |
This guide maps Mem0 APIs to Brain and provides a migration script.
API Mapping: Mem0 → Brain
| Mem0 API | Purpose | Brain Equivalent | Notes |
|---|---|---|---|
memory.add(text, metadata) | Store a memory | brain.store({ agentId, content, metadata }) | Content is the same; metadata structure changes slightly |
memory.get(query) | Retrieve memories | brain.retrieve({ agentId, query, topK }) | Brain defaults to hybrid retrieval; customize with mode param |
memory.search(query, filters) | Search with filters | brain.retrieve({ agentId, query, filters }) | Filters are fully supported in Brain |
memory.update(id, text) | Update memory | brain.update({ agentId, memoryId, content }) | Brain supports versioning |
memory.delete(id) | Delete memory | brain.delete({ agentId, memoryId }) | Same |
memory.list() | List all | brain.list({ agentId }) | Paginated in Brain |
Step 1: Export Mem0 Data
First, extract all memories from Mem0. If you're using Mem0 Cloud, use their export endpoint or iterate over memories:
import requests
from mem0 import Memory
# Initialize Mem0
mem0 = Memory.from_config({
"llm": {"provider": "openai", "config": {"model": "gpt-4"}},
"embedder": {"provider": "openai"},
"vector_store": {"provider": "qdrant"},
})
# Export all memories
all_memories = []
agent_id = "your_agent_id"
# Mem0 doesn't have a bulk export, so iterate
memories = mem0.search("*", limit=10000) # High limit to get all
for memory in memories:
all_memories.append({
"id": memory.get("id"),
"content": memory.get("content"),
"metadata": memory.get("metadata", {}),
})
# Save to JSON
import json
with open("mem0_export.json", "w") as f:
json.dump(all_memories, f, indent=2)
print(f"Exported {len(all_memories)} memories")Step 2: Transform Mem0 Schema to Brain Schema
Mem0 memories are typically:
{
"id": "mem_xyz",
"content": "User prefers late-night meetings after 10pm",
"metadata": {
"agent_id": "agent_1",
"tags": ["user_preference", "scheduling"]
}
}Transform to Brain schema:
{
"content": "User prefers late-night meetings after 10pm",
"agentId": "agent_1",
"metadata": {
"type": "user_preference",
"tags": ["scheduling"],
"source": "mem0_migration",
"imported_at": "2026-05-13T10:00:00Z"
}
}Step 3: Run Migration Script
This Python script exports from Mem0 and imports to Brain Cloud:
import json
import requests
from datetime import datetime
# Mem0 setup (export phase)
from mem0 import Memory
mem0 = Memory.from_config({
"llm": {"provider": "openai", "config": {"model": "gpt-4"}},
"embedder": {"provider": "openai"},
"vector_store": {"provider": "qdrant"},
})
# Brain Cloud setup (import phase)
BRAIN_API_KEY = "your_brain_api_key"
BRAIN_BASE_URL = "https://api.brain.cloud"
def export_mem0(agent_id: str):
"""Export all memories from Mem0"""
memories = mem0.search("*", limit=10000)
transformed = []
for mem in memories:
transformed.append({
"content": mem.get("content", ""),
"agentId": agent_id,
"metadata": {
"type": mem.get("metadata", {}).get("tags", ["general"])[0],
"tags": mem.get("metadata", {}).get("tags", []),
"source": "mem0_migration",
"imported_at": datetime.now().isoformat(),
}
})
return transformed
def import_brain(memories: list):
"""Import memories to Brain Cloud"""
headers = {
"Authorization": f"Bearer {BRAIN_API_KEY}",
"Content-Type": "application/json"
}
imported_count = 0
failed_count = 0
for memory in memories:
try:
response = requests.post(
f"{BRAIN_BASE_URL}/v1/memories",
json=memory,
headers=headers,
)
response.raise_for_status()
imported_count += 1
except Exception as e:
print(f"Failed to import memory: {memory['content'][:50]}... Error: {e}")
failed_count += 1
return imported_count, failed_count
# Run migration
agent_id = "agent_migration_123"
print(f"Exporting from Mem0 for agent: {agent_id}")
memories = export_mem0(agent_id)
print(f"Importing {len(memories)} memories to Brain Cloud...")
imported, failed = import_brain(memories)
print(f"Migration complete: {imported} imported, {failed} failed")Step 4: Update Your Code
Before:
from mem0 import Memory
mem0 = Memory.from_config(config)
# Store
mem0.add("User likes coffee", {"agent_id": "agent_1", "tags": ["preference"]})
# Retrieve
results = mem0.search("What does the user like?")After:
from brain_sdk import Brain
brain = Brain(api_key="your_api_key")
# Store
brain.store(
agentId="agent_1",
content="User likes coffee",
metadata={"type": "preference", "tags": ["preference"]}
)
# Retrieve
results = brain.retrieve(
agentId="agent_1",
query="What does the user like?",
topK=5,
mode="hybrid" # Hybrid (keyword + semantic) for better results
)Cost Comparison: Mem0 vs Brain
Assume: 100 agents, 5K memories each = 500K total memories, 1K retrievals/day
| Service | Cost per Month | Notes |
|---|---|---|
| Mem0 Cloud | 2000 | Pay per memory stored + retrieved; pricing opaque |
| Brain Cloud | 800 | 0.0005 per retrieval |
| Brain Open Source | $0 | Self-host; only pay for infra (compute, storage) |
Savings by migrating: 60–75% cost reduction on memory operations.
Migration Checklist
- Export all memories from Mem0 (use script above)
- Review exported schema; map Mem0 metadata to Brain metadata structure
- Transform memories to Brain schema (update field names, add type/source)
- Set up Brain Cloud account (or self-host open-source version)
- Run migration script to import memories to Brain
- Verify import: sample 10 random memories, retrieve them, confirm content matches
- Update agent code: replace
mem0.add()withbrain.store(),mem0.search()withbrain.retrieve() - Test retrieval quality: run your retrieval queries against Brain, compare results to Mem0
- Backfill any missing edge cases (e.g., memory types Mem0 had but Brain schema doesn't capture)
- Decommission Mem0 connection after validation
Why Brain Wins
Hybrid retrieval: Brain combines keyword search (BM25) with semantic search. For memory "User prefers late-night meetings", a keyword query for "late-night" retrieves directly; Mem0 requires semantic similarity to "when should meetings be?".
Structured metadata: Brain's metadata is fully queryable. Filter by type="preference" or created_after="2026-05-01". Mem0 requires post-retrieval filtering in application code.
Lower latency: Brain Cloud is architected for <\100ms P99 latency. Mem0's network hops are slower.
Open source control: Brain's open-source version runs anywhere (local, Docker, K8s). Mem0 requires their cloud.
Next Steps
- Learn more: See Migrating from Pinecone for vector DB migration patterns
- Cost comparison: Check Cost Optimization for Agent Memory
- Build: Read Schema Design Patterns to design Brain schemas that match your agent's needs