---
title: "How to Implement Long-Term Memory for AI Agents (2026)"
url: "https://atlan.com/know/how-to-implement-long-term-memory-ai-agents/"
description: "Step-by-step Python guide to implementing long-term memory for AI agents using Mem0, Zep, and LangGraph, with working code and honest benchmark comparisons."
author: "Emily Winks"
author_role: "Data Governance Expert"
published: "2026-04-17T00:00:00.000Z"
updated: "2026-09-21T00:00:00.000Z"
---

## How do you implement long-term memory for AI agents?

Choose a persistence framework (Mem0, Zep, or LangGraph Store), install it in Python, and wire three operations into every agent turn: retrieve relevant memories before reasoning, run the LLM call, then store the new exchange. A basic Mem0 integration runs in under 30 minutes.

Steps at a glance:

- **Step 1** - Choose your memory framework (Mem0 for speed/breadth, Zep for temporal queries, LangGraph for LangChain-native teams)
- **Step 2** - Install and configure your chosen framework with the right vector backend
- **Step 3** - Add memories after each agent turn using the store pattern
- **Step 4** - Retrieve memories before each LLM call and inject into the system prompt
- **Step 5** - Scope every memory operation to an authenticated user_id
- **Step 6** - Move to production with async writes and a persistent backend
- **Step 7** - Monitor for stale memories and add a governance layer for enterprise data context

## Key takeaways

- Mem0 reports 0.200s p95 search latency and 1.44s p95 total, against full-context at 17.12s.
- Every turn needs three operations: retrieve memories before reasoning, make the LLM call, then store the new exchange.
- LangMem has 59.82s p95 search latency. Never use it for synchronous retrieval; run it async only.

To implement long-term memory for AI agents, choose a persistence framework (Mem0, Zep, or LangGraph Store), install it in Python, and wire three operations into every agent turn: retrieve relevant memories before reasoning, run the LLM call, then store the new exchange. A basic Mem0 integration runs in under 30 minutes and delivers 0.200s p95 retrieval latency - 91% lower overhead than full-context approaches. This guide covers all three frameworks side-by-side with working code, production configuration, and an honest comparison of where each breaks down.

**Quick overview:**

| Framework | p95 Retrieval Latency | Recall Accuracy | Self-Hosted | Best For |
|---|---|---|---|---|
| Mem0 | 0.200s search / 1.44s total | 66.9% (LOCOMO) | Yes (Qdrant/ChromaDB) | Fast user preference recall; widest integrations |
| Zep | 0.778s search / 2.93s total | 66.0% (LOCOMO) | Cloud only | Temporal/relational queries |
| LangGraph + LangMem | 59.82s search (LangMem) | 58.1% (LOCOMO) | Yes (PostgresStore) | LangChain-native teams; procedural memory |

---

## Why implement long-term memory?

Every LLM API call is isolated. Even with a 1M-token context window, accuracy degradation begins around 1,000 tokens of injected context - far below advertised limits. Agents built without external memory cannot recall a user's name, preferences, or prior decisions after a session ends. According to a 2025 AI agent memory survey, [32% of enterprise teams cite output quality as their top barrier to production deployment](https://atlan.com/know/why-ai-agents-forget/), tracing directly to this statelessness. For a full explanation of the problem, see [Long-Term vs Short-Term AI Memory](https://atlan.com/know/long-term-vs-short-term-ai-memory/).

With a memory layer in place, agents can recall past preferences, skip re-asking known facts, and build compounding context over months. In Mem0's own benchmark, Mem0 scores 66.9% on LOCOMO with a 0.200s p95 search latency and a 1.44s p95 end-to-end latency. Full-context scores higher, 72.9%, but takes 17.12s p95 end to end, which rules it out for any agent with a sub-second SLA. Read the two latency numbers as the pair they are: search alone, and the whole turn.[1]

This guide targets Python developers and AI engineers building conversational agents, data assistants, or any agent that serves the same user across multiple sessions. A solo developer can reach a working prototype in an afternoon. For the concept-level companion to this code-first guide, see [Memory Layer for AI Agents](https://atlan.com/know/memory-layer-for-ai-agents/) and [In-Context vs External Memory](https://atlan.com/know/in-context-vs-external-memory-ai-agents/).

---

## Prerequisites

Before writing any code, confirm the following:

**Organizational:**
- [ ] Clarity on use case: personal preference recall, cross-session continuity, or temporal/relational queries
- [ ] A user_id strategy: how will you identify users across sessions? (UUID, email hash, or existing auth token)
- [ ] Decision on data residency: can user memory data leave your infrastructure, or do you need self-hosted storage?

**Technical:**
- [ ] Python 3.9+ (3.11 recommended for async performance)
- [ ] OpenAI or Anthropic API key (both Mem0 and LangMem use an LLM for memory extraction)
- [ ] For Mem0 self-hosted: Qdrant or ChromaDB running locally (Docker preferred)
- [ ] For LangGraph production: PostgreSQL instance

**Time:**
- Basic implementation: 2-4 hours
- Production-ready with persistence, multi-user scoping, and monitoring: 1-2 days

**Difficulty:** Intermediate

---

## Step 1: choose your memory framework

**What you'll accomplish:** Compare Mem0, Zep, LangMem, and LangGraph Store across five dimensions so you can commit to one framework before writing any code.

**Time:** 30 minutes

| Framework | GitHub Stars | p95 Retrieval Latency | Recall Accuracy (LOCOMO) | Self-Hosted | Best For |
|---|---|---|---|---|---|
| Mem0 | 65.6k (`mem0ai/mem0`) | **0.200s** | 66.9% vector / 68.4% graph | Yes (Qdrant/ChromaDB) | Fast user preference recall; widest framework integrations |
| Zep | 31.0k (`getzep/graphiti`) | 0.778s | 66.0% | Cloud only | Temporal/relational queries; CRM-style "who owned X last quarter?" |
| LangMem | - | **59.82s** | 58.1% | Yes (any LangGraph Store) | Procedural memory (prompt self-improvement); LangChain-native teams |
| LangGraph Store | - | Depends on backend | - | Yes (InMemoryStore -> PostgresStore) | Teams already in LangGraph ecosystem; storage-agnostic flexibility |

Every latency and accuracy figure in this table comes from one run: Mem0's LOCOMO benchmark in [arXiv:2504.19413](https://arxiv.org/abs/2504.19413). Mem0 measured its own system and its rivals' in that run, so treat the cross-vendor ordering as one vendor's result, not a settled ranking. Star counts are from the GitHub API, September 2026.

For a full framework comparison with scoring, see [Best AI Agent Memory Frameworks 2026](https://atlan.com/know/best-ai-agent-memory-frameworks-2026/). For a focused Mem0 vs Zep comparison, see [Zep vs Mem0](https://atlan.com/know/zep-vs-mem0/).

### Decision guidance

**Pick Mem0** if you need something running today, want the largest community and ecosystem (65,589 stars as of September 2026, 20 vector store backends and 21 integrated frameworks), and your use case is user preference recall or conversation continuity. The managed cloud API path requires 5 lines of code.

**Pick Zep** if your agent needs to reason about how facts changed over time - for example, "Alice was the budget owner in Q4, Bob took over in February." Zep's Graphiti engine models `valid_at`/`invalid_at` timestamps, so a superseded fact is marked stale rather than deleted. In Mem0's own LOCOMO run, Zep scored 65.99% to Mem0's 66.88% at a higher latency; that is a rival's measurement of Zep, so read it as one data point rather than a verdict.[1]

**Pick LangGraph Store + LangMem** if your team is already running LangGraph in production and you need procedural memory - an agent that improves its own system prompt over time. Accept the 59.82s extraction latency; it runs asynchronously, not on the hot path.

**Never use LangMem for real-time search.** The 59.82s p95 latency[3] makes it categorically unusable as a synchronous retrieval step in any agent.

**Validation checklist:**
- [ ] Use case maps to one framework's strengths
- [ ] Data residency requirement checked (cloud vs. self-hosted)
- [ ] user_id strategy decided

---

## Step 2: install and configure your chosen framework

**What you'll accomplish:** Get your chosen framework installed, environment variables set, and a smoke-test connection working.

**Time:** 20 minutes

### Mem0, self-hosted with Qdrant (recommended for data residency)

```python
pip install mem0ai
# Start Qdrant: docker run -p 6333:6333 qdrant/qdrant
```

Configure Mem0 to point to your local Qdrant instance:

```python
from mem0 import Memory
import os

config = {
    "llm": {
        "provider": "openai",
        "config": {
            "model": "gpt-4.1-nano-2025-04-14",
            "api_key": os.getenv("OPENAI_API_KEY")
        }
    },
    "embedder": {
        "provider": "openai",
        "config": {"model": "text-embedding-3-small"}
    },
    "vector_store": {
        "provider": "qdrant",
        "config": {"url": "localhost", "port": 6333}
    }
}

memory = Memory.from_config(config)
```

### Mem0, cloud managed API (fastest start)

```python
pip install mem0ai
# Set OPENAI_API_KEY + MEM0_API_KEY

from mem0 import MemoryClient
client = MemoryClient(api_key=os.getenv("MEM0_API_KEY"))
# No vector DB setup needed
```

### Zep

```python
pip install zep-cloud
# Set ZEP_API_KEY

from zep_cloud.client import Zep
client = Zep(api_key=os.getenv("ZEP_API_KEY"))

# One-time setup per user
client.user.add(user_id="user_jane", email="jane@example.com",
                first_name="Jane", last_name="Smith")
```

### LangGraph InMemoryStore (dev) -> PostgresStore (prod)

For the deep LangGraph implementation reference, see [Long-Term Memory LangChain Agents](https://atlan.com/know/long-term-memory-langchain-agents/).

```python
pip install langgraph langmem

from langgraph.store.memory import InMemoryStore        # dev only
from langgraph.store.postgres import PostgresStore      # production
# Note: agent creation with memory tools is shown in Step 3

# DEV
store = InMemoryStore(
    index={"embed": "openai:text-embedding-3-small", "dims": 1536}
)

# PROD: swap to PostgresStore (identical interface - no agent code changes required)
# DB_URI = "postgresql://user:password@localhost:5432/agentdb"
# with PostgresStore.from_conn_string(DB_URI) as store:
#     store.setup()
```

**Validation checklist:**
- [ ] `OPENAI_API_KEY` (or `ANTHROPIC_API_KEY`) set in environment
- [ ] Framework-specific API key set (`MEM0_API_KEY` for cloud Mem0, `ZEP_API_KEY` for Zep)
- [ ] Qdrant running and reachable at localhost:6333 (if self-hosted Mem0)
- [ ] PostgreSQL accessible (if LangGraph production path)
- [ ] Import works: `from mem0 import Memory` (or equivalent) without error

---

## Step 3: add memories to your agent

**What you'll accomplish:** Wire memory storage into your agent so every conversation turn is captured.

**Time:** 30 minutes

Memory should be stored **after** getting the LLM response, not before. Pass both the user message and assistant response so the extraction LLM has full context to decide what to store. For background on what [types of AI agent memory](https://atlan.com/know/types-of-ai-agent-memory/) are being created here, see the linked reference.

### Mem0 add pattern

```python
from mem0 import Memory
from openai import OpenAI

memory = Memory.from_config(config)  # config from Step 2
openai_client = OpenAI()
USER_ID = "user_alice"

def chat_with_memory(user_input: str) -> str:
    # 1. Retrieve relevant memories (Step 4 covers this in detail)
    relevant = memory.search(user_input, user_id=USER_ID, limit=3)
    memory_context = ""
    if relevant:
        memory_context = "Known about this user:\n"
        for m in relevant:
            memory_context += f"- {m['memory']}\n"

    # 2. Build messages with memory injected
    messages = [
        {"role": "system", "content": f"You are a helpful assistant. {memory_context}"},
        {"role": "user", "content": user_input}
    ]

    # 3. Get response
    response = openai_client.chat.completions.create(
        model="gpt-4.1-nano-2025-04-14",
        messages=messages
    )
    answer = response.choices[0].message.content

    # 4. Store new memory from this exchange
    memory.add([
        {"role": "user", "content": user_input},
        {"role": "assistant", "content": answer}
    ], user_id=USER_ID)

    return answer
```

Mem0's extraction LLM reads these messages and decides whether to ADD, UPDATE, DELETE, or NOOP existing memories. Deduplication is automatic.

### Zep add pattern

```python
import uuid
from zep_cloud.types import Message
from datetime import datetime, timezone

# Call this at the start of each new session to get a fresh thread_id
def new_conversation() -> str:
    thread_id = uuid.uuid4().hex
    client.thread.create(thread_id=thread_id, user_id="user_jane")
    return thread_id

def chat_with_zep(thread_id: str, user_input: str) -> str:
    # 1. Get assembled context from Zep's knowledge graph
    context = client.thread.get_user_context(thread_id=thread_id)
    context_block = getattr(context, "context", "")

    # 2. Respond with context
    messages = [
        {"role": "system", "content": f"You are a helpful assistant.\n\n{context_block}"},
        {"role": "user", "content": user_input}
    ]
    response = openai_client.chat.completions.create(
        model="gpt-4.1", messages=messages
    )
    answer = response.choices[0].message.content

    # 3. Add both turns to Zep graph (processed asynchronously)
    ts = datetime.now(timezone.utc).isoformat()
    client.thread.add_messages(thread_id, messages=[
        Message(created_at=ts, name="Jane", role="user", content=user_input),
        Message(created_at=ts, name="Assistant", role="assistant", content=answer)
    ])
    return answer

# Usage
thread_id = new_conversation()
answer = chat_with_zep(thread_id, "What datasets have SLA issues this morning?")
```

Note: Zep processes messages asynchronously into its temporal knowledge graph. Retrieval may not reflect new messages immediately - background graph processing has latency. This is a known production trade-off.

### LangGraph store.put() pattern

```python
from langmem import create_manage_memory_tool, create_search_memory_tool
from langgraph.prebuilt import create_react_agent

USER_ID = "alice"

agent = create_react_agent(
    "openai:gpt-4.1",
    tools=[
        create_manage_memory_tool(namespace=("memories", USER_ID)),
        create_search_memory_tool(namespace=("memories", USER_ID)),
    ],
    store=store,  # store from Step 2
)

# The agent decides autonomously when to store/retrieve memories
response = agent.invoke({
    "messages": [{"role": "user", "content": "Remember: I prefer dark mode."}]
})
```

**Common mistakes:**

- **Over-storing:** Passing entire long conversations every turn bloats the vector store and degrades retrieval precision. Pass only the current turn, not the full history.
- **Under-storing:** Calling `memory.add()` only on "important" turns - the extraction LLM determines importance; let it run on every turn.
- **Missing user_id:** Forgetting to pass `user_id` stores memory in a default namespace, making it visible to all users.

---

## Step 4: retrieve memories before reasoning

**What you'll accomplish:** Inject relevant past memories into the system prompt before every LLM call so the agent can reason with accumulated context.

**Time:** 20 minutes

### Semantic search pattern (Mem0)

```python
relevant = memory.search(user_input, user_id=USER_ID, limit=5)
memory_context = "\n".join(f"- {m['memory']}" for m in relevant)
system_prompt = f"You are a helpful assistant.\n\nKnown about this user:\n{memory_context}"
```

`limit=5` keeps the injected context tight. Increasing the limit improves recall but adds token cost and risks injecting irrelevant memories.

### Zep context retrieval

```python
context = client.thread.get_user_context(thread_id=thread_id)
context_block = context.context  # pre-assembled by Zep's graph engine
```

Zep returns a pre-assembled context block - no manual formatting needed. The graph engine ranks facts by relevance and recency.

### LangGraph: agent-driven retrieval

With `create_search_memory_tool` attached, the LangGraph agent decides when to search. For explicit search: `store.search(namespace, query="language preferences")`.

### Context budget management

- Aim for retrieved memories under 300 tokens of the system prompt budget
- Use `limit=3-5` for real-time agents; increase only if accuracy tests show recall gaps
- Test that memories injected don't push total context past the point of LLM accuracy degradation - research shows degradation begins around 1,000 tokens of injected context

For the deeper architectural analysis of why [in-context vs external memory](https://atlan.com/know/in-context-vs-external-memory-ai-agents/) trade-offs matter here, see the linked guide.

**Validation:** Start a fresh Python session, add a known memory, end the session, start a new session, and confirm the agent recalls the fact without it being in the conversation history.

---

## Step 5: handle multi-user and namespace scoping

**What you'll accomplish:** Ensure memories for user A are never surfaced to user B - a critical production requirement.

**Time:** 30 minutes

### user_id discipline

Every `memory.add()` and `memory.search()` call must include the `user_id` of the authenticated session. The `user_id` should come from your auth layer, never from a request parameter the client can forge.

### Namespace patterns

- **Mem0:** `user_id` parameter handles isolation natively
- **LangGraph Store:** Namespace tuple enforces scoping - supports org/team/user hierarchy
- **Zep:** a thread is created under a `user_id`, and every message on that thread ingests into that user's graph, so pick the identifier before the first turn

```python
# LangGraph org-level scoping
namespace_shared = ("org", org_id, "shared_context")    # shared across org
namespace_private = ("user", user_id, "preferences")    # user-private

store.put(namespace_private, "memory-key", {"data": "value"})
store.get(namespace_private, "memory-key")
```

### Production concern: memory leakage

The most common isolation failure: a shared `Memory()` instance where `user_id` is accidentally omitted or hardcoded. In a multi-tenant API, this exposes one user's memories to all users.

**Validation:**
- [ ] Create two test user_ids: `user_alice` and `user_bob`
- [ ] Add a memory under `user_alice`
- [ ] Search under `user_bob` - result must be empty
- [ ] Confirm no cross-contamination in the vector store

---

## Step 6: move to production (persistence and async)

**What you'll accomplish:** Replace dev-only in-memory stores with persistent backends, enable async writes so memory storage doesn't block agent responses, and configure custom extraction prompts for domain-specific memory quality.

**Time:** 1-2 hours

### Mem0: cloud vs. self-hosted

**Cloud Mem0 (`MemoryClient`):** Zero infrastructure. Preferred for teams without data residency requirements. Write latency is the cloud round-trip.

**Self-hosted with Qdrant:** Deploy Qdrant via Docker, configure Mem0 with `Memory.from_config(config)` pointing to `localhost:6333`. Required for EU data residency, HIPAA, or any use case where user memory cannot leave your infrastructure.

### LangGraph: InMemoryStore -> PostgresStore upgrade

```python
# DEV
store = InMemoryStore(index={"embed": "openai:text-embedding-3-small", "dims": 1536})

# PROD: swap in with identical interface - no agent code changes required
DB_URI = "postgresql://user:password@localhost:5432/agentdb"
with PostgresStore.from_conn_string(DB_URI) as store:
    store.setup()
```

### Async write pattern

Mem0 supports `async_mode=True`. Memory writes happen in a background thread so they do not add latency to the agent's response time. This is critical for any agent with a sub-second SLA.

### Custom extraction prompts

Use `MemoryConfig` to pass a domain-specific extraction prompt. For a data engineering agent, instruct Mem0 to capture "preferred SQL dialect, data stack, active projects, blockers." This prevents generic extraction and reduces irrelevant memory retrieval.

```python
from mem0 import Memory
from mem0.configs.base import MemoryConfig

custom_extraction_prompt = """
Extract key facts focusing on:
1. Personal preferences and constraints
2. Professional context and role
3. Technical background and stack
4. Goals, blockers, and priorities

Conversation: {messages}
Format as clear, concise facts.
"""

config = MemoryConfig(
    vector_store={"provider": "qdrant", "config": {"url": "localhost", "port": 6333}},
    llm={"provider": "openai", "config": {"model": "gpt-4.1-nano-2025-04-14"}},
    embedder={"provider": "openai", "config": {"model": "text-embedding-3-small"}},
)
memory = Memory(config=config)
```

For context on what the [AI agent cold start problem](https://atlan.com/know/ai-agent-cold-start-problem/) means for memory systems at scale, see the linked reference.

---

## Step 7: monitor and govern long-term memory

**What you'll accomplish:** Detect and evict stale memories, and understand where personal-context memory frameworks hit their ceiling for enterprise use cases.

**Time:** Ongoing

### Staleness detection and eviction

- **Mem0:** Supports `memory.update(memory_id, data)` and `memory.delete(memory_id)`. Build a staleness check that retrieves all memories for a user (`memory.get_all(user_id=USER_ID)`) and evicts facts older than a configurable TTL.
- **Zep:** The temporal graph handles `invalid_at` timestamps natively - facts that are superseded are automatically marked stale without manual eviction.
- **LangGraph Store:** TTL is built in. Pass a `TTLConfig` with `default_ttl` in minutes, plus `refresh_on_read` and `sweep_interval_minutes`, and the store expires and sweeps items for you.

### What no framework solves: the "active forgetting" gap

Community analysis (DEV.to, 2025) notes that all current frameworks treat memory as retrieval but lack a mechanism for *when* to retrieve and *when* to forget. No framework implements relevance decay. Irrelevant old memories continue to surface until manually deleted.

As a practical mitigation, implement two patterns:

1. **TTL sweep:** Schedule a daily job that calls `memory.get_all(user_id=USER_ID)` and deletes any memory with a `created_at` timestamp older than 90 days (or your domain-appropriate TTL).
2. **Re-confirmation prompt:** When the agent uses a fact older than 30 days, append to the system prompt: "You last confirmed [X] on [date] - verify this is still accurate before acting on it." This surfaces staleness to the user without deleting the memory prematurely.

### The enterprise governance gap

Personal memory frameworks (Mem0, Zep, LangGraph) store what a user said. They cannot store what an organization knows: which datasets are certified, what lineage relationships apply, who owns which data, or which governance policies are in force. That requires a live metadata layer - not a vector store. For the architectural distinction, see [Active Metadata as AI Agent Memory](https://atlan.com/know/active-metadata-ai-agent-memory/) and [Vector Database vs Knowledge Graph for Agent Memory](https://atlan.com/know/vector-database-vs-knowledge-graph-agent-memory/).

---

## Common pitfalls

The most expensive implementation mistakes are namespace failures (memory leakage between users), LangMem latency surprises (59.82s p95 on production search), Zep async delay (new memories not immediately retrievable), and treating memory as a complete solution for enterprise data context.

### Over-storing (token overhead, irrelevant retrieval)

Passing full conversation histories to `memory.add()` bloats the vector store and causes retrieval to surface irrelevant older context. Mem0's graph mode with custom extraction prompts mitigates this. A 2024 Mem0 issue on graph token cost reported 15x, on the open-source graph path Mem0 has since removed; it closed in March 2026 (`mem0ai/mem0#2066`). Pass only the current turn.

### Single namespace for all users (privacy and isolation failures)

The most common production bug: a shared `Memory()` instance with no `user_id` - all users' memories are stored together and retrieved interchangeably. Always bind every read and write call to an authenticated `user_id` from your session layer.

### No staleness management (stale context corrupts agent decisions)

Memories added six months ago may contradict current facts - for example, a user changed their preferred stack from PyTorch to JAX. Without TTL policies or manual eviction, stale memories inject false context. Implement a scheduled eviction job from day one.

### Ignoring the enterprise knowledge layer

Mem0 and Zep solve personal context memory. They cannot tell an agent which columns in `sales.pipeline` are certified, which ones carry EU data residency restrictions, or who to contact when an SLA breach occurs. This is a different problem requiring live metadata access.

---

## Real stories from real customers: AI context in production


    "We're excited to build the future of AI governance with Atlan. All of the work that we did to get to a shared language at Workday can be leveraged by AI via Atlan's MCP server...as part of Atlan's AI Labs, we're co-building the semantic layer that AI needs with new constructs, like context products."
    - Joe DosSantos, VP of Enterprise Data & Analytics, Workday



    Watch Now ->



    "Atlan is our context operating system to cover every type of context in every system including our operational systems. For the first time we have a single source of truth for context."
    - Sridher Arumugham, Chief Data Analytics Officer, DigiKey



    Watch Now ->


---

## Implementing long-term memory: what to do next

With a working memory implementation in place, the next steps depend on your production goals.

**Multi-agent memory sharing:** Once single-agent memory works, explore shared namespaces in LangGraph Store for agents within the same organization that need access to the same context pool.

**Procedural memory:** If your agent repeatedly makes the same type of mistake, add LangMem's `create_prompt_optimizer` to let the agent improve its own system prompt from user feedback trajectories.

```python
from langmem import create_prompt_optimizer

optimizer = create_prompt_optimizer(
    "anthropic:claude-3-5-sonnet-latest",
    kind="metaprompt",
    config={"max_reflection_steps": 3}
)

improved_prompt = optimizer.invoke({
    "trajectories": trajectories,  # (conversation_turns, feedback) pairs
    "prompt": "You are a helpful AI assistant"
})
```

**Governance layer:** For data agents, connect Atlan's MCP alongside Mem0 or Zep - personal context from the memory framework, organizational data context from Atlan.

**How to measure success:**
- Recall accuracy: test whether the agent correctly answers questions about facts from previous sessions
- Session continuity score: percentage of sessions where the agent greets the user with accurate prior context

**Related resources:**
- [How to Build a Memory Layer for AI Agents](https://atlan.com/know/how-to-build-memory-layer-ai-agents/) - architecture-first companion to this code-first guide
- [Best AI Agent Memory Frameworks 2026](https://atlan.com/know/best-ai-agent-memory-frameworks-2026/) - full framework comparison with scoring

  Book a Demo

---

## FAQs about implementing long-term memory for AI agents

### 1. How do you add memory to an AI agent?

Wrap the agent's LLM call with two operations: before the call, run `memory.search(user_input, user_id=user_id, limit=5)` to retrieve relevant past context and inject it into the system prompt; after the call, run `memory.add([user_turn, assistant_turn], user_id=user_id)` to store the new exchange. That's the complete pattern for Mem0.

### 2. What is long-term memory in an AI agent?

Long-term memory is a persistent external store - vector database, knowledge graph, or relational DB - that survives between LLM API calls and sessions. Unlike in-context memory (conversation history in the prompt), long-term memory is indexed and retrieved semantically, so it scales beyond token limits.

### 3. How does Mem0 work for AI agent memory?

Mem0 intercepts messages passed to `memory.add()`, runs an LLM-based extraction pass to identify ADD/UPDATE/DELETE/NOOP operations, and stores extracted facts as vector embeddings in a configurable backend (Qdrant, ChromaDB, and 17 others). On retrieval, `memory.search()` runs a semantic search and returns ranked facts. Mem0 achieves 66.9% recall accuracy at 0.200s p95 latency on the LOCOMO benchmark.

### 4. What is the difference between LangMem and Mem0?

Mem0 is a standalone memory framework optimized for speed (0.200s p95 latency) and breadth (19 vector backends). LangMem is a LangChain SDK that adds semantic, episodic, and procedural memory on top of LangGraph Store - its unique feature is prompt optimization (an agent that improves its own system prompt over time). LangMem's p95 search latency is 59.82 seconds; it should not be used for synchronous retrieval.

### 5. Can LangChain agents have persistent memory?

Yes. LangGraph Store paired with PostgresStore for production provides persistent memory for any LangGraph agent. Swap `InMemoryStore` (dev-only, lost on restart) for `PostgresStore.from_conn_string(DB_URI)` with a one-line change. The store object interface is identical - no agent code changes required.

### 6. How do I implement cross-session memory in Python?

Use Mem0's open-source `Memory` class with a persistent vector backend: configure Qdrant (self-hosted) or use the Mem0 cloud client. Bind every `add()` and `search()` call to a stable `user_id`. Memory persists across process restarts because it lives in the vector store, not in Python memory.

### 7. What vector database should I use for AI agent memory?

For local development: ChromaDB (no Docker needed). For self-hosted production: Qdrant (best Mem0 integration, scales to billions of vectors). For teams already on LangGraph: PostgresStore with the pgvector extension. For temporal and relational queries: Zep's Graphiti engine (hosted).

### 8. What is Zep used for in AI agents?

Zep builds a temporal knowledge graph from conversation messages and structured business data. Its primary advantage over Mem0 is understanding how facts change over time - Zep can answer questions like "who owned this account in Q3 vs Q4?" using `valid_at`/`invalid_at` edge timestamps. Best for CRM-style agents, support agents with account history, and any use case where temporal reasoning about changing facts matters.

---

## Sources

1. [Mem0: Building Production-Ready AI Agents with Scalable Long-Term Memory, Mem0 / ECAI 2025](https://arxiv.org/abs/2504.19413)
2. [State of AI Agent Memory 2026, Mem0](https://mem0.ai/blog/state-of-ai-agent-memory-2026)
3. [Mem0 LOCOMO latency table (LangMem search p95 59.82s), arXiv](https://arxiv.org/abs/2504.19413)
4. [Zep: A Temporal Knowledge Graph Architecture for Agent Memory (Zep vendor research), arXiv](https://arxiv.org/abs/2501.13956)
5. [Launching Long-Term Memory Support in LangGraph, LangChain](https://blog.langchain.com/launching-long-term-memory-support-in-langgraph/)
6. [Add TTLs to your LangGraph application, LangChain](https://langchain-ai.github.io/langgraph/how-tos/ttl/configure_ttl/)
7. [LangMem SDK Launch, LangChain](https://www.langchain.com/blog/langmem-sdk-launch)
8. [Long-Term Agentic Memory with LangGraph, DeepLearning.AI](https://www.deeplearning.ai/short-courses/long-term-agentic-memory-with-langgraph/)
9. [Zep Quick Start Guide, Zep](https://help.getzep.com/quick-start-guide)
10. [Mem0 pricing and plan limits, Mem0](https://mem0.ai/pricing)

## Next steps

- To see Atlan's context layer in action first, watch a recorded demo: [Atlan context layer demos](https://atlan.com/context-layer-demo/)
- To see Atlan working on your own data stack, book a demo: [Book a demo with Atlan](https://atlan.com/forms/talk-to-sales-contact/)
