Guides · Flagship
How to Add Memory to an AI Agent
Add memory to an AI agent in five steps: choose a memory store, pick a framework, implement write on interaction, retrieve before each LLM call, and evaluate with LOCOMO or LongMemEval.
Implementation loop
Before you start
Prerequisites
You need an LLM API, an agent framework, a vector store or memory API, and an embedding model.
- LLM API — OpenAI, Anthropic, or any compatible endpoint
- Agent framework — LangGraph, CrewAI, n8n, OpenAI Agents SDK, or custom loop
- Memory store — Engram, Mem0 API, Zep, pgvector, Pinecone, or Redis
- Embedding model — text-embedding-3-small (1536 dims) or equivalent
Walkthrough
Five steps to add agent memory
Step 1
Choose your memory architecture
Match framework to your existing infra before writing code.
| If you have… | Use | Why |
|---|---|---|
| Weaviate already | Engram | Vector-native memory layer |
| No vector infra yet | Mem0 API | Managed write/retrieve in minutes |
| LangGraph agent | LangMem | Native checkpointer + store |
| Long conversations | Letta | MemGPT paging built-in |
| Changing facts over time | Zep | Temporal knowledge graph |
| Full control | Redis + pgvector | DIY — you own each step |
Step 2
Set up storage
Provision a vector DB or sign up for a managed memory API.
Mem0: create account → API key → note user_id scoping.
DIY: create pgvector index with embedding dimensions matching your model (1536 for text-embedding-3-small). Add metadata columns: user_id, created_at, source.
Step 3
Implement memory writes
Hook after each turn or tool result — extract salient facts and write to the store.
# Mem0 pattern
memory.add(messages=conversation_turn, user_id="user_123")
# DIY pattern
fact = extract_facts(llm, conversation_turn) # "User prefers dark mode"
embedding = embed(fact)
vector_store.upsert(id=uuid4(), vector=embedding, metadata={"user_id": "user_123"})
Run writes asynchronously after the user sees the response — don’t block the reply on storage latency.
Step 4
Implement memory retrieval
Before each LLM call, search top-k memories and inject into the system prompt.
# Mem0 pattern
memories = memory.search(query=current_user_message, user_id="user_123", limit=8)
system_prompt += format_memories(memories)
# DIY pattern
query_embedding = embed(current_user_message)
results = vector_store.search(query_embedding, filter={"user_id": "user_123"}, top_k=8)
system_prompt += format_memories(results)
Tune top_k (typically 5–10). Add recency boost for time-sensitive facts. Format as bullets under ## User memories in the system prompt.
Step 5
Evaluate and iterate
Run LOCOMO or LongMemEval subsets on your domain queries; tune extraction and top-k.
- Recall hit rate — does retrieval surface the right memory?
- Latency p95 — write + retrieve under your SLA
- Token cost — memories injected per turn
- Conflict cases — user corrects a prior fact
Quick paths
Framework-specific shortcuts
Engram
Send raw text or conversations to the Engram API; its async pipeline extracts, deduplicates and commits memories on Weaviate. Best if Weaviate is already your vector layer.
Same pattern works for CrewAI, n8n and OpenAI Agents SDK — wire API calls at turn boundaries.
FAQ
Frequently asked questions
What is the easiest way to add memory to an AI agent?
Mem0 Cloud API — sign up, call add() after each turn and search() before each LLM call. No vector infra to manage. See step-by-step above and Mem0 alternatives.
How do I add memory with Mem0?
Install the SDK, set your API key, call memory.add(messages, user_id) after each turn and memory.search(query, user_id) before the LLM call. See Mem0 alternatives for setup details.
How do I add memory to a LangGraph agent?
Use LangMem for native checkpointer + store integration, or Engram/Mem0/Zep as external APIs at graph node boundaries. See LangMem.
How do I add memory to an n8n AI agent?
Add HTTP Request nodes calling Engram, Mem0 or Zep APIs — write after the agent responds, retrieve before the next LLM node. Same five-step pattern as any framework.
How do I add memory to CrewAI?
Wire Engram, Mem0 or Zep SDK calls in task callbacks — extract facts after each agent turn, inject retrieved memories into the next task prompt. See writing memories.
How do I add memory to OpenAI Agents SDK?
Use Mem0 or a vector store as external tools, or implement write/retrieve in your agent loop before each responses.create() call. See memory as a tool.
Can I add memory without a framework?
Yes — DIY with Redis or pgvector: embed facts after each turn, search before each LLM call. You implement extraction, ranking and eviction yourself. See storage backends.
How much does agent memory cost?
Costs: embedding API calls, vector storage, and retrieval latency. Managed APIs (Mem0) bundle these; DIY you pay per embedding + DB hosting. Reduce tokens by selective retrieval vs resending full history.
Is agent memory secure?
Scope memories per user_id, encrypt at rest, add TTLs for sensitive data and audit retrieval logs. Never store secrets in plaintext memory. See memory security.
When is agent memory production-ready?
When recall passes LOCOMO/LongMemEval on your domain, latency is under SLA, and you have eviction + conflict handling. See evaluation hub and step 5 above.