AI Agent long-term memory layer

Document type
Field manual
Updated
Reading time
12 min

Mem0 memory-layer guide: pip/npm setup, add/search APIs, library, self-hosted and cloud deployments, agent integrations and a memory-enabled chatbot.

SourceOpen original report
Document details
mem0ai/mem0
stars
57.2K
forks
6.5K
Data as of

Mem0 (pronounced "mem-zero") adds intelligent memory to AI assistants and agents, extracting facts worth remembering from each conversation and retrieving them automatically next time for personalized answers. Use two pip or npm installation lines, run Memory.add / Memory.search, choose library / self-hosted / cloud deployment and implement a complete memory-enabled chatbot.

GitHub Stars
55.5k
LongMemEval accuracy
93.4
pip / npm installation
2 lines
Open-source license
Apache2.0

01Purpose and operation

Independent memory API between applications and LLMs

Large language models are stateless: each new conversation starts without knowledge of you. Most users supply full history as context to provide memory, but this is slow and expensive, and exceeding the context limit causes all history to be forgotten. Mem0 separates memory into an independent service: after a conversation, it extracts facts worth keeping (preferences, decisions, names and project context) into a vector store; next time, it retrieves the most semantically relevant facts and injects only those into the prompt.

Mem0 provides add / search / get_all memory APIs between your application and LLM; it is neither a RAG framework nor a chatbot. The official description says Mem0 (mem-zero) adds intelligent memory to AI assistants and agents for personalized interactions. It has User, Session and Agent scopes, keeping long-term user preferences, current conversation context and an agent's own experience separate.

The accompanying 2026/4 paper (arXiv:2504.19413) explains this design: on the LoCoMo long-conversation benchmark, memory-based answer accuracy rises from 71.4 to 91.6; LongMemEval rises from 67.8 to 93.4. Retrieving a few facts instead of full history keeps token use per operation at 6.7K–7.0K and p50 latency at 0.88–1.09 seconds. The memory layer can improve memory accuracy and reduce computation.

  1. User message

  2. search

  3. Inject context

  4. LLM response

  5. add

  6. Extract facts

  7. Save to vector store

Mem0 ("mem-zero") enhances AI assistants and agents with an intelligent memory layer, enabling personalized AI interactions.

— mem0ai/mem0 README · Project description

02Two-line installation

Two-line installation and OpenAI-backed memory

Mem0 library mode requires no server or account: install with pip install mem0ai (or npm install mem0ai for Node). OpenAI is the default LLM and embedding provider, so first set export OPENAI_API_KEY=..., then use from mem0 import Memory; memory = Memory(). For advanced NLP such as entity extraction, install pip install mem0ai[nlp] and download the spaCy model.

bash
# Basic installation
pip install mem0ai

# For advanced NLP / entity extraction
pip install "mem0ai[nlp]"
python -m spacy download en_core_web_sm

# Supply a key (OpenAI is the default LLM + embedding provider)
export OPENAI_API_KEY=sk-...
python
from mem0 import Memory

memory = Memory()
memory.add("我偏好深色主題,還有 vim 鍵位", user_id="alice")
results = memory.search(query="alice 喜歡什麼設定?", filters={"user_id": "alice"}, top_k=3)
for entry in results["results"]:
    print(entry["memory"])

CLI and browser extension

On Node, npm install -g @mem0/cli installs a CLI; run mem0 init once for the setup wizard, then add and search memories in the terminal. The official browser extension (the README lists ChatGPT and other uses) synchronizes preferences accumulated in different AI chat tools into shared memory.

bash
npm install -g @mem0/cli
mem0 init
mem0 add "Prefers dark mode and vim keybindings" --user-id alice
mem0 search "What does Alice prefer?" --user-id alice

03APIs / Deployment / Integrations

Core APIs: add, search and memory management

Mem0 has a small API surface: add turns conversations into memories, search retrieves them, and get_all / update / delete manage them. Its surrounding features add complexity: memory scopes (User / Session / Agent), configurable LLMs and vector stores, graph memory, self-hosted servers, a hosted platform, CLI, browser extension and agent framework integrations.

Core API · 01

memory.add()

Write memories

Pass messages (including an entire conversation); Mem0 automatically extracts facts worth long-term storage and saves them in a vector store. Use user_id / agent_id / run_id to assign ownership.

Core API · 02

memory.search()

Retrieve memories

A query performs semantic retrieval; filters restrict scope and top_k controls count. It returns the most relevant memories to inject into the prompt, instead of the entire history.

Core API · 03

memory.get_all()

List all memories

Export all memories for a user / session for audits, debugging or user access to what AI remembers. Users can inspect memory contents.

Core API · 04

update · delete · reset

Update and forget

Update or delete individual memories, or clear everything with reset. This lets your product implement a button to forget a particular fact.

Scope · 05

user_id / run_id / agent_id

Three memory scopes

User covers long-term preferences across conversations, Session (run) covers the current conversation, and Agent covers accumulated agent experience. These scopes are isolated to avoid contamination.

Config · 06

Memory.from_config()

Replace default models

OpenAI is the default LLM and embedding provider. A config dict switches to another provider or local models without changing calling code.

Config · 07

vector_store

Replace the vector store

Choose Qdrant, Chroma, pgvector and other backends, from local files to cloud hosting. You choose memory storage while the API stays consistent.

Graph · 08

graph memory

Graph memory

When enabled, Mem0 builds a knowledge graph of people, events and relationships, linking entities across memories. Retrieval can follow relationships as well as semantic similarity.

Hosted · 09

MemoryClient

Hosted platform

Get an API Key at app.mem0.ai. The same add / search APIs store memory in the Mem0 cloud, with a Dashboard and usage analytics. No maintenance is required.

Self-Host · 10

docker compose up

Self-hosted server

Run the memory layer on your infrastructure with a built-in Dashboard and API Key management, for teams needing control over data storage.

CLI · 11

@mem0/cli

CLI memory

Install globally with npm and use mem0 init / add / search to operate memory in the terminal without writing code, for scripts or quick experiments.

Integrations · 12

LangGraph · CrewAI · Vercel AI SDK

Integrate with your Agent

Official integrations include LangGraph, CrewAI, Vercel AI SDK, Claude Code, Cursor and Windsurf, plus a browser extension for ChatGPT and other chat tools, connecting memory to existing agent frameworks.

Library / self-hosted / cloud comparison

DimensionLibrarySelf-Hosted serverCloud Platform
Best suited toTests, prototypesTeams running their own infrastructureProduction without maintenance
Getting startedpip install mem0aidocker compose upRegister at app.mem0.ai
Dashboard—YesYes
Accounts and API Key management—YesYes
Advanced features—SomeAll

04Usage notes · README / Official documentation

Memory scopes, retrieval counts and deployment

Mem0 has few APIs, but usage choices matter: incorrect scopes, retrieving too many memories or leaving models unchanged can cancel its cost savings. These notes come from the official README, documentation and accompanying paper.

Separate memory scopes

Most users start with only user_id. Isolate current conversation context with run_id, and agent experience with agent_id. Mixing the three scopes contaminates long-term preferences with short-term conversation content.

Source · mem0 official documentation · Memory scopes

Keep top_k small

The quickstart deliberately uses top_k=3. Mem0 is designed to retrieve only a few highly relevant memories; retrieving 20 and inserting them into the prompt returns to the full-history approach, making it slow, expensive and noisy.

Source · README · quickstart + Benchmark method

add accepts entire conversations

You do not need to split sentences. Pass the entire message array [{"role":"user",...},{"role":"assistant",...}] to memory.add(); Mem0 extracts facts to remember. You assign their user / session.

Source · README · chat_with_memories

Save assistant replies too

The official example deliberately uses messages.append for the assistant reply before add, because AI commitments (such as agreeing to switch to dark mode) are facts worth remembering too, alongside user messages.

Source · README · quickstart code

[nlp] extra is not installed by default

Basic pip install mem0ai excludes advanced NLP / entity extraction dependencies. Install pip install "mem0ai[nlp]" and download the spaCy model (python -m spacy download en_core_web_sm) before discovering missing dependencies in production.

Source · README · Installation

Verify with the library, then move to a server for scale

The APIs for the three deployments are nearly identical. Verify logic with pip install; when collaboration, a Dashboard or API Key management is needed, switch to docker compose up for self-hosting or app.mem0.ai for hosting, without rewriting calling code.

Source · README · Deployment comparison

Local models and token costs

Each add / search uses OpenAI by default (fact extraction + embeddings). For research or internal tools, use Memory.from_config() to point the LLM and embedder at local models, tying costs to your own compute.

Source · mem0 official documentation · Configuration

Graph memory tradeoffs

arXiv:2504.19413 explains the tradeoffs between vectors alone and vectors + graphs. Graphs pay off only in multi-entity, multi-hop scenarios. Read the paper's figures before connecting another graph database.

Source · arXiv:2504.19413

05Worked example · An assistant that remembers you

Installation and cross-conversation memory verification

In this example, Alice tells the assistant her preferences in the first conversation. The next day, a new process with no context still remembers Traditional Chinese, TypeScript and no emoji. History is not inserted manually; memory.add and memory.search handle it. Commands follow the official README quickstart.

~/projects/support-bot · python 3.12 · mem0ai


> pip install mem0ai
# Successfully installed mem0ai-...
> export OPENAI_API_KEY=sk-...


# ── First conversation: Alice tells the assistant her preferences ──────────────
> python
$ >>> from mem0 import Memory
$ >>> memory = Memory()
# [mem0] llm + embedder = OpenAI · vector store = 本機
$ >>> messages = [
  $ ...   {"role":"user","content":"我是 Alice,前端工程師。回我訊息用繁體中文,程式碼用 TypeScript,不要 emoji。"},
  $ ...   {"role":"assistant","content":"好的 Alice,我記下了:繁體中文回覆、TypeScript、無 emoji。"}]
$ >>> memory.add(messages, user_id="alice")
# [mem0] 萃取出 4 條事實 · event = ADD
  # · Name is Alice; works as a frontend engineer
  # · Prefers replies in Traditional Chinese
  # · Prefers code samples in TypeScript
  # · Does not want emoji in replies
$ >>> exit()


# ── Next day, a new process with no retained context ──────────
> python chat.py
$ You: 幫我寫一個 debounce 函式


# [mem0] memory.search(query="幫我寫一個 debounce 函式",
                      # filters={"user_id":"alice"}, top_k=3)
# [mem0] → 撈回 3 條:Traditional Chinese / TypeScript / no emoji
# [llm] 把這 3 條塞進 system prompt,再丟 user 問題


claude: AI: 沒問題 Alice,這是 TypeScript 版的 debounce:
  claude: export function debounce<T extends (...a: any[]) => void>(fn: T, ms: number) {
    claude: let t: ReturnType<typeof setTimeout>;
    claude: return (...a: Parameters<T>) => { clearTimeout(t); t = setTimeout(() => fn(...a), ms); };
  claude: }


# [mem0] memory.add(本輪對話, user_id="alice")  ← 這次互動也存起來了
ok: [mem0] 沒有新事實要加(問題與既有偏好一致)· event = NONE


$ You: exit
claude: Goodbye!

        

Remembering that Alice uses TypeScript comes from search retrieval followed by prompt injection, without manually maintaining conversation history.

— Mem0 core mechanism: move memory from the context window to a vector store and retrieve on demand · Translated from Chinese

Cross-conversation memory and assistant replies

The example retrieves memory in a new process the next day. Traditional approaches either store full history in a database and insert it into each prompt (expensive, slow and liable to exceed limits), or retain no memory. Mem0 extracts conversations into a few stored facts and retrieves only the most relevant ones when needed. Token use stays stable and latency controllable; the user perceives an AI that knows them.

The example also puts assistant replies into messages before add. AI commitments are part of memory, distinguishing the implementation of a personalized assistant from a chatbot that only looks up preferences.

06Constraints

Memory costs, limits and maintenance

07Next steps

From prototypes to production deployment

Library mode is a starting point. Models, vector stores, deployment and integration frameworks can all be replaced. The next step is usually connecting Mem0 to existing systems rather than learning a new API.

Five deployment and integration settings

1. Replace default models and vector stores. Use Memory.from_config() to point the LLM and embedder at a chosen provider or local models, storing memory in Qdrant, Chroma or pgvector without changing calling code.

2. Separate memory scopes. user_id / run_id / agent_id work like database indexes: long-term preferences, single-conversation context and agent experience stay separate for clean retrieval.

3. Evaluate graph memory. For multi-entity, multi-hop scenarios (team membership or project dependencies on services), graph memory lets retrieval follow relationships. Use figures in arXiv:2504.19413 to assess whether it pays off.

4. Connect your Agent framework. LangGraph, CrewAI, Vercel AI SDK, Claude Code, Cursor and Windsurf have official integrations. For browser usage, use the official memory extension (the README lists ChatGPT and other uses).

5. Choose deployment. Use the library for prototypes, self-hosting (docker compose up, with a built-in Dashboard and API Key management) for team collaboration, or app.mem0.ai hosting for deployment without maintenance.

Three further documents

① docs.mem0.ai: Complete API, configuration, integrations, self-hosting and platform documentation. ② arXiv:2504.19413: Accompanying paper covering memory methods, benchmarks and vector-only vs graph tradeoffs. ③ github.com/mem0ai/mem0: Source, examples/, Node SDK and CLI.

Mem0 ("mem-zero") enhances AI assistants and agents with an intelligent memory layer, enabling personalized AI interactions.

— mem0ai/mem0 README