第5章:主流框架对比与选型
Mem0、Letta、Zep+Graphiti、LangGraph Long-term Memory、LangMem、Cognee 等主流 Agent Memory 框架的设计哲学、能力边界与选型决策
读懂论文之后,真到生产你不会从零造轮子——而是要选对框架。本章把 6 个主流 Agent Memory 框架横向对比,每个给出最简启动代码、设计哲学、能力边界,最后给一棵可操作的选型决策树。读完这一章,你应该能回答”我这个具体业务该用哪个”。
📑 目录
- 1. 框架全景速查
- 2. Mem0 — 开源 SDK 的事实标杆
- 3. Letta(MemGPT)— OS-style Agent Runtime
- 4. Zep + Graphiti — Bi-temporal KG 之王
- 5. LangGraph Long-term Memory — LangChain 原生
- 6. LangMem — 三类 Memory 工具集
- 7. Cognee / Supermemory — 其他玩家
- 8. 选型决策树
- 自我检验清单
- 参考资料
1. 框架全景速查
| 框架 | 出品方 | 开源协议 | 部署 | 核心特性 | 最适合 |
|---|---|---|---|---|---|
| Mem0 | mem0.ai | Apache 2.0 | 自托管 / 云 | vector + graph + KV 三层 | 个性化 chatbot |
| Letta | Letta Inc.(原 MemGPT 团队) | Apache 2.0 | 自托管 / 云 | OS-style memory,完整 agent runtime | 长会话 stateful agent |
| Zep + Graphiti | Zep AI | Apache 2.0(Graphiti) | 自托管 / 云 | bi-temporal KG | 企业级,时间一致性敏感 |
| LangGraph LT-Memory | LangChain | MIT | 自托管 | Store API,namespace,跨 thread | 已用 LangGraph 栈 |
| LangMem | LangChain | MIT | 库 | 三类 Memory helper | 快速原型 |
| Cognee | Cognee | Apache 2.0 | 自托管 | LLM 驱动 KG 构建 | 研究 / 知识图 |
| Supermemory | Supermemory | SaaS | 云 | 极简 REST | MVP 创业 |
2. Mem0 — 开源 SDK 的事实标杆
GitHub: mem0ai/mem0(48K+ stars)| 官网: mem0.ai
2.1 设计哲学
“给 Agent 加一行代码就有 Memory。”
把所有复杂的 extraction / dedup / merge 都封装在 memory.add(...) 里,开发者只关心”加什么、查什么”。
2.2 三层 Store
memory.add(messages, user_id="u1")
↓
LLM 抽取 facts → ["用户喜欢咖啡", "用户对乳糖不耐受"]
↓
Vector store(语义): 找相关老 memory → 决定是否 ADD/UPDATE/DELETE/NOOP
Graph store(关系): 抽取 entity-relation 三元组,写入 KG
SQLite / Postgres(metadata): 写历史日志
2.3 最简启动代码
from mem0 import Memory
memory = Memory() # 默认 Qdrant + SQLite + OpenAI
# 添加
memory.add(
messages=[
{"role": "user", "content": "我对乳糖不耐受"},
{"role": "assistant", "content": "好的,记住了"},
],
user_id="user_123",
)
# 检索
results = memory.search(query="早餐推荐", user_id="user_123")
# [{"memory": "用户对乳糖不耐受", "score": 0.92, ...}]
# 完整列表
all_memories = memory.get_all(user_id="user_123")
2.4 关键能力
| 能力 | 说明 |
|---|---|
| 三层 scope | user_id / session_id / agent_id 三个维度的 namespace |
| 自动去重 | embedding 相似度 + LLM judge,避免重复存 |
| 自动 update | 新事实和老事实冲突时,LLM 决定 ADD vs UPDATE vs DELETE |
| Graph 模式 | 启用后自动抽实体和关系,supports 多跳查询 |
| 多 vector store 支持 | Qdrant / Weaviate / Chroma / pgvector / Milvus / … |
| 多 LLM 支持 | OpenAI / Anthropic / Gemini / Ollama / Groq |
2.5 优劣
✅ 优势:开箱即用、API 简洁、社区活跃、文档完善 ❌ 劣势:每次 add 都要 LLM 调用(贵)、Graph 模式延迟较高、对极端复杂时间一致性场景不如 Zep
3. Letta(MemGPT)— OS-style Agent Runtime
GitHub: letta-ai/letta | 公司: Letta Inc.(MemGPT 论文作者创办)
3.1 设计哲学
“Agent 是 OS 级别的实体,有自己的状态、工具、记忆,能持久化、能被序列化、能跨进程恢复。”
不只是 memory 库,而是完整 Agent runtime——你不是”用 Letta 做 memory”,而是”用 Letta 写 agent”,memory 是其内置子系统。
3.2 三层 Memory
完全继承 MemGPT 论文设计:
| 层 | 类比 | 内容 |
|---|---|---|
| Core Memory | RAM | system prompt 中始终在线的 fact;典型 human block 和 persona block |
| Recall Memory | 历史对话 page | 旧对话历史,被 vector index |
| Archival Memory | disk | 长期事实库 / 文档,vector store |
LLM 通过工具调用切换 page:
core_memory_append, core_memory_replace
recall_memory_search, recall_memory_insert
archival_memory_search, archival_memory_insert
3.3 最简启动代码
from letta_client import Letta
client = Letta(base_url="http://localhost:8283")
# 创建 agent(自带 memory)
agent = client.agents.create(
name="my_assistant",
memory_blocks=[
{"label": "human", "value": "用户偏好咖啡不加糖"},
{"label": "persona", "value": "我是一个友好的助手"},
],
model="openai/gpt-4o-mini",
embedding="openai/text-embedding-3-small",
)
# 对话(memory 自动管理)
response = client.agents.messages.create(
agent_id=agent.id,
messages=[{"role": "user", "content": "推荐一家早餐店"}],
)
Agent 在内部决定何时 core_memory_append、何时 archival_memory_search,无需开发者干预。
3.4 关键能力
- Agent 状态完全持久化:可以关进程后恢复,跨周复用
- OS-style memory tool:LLM 自调度,不需要应用层管 paging
- 多模态 memory blocks:human / persona / scratchpad / 自定义
- 完整 agent runtime:tool calling、planning、自我反思都内置
- 企业级:auth、multi-tenant、audit log
3.5 优劣
✅ 优势:Agent 概念完整、长会话表现极好、企业级部署成熟 ❌ 劣势:学习曲线高、不适合”只想加点 memory”的轻量场景、对 LLM 调用次数多
4. Zep + Graphiti — Bi-temporal KG 之王
GitHub: getzep/graphiti | 论文: arXiv 2501.13956
4.1 设计哲学
“事实有时间,Memory 必须知道’这件事什么时候是真的’。”
把所有 memory 都建成 KG,每条 edge 带 4 时间戳(t_valid / t_invalid / t_created / t_expired),支持精确的”时间穿越”查询。
4.2 架构
Conversation / Tool calls / Documents
↓
Graphiti
├─ LLM 抽 entity + relation
├─ 与已有 KG 比对,检测冲突
├─ 自动 invalidate 过期事实
└─ 写入 Neo4j(节点和边带 vector + 时间戳)
↓
Hybrid 检索
├─ KG 多跳遍历
├─ Node embedding cosine
└─ BM25 全文
4.3 最简启动代码
from graphiti_core import Graphiti
from datetime import datetime
graphiti = Graphiti(
"bolt://localhost:7687", "neo4j", "password",
)
await graphiti.build_indices_and_constraints()
# 添加事实(自动抽取 entity / relation,处理时间冲突)
await graphiti.add_episode(
name="user_preference",
episode_body="我现在住在北京,以前住在上海",
source_description="客服对话",
reference_time=datetime.now(),
)
# 检索(支持时间穿越)
results = await graphiti.search(
query="用户当前住址",
center_node_uuid=user_uuid, # 以用户为中心
)
4.4 关键能力
- Bi-temporal:第 3 章已详解
- 自动事实演化:新事实进来,LLM 检测并 invalidate 老事实
- 混合检索:KG + vector + BM25 三路召回
- Custom ontology:可以预定义 entity / relation schema
- 18.5% 准确率提升 + 90% 延迟降低(vs MemGPT,论文实测)
4.5 优劣
✅ 优势:时间一致性最强、KG 推理能力强、企业审计/合规友好 ❌ 劣势:依赖 Neo4j、写入慢(LLM 抽实体 + 检测冲突)、入门门槛高
5. LangGraph Long-term Memory — LangChain 原生
5.1 设计哲学
“LangGraph 已经管 short-term state(checkpoint),long-term memory 应该是它的对等公民。”
提供一个通用 Store API,把 memory 抽象成 namespaced JSON 文档,具体后端可插拔(InMemory / Postgres / Redis / 自定义)。
5.2 关键概念
| 概念 | 说明 |
|---|---|
| Namespace | 类似目录,组织 memory(如 ("user_123", "preferences")) |
| Key | 类似文件名(如 "food") |
| Value | JSON dict |
| Search | 跨 namespace 按 query 语义检索 |
5.3 最简启动代码
from langgraph.store.postgres import PostgresStore
from langgraph.graph import StateGraph
from langgraph.checkpoint.postgres import PostgresSaver
store = PostgresStore.from_conn_string(...)
checkpointer = PostgresSaver.from_conn_string(...)
# 写入 long-term memory
store.put(
namespace=("user_123", "preferences"),
key="food",
value={"dietary": "lactose-intolerant", "cuisine": "Italian"},
)
# 节点函数中使用
def my_node(state, *, config, store):
user_id = config["configurable"]["user_id"]
memories = store.search(
namespace=(user_id, "preferences"),
query=state["messages"][-1].content,
limit=5,
)
# 把检索结果加入 prompt
...
graph = StateGraph(State)
graph.add_node("agent", my_node)
app = graph.compile(checkpointer=checkpointer, store=store)
# 调用
app.invoke(
{"messages": [...]},
config={"configurable": {"thread_id": "t1", "user_id": "user_123"}},
)
5.4 关键能力
- 跨 thread 共享:不同对话可以读同一个 memory
- Namespace 灵活:任意层级,适合多租户 / 多 agent
- 后端可插拔:InMemory(开发)/ Postgres / Redis / 自定义
- 与 LangGraph state 无缝整合:节点函数直接访问 store
5.5 优劣
✅ 优势:与 LangGraph 生态零摩擦、简洁、开发者熟悉 ❌ 劣势:更像是”基础设施 API”,没有 Mem0 那种自动 dedup/merge,需要自己写 extraction / consolidation 逻辑
6. LangMem — 三类 Memory 工具集
GitHub: LangChain 旗下
6.1 设计哲学
“LangGraph Store 是底座,LangMem 提供 episodic/semantic/procedural 三类 helper,让你不用自己写 extraction。”
LangMem 不是独立的 store,而是封装在 LangGraph Store 之上的工具包。
6.2 三类 Helper
from langmem import (
create_memory_manager, # 自动 extract + write semantic
create_memory_searcher,
create_thread_extractor, # 从 thread 抽取 episodic
create_prompt_optimizer, # procedural:优化 system prompt
)
6.3 最简启动代码
from langmem import create_memory_manager
manager = create_memory_manager(
"anthropic:claude-3-5-sonnet",
instructions="抽取用户的食物偏好",
schemas=[FoodPreference],
)
# 在对话结束后调用
result = manager.invoke({"messages": conversation_messages})
# result 是 List[FoodPreference],已经过 LLM extraction + dedup
6.4 优劣
✅ 优势:补足 LangGraph Store 缺的 extraction 层、和官方栈无缝 ❌ 劣势:相对新、文档还在演进、社区比 Mem0 小
7. Cognee / Supermemory — 其他玩家
7.1 Cognee
- 定位:LLM 驱动的 KG 构建工具(类似 Graphiti 但更研究风)
- 适合:需要灵活 ontology 的研究项目
7.2 Supermemory
- 定位:SaaS 化的极简 Memory API,REST 接口
- 适合:不想运维、希望”加 5 行代码就跑”的团队
7.3 其他
- mem0g(OMEGA)、MemoryOS、RamLama —— 横向竞品,各有取舍
- OpenAI Assistants API —— 平台级 memory,只在 OpenAI 内部用
8. 选型决策树
你的应用是?
│
├─ 个性化 chatbot / personal assistant
│ ├─ 需要快速接入,接受 LLM 调用成本 → Mem0
│ ├─ 需要长会话 + agent 状态持久化 → Letta
│ └─ 已有 LangGraph 栈 → LangGraph + LangMem
│
├─ 客服 / 多租户 SaaS
│ ├─ 需要审计、时间一致性强 → Zep + Graphiti
│ ├─ 强 user 隔离要求 → Mem0 with strong namespace
│ └─ 全自托管开源 → Mem0 / Letta
│
├─ 多 Agent 协作系统
│ ├─ 需要共享 KG → Graphiti / Cognee
│ └─ 隔离强(每 agent 独立) → Letta
│
├─ Code agent / SWE agent
│ ├─ Procedural memory 重要 → 自定义 prompt + LangMem prompt_optimizer
│ └─ 偶发使用 long context → ReadAgent 风格的 gist 摘要
│
├─ 研究 / KG 构建
│ └─ Cognee / Graphiti(较灵活)
│
├─ MVP / Hackathon
│ └─ Supermemory(免运维,REST 直调)
│
└─ 极致定制
└─ 自己用 LangGraph Store + 第 6 章的算子
8.1 横向对比表
| 框架 | 学习成本 | 部署复杂度 | LLM 调用成本 | 时间一致性 | 适合规模 |
|---|---|---|---|---|---|
| Mem0 | ★ | ★★ | 中-高 | ★★ | 中 |
| Letta | ★★★ | ★★★ | 高 | ★★ | 中-大 |
| Zep | ★★★ | ★★★ | 高 | ★★★★★ | 大 |
| LangGraph LT-Memory | ★★ | ★ | 低(无内置 extraction) | ★ | 小-大 |
| LangMem | ★★ | ★ | 中 | ★ | 中 |
| Cognee | ★★★ | ★★★ | 高 | ★★★ | 中 |
| Supermemory | ★ | 0(SaaS) | 不可见 | ★ | 小 |
✅ 自我检验清单
- 设计哲学辨析:能用一句话概括 Mem0 / Letta / Zep / LangGraph 各自的设计哲学
- Mem0 三层 Store:能解释为什么要 vector + graph + KV 三层
- Letta vs Mem0:能解释”OS-style agent runtime”和”memory SDK”的本质差异
- Zep 优势:能解释 bi-temporal 模型对哪类业务最关键
- LangGraph Store:能写出 namespace + key + value 的最简存取代码
- LangMem 三类 helper:能说出每个 helper 对应 CoALA 的哪类 Memory
- 选型决策:面对 5 个具体业务场景,能在 30 秒内给出推荐框架
- 成本权衡:能解释为什么”加 Memory 反而让 LLM 账单暴涨”(extraction 写入侧)
- 混合方案:能为某个复杂场景设计”用框架 A 做 X 部分 + 框架 B 做 Y 部分”的混合方案
📚 参考资料
官方文档与代码
- Mem0:github.com/mem0ai/mem0 | 文档
- Letta:github.com/letta-ai/letta | 文档
- Zep + Graphiti:github.com/getzep/graphiti | Zep 文档
- LangGraph Long-term Memory:官方文档 | Launch Blog
- LangMem:LangChain 官方
- Cognee:github.com/topoteretes/cognee
- Supermemory:supermemory.ai
横向对比
- 5 AI Agent Memory Systems Compared (2026):DEV Community
- Top 6 AI Agent Memory Frameworks for Devs (2026):DEV
- Best AI Agent Memory Frameworks in 2026:Atlan
- AI Agent Memory Systems in 2026 (Hermes OS Blog):博文
- State of AI Agent Memory 2026 —— Mem0:博文