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

1
Choose
Architecture
2
Set up
Storage
3
Write
After turn
4
Retrieve
Before LLM
5
Evaluate
Iterate

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

Frameworks hub · Vector databases

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…UseWhy
Weaviate alreadyEngramVector-native memory layer
No vector infra yetMem0 APIManaged write/retrieve in minutes
LangGraph agentLangMemNative checkpointer + store
Long conversationsLettaMemGPT paging built-in
Changing facts over timeZepTemporal knowledge graph
Full controlRedis + pgvectorDIY — you own each step

Best AI memory tools

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.

Storage backends · Embeddings for memory

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.

Writing and storing memories

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.

Memory retrieval · Scoring and ranking

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

LOCOMO benchmark · Evaluation hub

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.

Engram explained →

Mem0

API key → add() after turn → search() before LLM. Fastest managed path.

Mem0 alternatives →

LangMem

LangGraph checkpointer + store nodes. Native for LangGraph agents.

LangMem guide →

Letta

Agent with built-in MemGPT paging. Long conversations out of the box.

Letta alternatives →

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.