Long-term Memory: mem0

Mem0 is an open-source memory layer designed specifically for LLMs and AI agents. It gives AI applications the ability to remember, learn from, and personalize interactions across sessions—much like a human would.

CN: Mem0 是一个专为 LLM 和 AI Agent 设计的开源记忆层。它让 AI 应用能够像人一样,跨会话记住、学习并个性化交互

 Imagine your AI agent is a personal assistant. Without Mem0, every time you walk into the office, the assistant has no idea who you are, what you like, or what you talked about yesterday. You have to reintroduce yourself every single time. With Mem0, the assistant has a notebook that automatically writes down important things about you—your name, preferences, past requests—and reads the relevant notes before every conversation.

CN: 想象你的 AI Agent 是一个私人助理。没有 Mem0,每次你走进办公室,助理都不知道你是谁、喜欢什么、昨天聊了什么。你每次都要重新介绍自己。有了 Mem0,助理有一本笔记本,自动记录关于你的重要信息——你的名字、偏好、过去的请求——并在每次对话前阅读相关笔记。

Core Concepts of Mem0

2.1 Memory Lifecycle (记忆的生命周期)

EN: Mem0 handles memories through a complete lifecycle:

  • Extract (提取): Identifies important information from conversations
  • Store (存储): Saves structured memories with embeddings in a vector database
  • Retrieve (检索): Uses semantic search to find relevant memories
  • Update (更新): Smartly updates memories over time, resolving contradictions

CN: Mem0 通过完整的生命周期管理记忆:

  • 提取: 从对话中识别重要信息
  • 存储: 将结构化记忆连同向量嵌入保存到向量数据库
  • 检索: 使用语义搜索查找相关记忆
  • 更新: 智能地随时间更新记忆,解决矛盾

2.2 Memory Scopes (记忆的作用域)

EN: Mem0 associates each memory with an identifier, allowing organization by:

  • User ID: Personal memories for a specific user
  • Agent ID: Memories for a specific agent
  • Session: Short-term context within a session
  • Organization: Shared memories across an organization

CN: Mem0 将每个记忆与一个标识符关联,允许按以下维度组织:

  • User ID: 特定用户的个人记忆
  • Agent ID: 特定 Agent 的记忆
  • Session: 会话内的短期上下文
  • Organization: 跨组织的共享记忆

2.3 Mem0 vs RAG

EN: RAG is for static facts—documents, knowledge bases. AI Memory is for dynamic state—user preferences that evolve, change, and contradict over time. Mem0 is specialized for the memory use case, not a general-purpose RAG tool.

CN: RAG 针对静态事实——文档、知识库。AI Memory 针对动态状态——用户偏好会随时间演变、变化甚至自相矛盾。Mem0 是专门为记忆场景设计的,不是通用 RAG 工具

Mem0 Architecture

 Mem0 uses a hybrid approach:

ComponentENCN
LLM (e.g., GPT-4o-mini)Extracts facts and updates memories提取事实和更新记忆
Embedding Model (e.g., text-embedding-3-small)Converts text to vectors (1536 dims)将文本转换为向量
Vector Database (e.g., Qdrant)Stores and searches memory vectors存储和搜索记忆向量
History DB (SQLite)Tracks all memory operations追踪所有记忆操作
Reranker (optional)Re-ranks retrieved memories for relevance对检索结果重新排序

Key Takeaways

要点ENCN
Mem0 是 LLM 的记忆层,非 RAGMem0 is a memory layer for LLMs, not RAGMem0 是 LLM 的记忆层,不是 RAG
LLM 无状态,Mem0 提供跨会话记忆LLMs are stateless; Mem0 provides cross-session memoryLLM 无状态;Mem0 提供跨会话记忆
核心操作:m.add() 存储,m.search() 检索Core ops: m.add() to store, m.search() to retrieve核心操作:m.add() 存储,m.search() 检索
记忆通过 user_id/agent_id 作用域隔离Memories scoped by user_id/agent_id记忆通过 user_id/agent_id 作用域隔离
默认使用 OpenAI (gpt-4o-mini + text-embedding-3-small)Default: OpenAI (gpt-4o-mini + text-embedding-3-small)默认使用 OpenAI (gpt-4o-mini + text-embedding-3-small)
LangGraph 中作为 retrieval + update 两个节点In LangGraph: retrieval + update nodesLangGraph 中作为 retrieval + update 两个节点
生产环境需显式配置 LLM/向量库Production: explicit LLM/vector DB config生产环境需显式配置 LLM/向量库

Type of Memory System

Think of an AI Agent as a new employee at your data company. Without memory, this employee lives in a state of permanent “Groundhog Day.” Every time you speak to him, it’s his first day on the job. He doesn’t remember the SQL query you asked for 5 minutes ago, doesn’t remember your preference for partitioned tables, and certainly doesn’t remember the mistake he made last week.

What is “Memory” in an AI Agent?

In the context of AI and Large Language Models (LLMs), a Memory System is not a single component (like RAM in a computer). Instead, it is a multi-layered architectural framework designed to store, retrieve, and synthesize information across different timescales.

Memory is what turns a stateless API caller into a true, stateful agent. It enables continuity, personalization, and learning over time. Without it, every interaction is isolated; with it, the agent becomes a colleague that grows with you.

记忆是把一个“无状态 API 调用者”变成真正的“有状态 Agent”的关键。它实现了连续性、个性化和长期学习。没有记忆,每次交互都是孤立的;有了记忆,Agent 就变成了一位与你一同成长的同事。

 In cognitive architectures for LLMs, we classify memory into four distinct types, borrowing heavily from cognitive psychology (Atkinson-Shiffrin model) but adapted for software engineering.
CN: 在 LLM 的认知架构中,我们借鉴认知心理学(Atkinson-Shiffrin 模型)并将其适配到软件工程,将记忆划分为四种不同类型。

  1. Short-Term Memory (STM) – The immediate context window.
    短期记忆(STM) – 即时的上下文窗口。
  2. Long-Term Memory (LTM) – Permanent storage of facts and preferences.
    长期记忆(LTM) – 事实和偏好的永久存储。
  3. Episodic Memory – Memory of specific past events and interactions.
    情景记忆(Episodic Memory) – 对特定过去事件和交互的记忆。
  4. Semantic Memory – Memory of general world knowledge and structured concepts.
    语义记忆(Semantic Memory) – 对通用世界知识和结构化概念的记忆。

1. Short-Term Memory (STM) / Working Memory

This is the “scratchpad” of the agent. It holds whatever the LLM is currently processing. In practical terms, this is the Context Window combined with the current conversation turn.
CN: 这是 Agent 的“草稿纸”。它保存 LLM 当前正在处理的内容。在实际中,这就是 Context Window(上下文窗口) 加上当前的对话轮次。

Volatile, limited capacity (e.g., 128k tokens), and fast. It’s managed by the system prompt and the chat history.

易失性、容量有限(例如 128k tokens)、速度快。它由 System Prompt 和聊天历史管理。

Example: In a chat, the agent remembers your name because you just told it 2 messages ago. It remembers the user_id variable you passed in the current API call.
CN: 例子: 在聊天中,Agent 记得你的名字,因为你刚在 2 条消息前告诉过它。它记得你在当前 API 调用中传递的 user_id 变量。

2. Long-Term Memory (LTM)

This is the “hard drive” of the agent. It stores facts, user preferences, and configurations that persist across sessions (days, weeks, or months).
CN: 这是 Agent 的“硬盘”。它存储跨会话(数天、数周或数月)持久化的事实、用户偏好和配置。

 Characteristics: Persistent, high capacity, slower to access (requires a database query), and durable.
CN: 特点: 持久性、高容量、访问速度较慢(需要数据库查询)、耐用。

Example: The agent remembers that you always prefer parquet over csv, or that your company’s production database is postgres://prod-db:5432. This is stored in a PostgreSQL table keyed by your user_id.
CN: 例子: Agent 记得你总是更喜欢 parquet 而不是 csv,或者你公司的生产数据库是 postgres://prod-db:5432。这存储在按 user_id 索引的 PostgreSQL 表中。

3. Episodic Memory

This is the “diary” or “logbook” of the agent. It records specific, time-stamped past interactions, tasks completed, successes, and failures. It answers the question: “What happened previously?”
CN: 这是 Agent 的“日记”或“日志”。它记录具体的、带时间戳的过去交互、已完成的任务、成功和失败。它回答:“之前发生了什么?”

Characteristics: Chronological, specific, and context-rich. Used for error analysis, auditing, and experience reuse.
CN: 特点: 按时间顺序排列、具体、上下文丰富。用于错误分析、审计和经验复用。

 Example: “On July 15th at 3 PM, you asked me to generate a sales report, but I failed because the table sales_2023 was missing.” The agent recalls this specific failure to avoid repeating it or to suggest a fix.
CN: 例子: “在 7 月 15 日下午 3 点,你让我生成一份销售报告,但因为表 sales_2023 不存在,我失败了。” Agent 回忆起这个具体的失败,以避免重蹈覆辙或建议修复方案。

4. Semantic Memory

This is the “encyclopedia” of the agent. It stores general, factual, and conceptual knowledge about the world and your specific domain (like your company’s business logic), decoupled from specific experiences.
CN: 这是 Agent 的“百科全书”。它存储关于世界和你特定领域(如你公司的业务逻辑)的通用、事实性和概念性知识,与具体经历脱钩。

Characteristics: Factual, abstract, and structured. This is typically your RAG Knowledge Base.
CN: 特点: 事实性、抽象、结构化。这通常就是你的 RAG 知识库

Example: “The sales table is joined with customers on customer_id.” This is a fact about your data model. It is true regardless of who asked, or when.
CN: 例子: “sales 表通过 customer_id 与 customers 表关联。” 这是关于你数据模型的一个事实。无论谁问,或者什么时候问,它都是真的。

Summary Table of the Four Types

Memory TypeHuman AnalogyAI ImplementationPersistenceExample
Short-TermSticky Note / Scratchpadmessages array, Context WindowSession only (Volatile)Current chat conversation
Long-TermEmployee File / Hard DrivePostgreSQL / RedisPermanent (Years)“I prefer Python 3.11”
EpisodicPersonal Diary / LogsTime-series DB / Vector DBPermanent (Months/Years)“Last week, you asked for X and got error Y.”
SemanticEncyclopedia / WikiVector DB / Graph DBPermanent (Updated via RAG)“The Primary Key of orders is order_id.”

Key Takeaways Table

要点 (EN)要点 (CN)
Memory turns a stateless LLM into a stateful Agent.记忆将无状态 LLM 变为有状态 Agent。
STM is the current context window; manage it via sliding windows.短期记忆是当前上下文窗口;通过滑动窗口管理它。
LTM stores user preferences (key-value) in databases like PostgreSQL.长期记忆在 PostgreSQL 等数据库中存储用户偏好(键值对)。
Episodic is a chronological log of actions; use it for audit and learning.情景记忆是行动的时间顺序日志;用于审计和学习。
Semantic is general domain knowledge; typically powered by Vector DBs (RAG).语义记忆是通用领域知识;通常由向量数据库驱动(RAG)。
The build_system_context() method is where you integrate all four types for the LLM.build_system_context() 方法是整合所有四种记忆给 LLM 的地方。
In production, STM is ephemeral, LTM/Episodic/Semantic are persistent.在生产中,短期记忆是临时的,长期/情景/语义记忆是持久的。