Agent Memory第 2 篇,共 2 篇
记忆机器的四个部件
agent memory 要在模型和窗口之外留住 agent 自己的经历,任何记忆系统都要回答两类问题:存下来的东西长什么样,以及怎样存进去、怎样随时间整理、怎样取回来。前一类交给存储 S,后一类交给形成 F、演化 E、检索 R 三个操作。存储按单位、组织、存放方式和索引展开,三个操作各自展开成几个设计选择
按 agent memory 的定义,记忆是 agent 自己经历的记录,存在模型和窗口之外,跨会话保留,需要时再放回窗口。要做成一个系统,有两类问题躲不掉。数据问题:存下来的东西长什么样,一条记录是什么,记录之间怎么组织。操作问题:什么东西、以什么形态存进去,存进去之后怎样随时间整理,需要时怎样取回来。
这一篇把记忆系统拆成四个部件回答这两类问题:一个存储 S,三个操作形成 F、演化 E、检索 R。读完以后,任何一个记忆系统都可以填进这四格,看它把力气花在哪一格。只讨论显式记录,也就是一条条能单独查看、修改的记录;参数和模型内部状态形式的记忆属于 LLM memory 的做法,不在这里。
- 存储 S:存下来的东西本身,回答数据问题。
- 形成 F:从窗口到存储的那根箭头,决定什么值得留、写成什么样。
- 演化 E:存储内部的变化,把新写进来的记录和已有的整合,合并、改写、删除。
- 检索 R:从存储回到窗口的那根箭头,决定取哪些、带多少、放在窗口的哪里。
三个操作和 agent 记忆综述里的划分一致:形成把这一步产生的东西有选择地变成记忆候选,而不是原样存下整段历史;演化把候选并进已有的记忆;检索按任务构造查询、取回相关内容11 Hu 等 Memory in the Age of AI Agents. 2025.原文:“selectively transforms these artifacts into memory candidates, extracting information with potential future utility rather than storing the entire interaction history verbatim.” “Formed memory candidates are integrated into the existing memory base through an evolution operator” “denotes a retrieval operator that constructs a task-aware query and returns relevant memory content.” 。
用 给推理步编号, 是这一步新进来的内容:用户的新消息、工具返回的结果、模型自己上一步的输出。四个部件写成式子:
可以什么都不写,原样返回 ; 在大多数步上可以什么都不做。 是检索取回的一组记录, 是查询。 把取回的记录和最近几步的原始内容拼成窗口 ,总量不超过预算 。 是模型,不属于记忆系统;但它的输出 会成为 的一部分,再经过 。
熟悉数据库的话可以拿 CRUD 对一下:F 是带加工的 Create,R 是带排序和预算的 Read,Update 和 Delete 被收进了 E,S 是 CRUD 不讨论的 schema。差别在于 CRUD 的操作都由调用方发起、按键精确命中;这里 F 要自己决定存什么,R 是模糊检索。
存储要定四件事:一条记录是什么,记录之间怎么组织,用什么来存,建哪些索引。
常见的单位有六种。
- 原文片段:一条消息,或一段固定长度的切块,原样保存。HippoRAG 把调用方传入的一段原文就当作一条记录;Zep 把原始消息存成 episode,作为其他记录的出处。原文保真,但同一件事散在好几处,检索命中的是措辞,不是事实。
- 事实句:从对话里抽出的一句自包含的陈述。Mem0 的单位就是一句不依赖上下文也能读懂的事实;Hindsight 抽的是带时间、人物、原因的叙事事实。事实句密,但抽取决定了丢掉什么,原话的语气和上下文都没了。
- 实体与关系:节点是实体,边是实体之间的关系。Zep 的单位是一条事实边,两端各一个实体,边上写一句事实;HippoRAG 另存短语和三元组。可以顺着关系找,适合多跳的问题;代价是建图贵,实体消解出错会把两个人合成一个。
- 摘要:一段对话、一个实体或一组记录的概括。Zep 给每个实体维护一段摘要,Supermemory 把事实聚合成一份用户画像。摘要省窗口,但细节丢了,底下的记录一变,摘要就得重写。
- 文件:一整份可读可改的文档。Letta Code 的单位是一个 Markdown 文件;Claude Code 的自动记忆也是一组 Markdown 文件,每次会话开始时把 MEMORY.md 的前 200 行整份放进窗口22
Claude Code 文档,How Claude remembers your project,2026-09-29 访问。原文:“The first 200 lines of
MEMORY.md, or the first 25KB, whichever comes first, are loaded at the start of every conversation.” “Claude keepsMEMORY.mdconcise by moving detailed notes into separate topic files.” 。人和模型都能直接编辑,但单位粗,读的时候往往整份带上。 - 经验:从过去的任务里留下的做法。按抽象程度分三层:几乎不加工的案例,供直接照做;提炼出的策略,供规划时参考;编译成函数或 API 的技能,可以直接执行33 Hu 等 Memory in the Age of AI Agents. 2025.原文:“We categorize existing works based on the abstraction level of stored knowledge: Case-based Memory preserves raw records for direct replay, Strategy-based Memory distills abstract heuristics for planning, and Skill-based Memory compiles executable capabilities for action.” 。
多数系统不只用一种单位。Zep 同时存原始消息、事实边、实体摘要和社区;Hindsight 在事实之外还留着原文层。几种单位怎么放在一起,就是组织方式的问题。
按记录之间有没有显式的关系、有几层,分三种44 Hu 等 Memory in the Age of AI Agents. 2025.原文:“Flat Memory stores information as accumulations of discrete units, without explicitly modeling semantic or relational dependencies among them.” “Planar Memory introduces an explicit organizational topology among memory units, but only within a single structural layer” “Hierarchical memory organizes information across layers, using inter-level connections to shape the memories into a volumetric structured space.” :
- 平铺(1D):记录一条条堆着,彼此之间没有显式的关系。Mem0 的事实之间没有层级,也没有摘要层;MemOS 默认也是扁平的。写入最便宜,检索只能靠相似度,一条记录和另一条有什么关系,存储里看不出来。
- 平面(2D):记录之间有显式的关系,比如图、树、表,但只有一层,所有记录的抽象程度相同。查找时可以顺着关系走,但只能在这一层里走。Letta Code 的文件按目录组织,根目录和最多两级子目录,靠每一级的索引文件串起来。
- 层级(3D):多层结构,上下层的抽象程度不同,从原始观察到事件摘要,再到更高层的主题,层与层之间相连。查找时既能在同一层里走,也能上下走,先在高层找到相关的主题,再往下找到具体的记录55 Hu 等 Memory in the Age of AI Agents. 2025.原文:“Cross-layer connections further yield a volumetric memory space through which the system can navigate not only laterally among units but also vertically across abstraction levels.” “Pyramid This category constructs memory as multi-level pyramids, where information is progressively organized into higher layers of abstraction and queried in a coarse-to-fine manner.” 。Zep 是原始消息、实体与事实边、社区三层;HippoRAG 用一层知识图谱做索引,底下是段落存储,先在图上走,再落到段落66 Hu 等 Memory in the Age of AI Agents. 2025.原文:“Extending the idea of clustering memory nodes, Zep … formalizes agent memory as a Temporal Knowledge Graph, and it similarly performs community partitioning.” “Biologically inspired architectures such as HippoRAG … factor memory into an associative indexing component, implemented as an open knowledge graph, and an underlying passage store, using the graph layer to orchestrate multi-hop retrieval over stored content.” 。
层数越多,查找的路越多,维护的成本也越高:底层的一条事实变了,上面的摘要和社区要不要跟着重写,就成了演化要回答的问题。
组织形式是逻辑上的,落到磁盘上还有一层选择:用什么来存。同样是一张图,Zep 存在图数据库里,VoiceMem 存成实体表、关系边表和链接表几张表,HippoRAG 整张图放在内存里。常见的存法有五种。
- 文本文件:记录就是磁盘上的文件,人能直接打开来读、来改。Letta Code 的记忆是一个 git 仓库里的 Markdown 文件树,模型改完文件自己执行 git 提交,每次提交留一条历史;对话历史另按会话存成逐行追加的 JSONL。
- 向量库:每条记录带一个向量,库的主要能力是按相似度找。Mem0 的事实存在向量库里;VoiceMem 把事实也放在 Mem0 的向量库里,只把它当向量库加元数据用。
- 图数据库:节点和边是一等公民,边上可以带属性,适合顺着关系走。Zep 把原始消息、实体、事实边和社区都放在同一个图数据库里,默认是 Neo4j;MemOS 的节点在图库里,向量默认放在图库之外的向量库里。
- 关系表:记录、实体、关联各占一张表,用外键或关联表连起来。Hindsight 的事实、实体、事实与实体的关联、observation 都是数据库表;VoiceMem 的认知图也是表:实体表、关系边表、实体到事实的链接表。图结构不一定要用图数据库存。
- 内存结构:整个存储在进程内存里,定期序列化到磁盘。HippoRAG 的三张向量表加一张无向图都在内存里,图以 pickle 落盘。
多数系统不止用一种。MemOS 是图库加向量库,VoiceMem 是向量库加 sqlite,Letta Code 是文件加 JSONL。
存法决定数据放在哪里,索引决定检索能怎么找。同一份存储,有没有某种索引,决定了某一种找法是快、是慢,还是根本做不到。
- 向量索引:按语义相似找近邻。有的系统建了近似最近邻索引,比如 Hindsight 在写入之后提交一个后台任务维护向量索引;有的只存向量、不建索引,Zep 把向量直接存在边上,相似度在查询语句里逐条算,HippoRAG 默认整表读进内存做点积。规模小时差别不大,规模一大,没有索引就得整表扫。
- 全文索引:按关键词找,通常配 BM25 打分。Zep 在关系名和事实句上建了全文索引;Hindsight 给每条事实留一列全文检索用的信号,放实体名和日期词;Mem0 存一份词形还原后的副本,专门用来做关键词匹配。HippoRAG 和 Letta Code 都没有全文索引。
- 实体索引:从一个名字找到提到它的记录。Mem0 有一张实体表,每个实体记着它出现在哪些事实里。它是从实体到事实的倒排索引,不是图:实体之间没有关系边,只能从名字找到事实,不能从一个实体走到另一个实体。
- 结构索引:按目录、层级或链接找。Letta Code 每一级子目录都有一份索引文件,里面是给模型读的普通相对链接;这是它唯一的索引,没有向量、全文或范围索引,找记忆靠模型自己翻目录。
图结构和索引容易混在一起。图是记录之间的关系本身,存的是知识;索引是为了找得快而另建的查找表,删掉它不丢信息,只是变慢。Mem0 的实体表是索引,Zep 的事实边是图。
索引不是越多越好。每多一种索引,写入时就多一处要同步更新,后台还要多一份维护工作;不建索引的存储写入便宜,规模小时也够用。
形成决定两件事:什么值得留,写成什么样。它不是把交互历史全部记下来,而是挑出将来可能有用的信息,压成紧凑的记录77 Hu 等 Memory in the Age of AI Agents. 2025.原文:“selectively transforms these artifacts into memory candidates, extracting information with potential future utility rather than storing the entire interaction history verbatim.” 。原始信息又长又杂,怎么抽是关键88 Zhang 等 A Survey on the Memory Mechanism of Large Language Model based Agents. 2024.原文:“Previous research indicates that designing the strategy of information extraction during the memory writing operation is vital” “This is because the original information is commonly lengthy and noisy.” 。
对显式记录来说,写法有四种,加工程度依次加深:
- 原样存:不加工,消息或切块直接入库。
- 语义摘要:把冗长的原始内容压成摘要,保留整体意思99 Hu 等 Memory in the Age of AI Agents. 2025.原文:“Semantic Summarization … transforms lengthy raw data into compact summaries” “Knowledge Distillation … extracts specific cognitive assets, ranging from factual details to experiential planning strategies.” “Structured Construction … organizes amorphous source data into explicit topological representations, such as knowledge graphs or hierarchical trees” 。
- 知识蒸馏:抽出具体的知识,从事实细节到做事的策略。Mem0 用一次模型调用把一段对话抽成若干条事实句;Hindsight 让模型从一块原文里抽出叙事事实,只留值得长期记住的。
- 结构化构建:把零散的内容组织成图或树这类显式结构。Zep 一次写入要过抽实体、实体消解、抽边、边消解、失效、摘要六关;HippoRAG 离线让模型从每段原文抽出实体和三元组,拼成一张图。
加工可以同步做,也可以分两步。MemOS 先把一段消息原样存成一个原文节点,请求立刻返回;后台再用模型把它抽成事实句,删掉原文节点。
形成里还藏着一条回路。 会进入 ,再经过 :被取回的一条记忆,经模型复述一遍,复述又被抽成新事实存回去,内容相近、措辞不同、时间更新的第二条就这样出现了。判重时看到的旧记忆通常只有最相近的几条,不保证能把两条对上。堵这条回路的位置在形成:只抽用户说的话,或者给每条记录标上说话人,把助手的话存成另一种类型。
新记录如果只是追加进库,会忽略条目之间的依赖和矛盾,也不管信息是否过时1010 Hu 等 Memory in the Age of AI Agents. 2025.原文:“A naive strategy is simply appending new entries to the existing memory bank.” “However, it overlooks the semantic dependencies and potential contradictions between memory entries and neglects the temporal validity of information.” “Memory Consolidation … merges new and existing memories and performs reflective integration, forming more generalized insights.” “Memory Updating … resolves conflicts between new and existing memories, correcting and supplementing the repository to maintain accuracy and relevance.” “Memory Forgetting … removes outdated or redundant information, freeing capacity and improving efficiency.” 。演化有三种:
- 巩固:把相关的新旧记录合并,提炼出更一般的结论。Hindsight 在后台把一批未整合的事实合成更高层的观察;Letta Code 在压缩之后派一个后台的 reflection 子 agent 去整理记忆文件。
- 更新:解决新旧记录之间的冲突,修正和补充。Zep 在写入路径上同步判冲突:新边和旧边矛盾时,旧边被标上失效时间,不删除。
- 遗忘:删掉过时或冗余的记录。Mem0 的事实可以带到期日,到期后读取时被隐藏,但仍留在库里;MemOS 有容量上限。
演化在哪里做,各系统差别很大:Zep 挂在写入路径上同步做,Hindsight 和 MemOS 在后台做,Mem0 几乎不做,改写和删除都留给应用手动调用。
式子里 的第二个参数 ,是检索影响存储的唯一通道。多数系统的演化不看它;少数系统让命中过的记录升温、久不命中的慢慢衰减。人类记忆研究里,这对应一次成功的提取会让记忆变强。
检索按执行顺序分四步1111 Hu 等 Memory in the Age of AI Agents. 2025.原文:“Retrieval Timing and Intent … determines the specific moments and objectives for memory retrieval, shifting from passive, instruction-driven triggers to autonomous, self-regulated decisions.” “Query Construction … bridges the semantic gap between the user’s raw input and the stored memory index by decomposing or rewriting queries into effective retrieval signals.” “Retrieval Strategies … executes the search over the memory repository, employing paradigms ranging from sparse lexical matching to dense semantic embedding and structure-aware graph traversal.” “Post-Retrieval Processing … refines the retrieved raw fragments through re-ranking, filtering, and aggregation, ensuring that the final context provided to the model is concise and coherent.” :
- 时机与意图:什么时候查,查哪个库。做法从每轮都查,到由模型自己决定。Mem0 和 Zep 都由应用按需调用,不会每轮自动检索;Letta Code 让模型用读文件的工具自己翻子目录。
- 查询构建:把用户的原话变成适合检索的查询 ,可以分解,也可以改写。多数系统直接拿用户最后一句话当查询,但「规划成都行程」永远查不到「我吃素」:长期约束要用上, 得来自当前任务,而不是用户的措辞。
- 检索策略:执行搜索,有词法匹配、语义向量、图遍历,以及几种的混合。Mem0 用向量召回候选,再用关键词和实体匹配调整排序;Zep 把关键词和向量两路结果按名次融合;HippoRAG 把问题映射成图上的起点,跑一次 Personalized PageRank 给段落排序。
- 后处理:对取回的记录重排、过滤、聚合。MemOS 用交叉编码器重排、按阈值过滤、再去重;Hindsight 也用交叉编码器重排,返回结果按 token 预算截断。
最后一步是拼窗口: 决定取回的记录放在窗口的哪里、带多少。预算可以按条数算,也可以按 token 算;放进去的可以只是正文,也可以连带时间和来源。
框架不包含什么
Section titled “框架不包含什么”两样东西故意留在框架外面。
模型本身。 长上下文、KV cache、权重里的知识,都是在改 ,不是改四个部件。参数和内部状态形式的记忆属于 LLM memory 的做法。
agent 产品的管线。 指令文件、技能、压缩触发、hooks,是记忆在 agent 产品内部的装法;这一篇只看独立的记忆系统。
-
Hu 等 Memory in the Age of AI Agents. 2025. 原文:“selectively transforms these artifacts into memory candidates, extracting information with potential future utility rather than storing the entire interaction history verbatim.” “Formed memory candidates are integrated into the existing memory base through an evolution operator” “denotes a retrieval operator that constructs a task-aware query and returns relevant memory content.” ↩
-
Claude Code 文档,How Claude remembers your project,2026-09-29 访问。原文:“The first 200 lines of
MEMORY.md, or the first 25KB, whichever comes first, are loaded at the start of every conversation.” “Claude keepsMEMORY.mdconcise by moving detailed notes into separate topic files.” ↩ -
Hu 等 Memory in the Age of AI Agents. 2025. 原文:“We categorize existing works based on the abstraction level of stored knowledge: Case-based Memory preserves raw records for direct replay, Strategy-based Memory distills abstract heuristics for planning, and Skill-based Memory compiles executable capabilities for action.” ↩
-
Hu 等 Memory in the Age of AI Agents. 2025. 原文:“Flat Memory stores information as accumulations of discrete units, without explicitly modeling semantic or relational dependencies among them.” “Planar Memory introduces an explicit organizational topology among memory units, but only within a single structural layer” “Hierarchical memory organizes information across layers, using inter-level connections to shape the memories into a volumetric structured space.” ↩
-
Hu 等 Memory in the Age of AI Agents. 2025. 原文:“Cross-layer connections further yield a volumetric memory space through which the system can navigate not only laterally among units but also vertically across abstraction levels.” “Pyramid This category constructs memory as multi-level pyramids, where information is progressively organized into higher layers of abstraction and queried in a coarse-to-fine manner.” ↩
-
Hu 等 Memory in the Age of AI Agents. 2025. 原文:“Extending the idea of clustering memory nodes, Zep … formalizes agent memory as a Temporal Knowledge Graph, and it similarly performs community partitioning.” “Biologically inspired architectures such as HippoRAG … factor memory into an associative indexing component, implemented as an open knowledge graph, and an underlying passage store, using the graph layer to orchestrate multi-hop retrieval over stored content.” ↩
-
Hu 等 Memory in the Age of AI Agents. 2025. 原文:“selectively transforms these artifacts into memory candidates, extracting information with potential future utility rather than storing the entire interaction history verbatim.” ↩
-
Zhang 等 A Survey on the Memory Mechanism of Large Language Model based Agents. 2024. 原文:“Previous research indicates that designing the strategy of information extraction during the memory writing operation is vital” “This is because the original information is commonly lengthy and noisy.” ↩
-
Hu 等 Memory in the Age of AI Agents. 2025. 原文:“Semantic Summarization … transforms lengthy raw data into compact summaries” “Knowledge Distillation … extracts specific cognitive assets, ranging from factual details to experiential planning strategies.” “Structured Construction … organizes amorphous source data into explicit topological representations, such as knowledge graphs or hierarchical trees” ↩
-
Hu 等 Memory in the Age of AI Agents. 2025. 原文:“A naive strategy is simply appending new entries to the existing memory bank.” “However, it overlooks the semantic dependencies and potential contradictions between memory entries and neglects the temporal validity of information.” “Memory Consolidation … merges new and existing memories and performs reflective integration, forming more generalized insights.” “Memory Updating … resolves conflicts between new and existing memories, correcting and supplementing the repository to maintain accuracy and relevance.” “Memory Forgetting … removes outdated or redundant information, freeing capacity and improving efficiency.” ↩
-
Hu 等 Memory in the Age of AI Agents. 2025. 原文:“Retrieval Timing and Intent … determines the specific moments and objectives for memory retrieval, shifting from passive, instruction-driven triggers to autonomous, self-regulated decisions.” “Query Construction … bridges the semantic gap between the user’s raw input and the stored memory index by decomposing or rewriting queries into effective retrieval signals.” “Retrieval Strategies … executes the search over the memory repository, employing paradigms ranging from sparse lexical matching to dense semantic embedding and structure-aware graph traversal.” “Post-Retrieval Processing … refines the retrieved raw fragments through re-ranking, filtering, and aggregation, ensuring that the final context provided to the model is concise and coherent.” ↩