把用户记忆变成可执行代码,让智能代理更懂你。
User as Code: Executable Memory for Personalized Agents
- 用可运行的Python代码存储用户状态和规则,实现统一表示与推理
- 在历史汇总问题上准确率达99%,远超传统检索方法(6-43%)
- 能自动检测过敏药物冲突等安全风险,适合高可靠性场景
个性化AI代理需要持久的用户记忆:一个跨多轮对话构建、每轮新对话都参考的用户模型。当前记忆多为无结构文本、知识图谱或扁平事实存储,通过检索匹配当前请求。这类“事实集合”记忆虽能召回单个事实,但因存储与行动分离,难以解决矛盾、聚合记录或强制规则。我们提出用户即代码(User as Code, UaC):将用户模型视为活的软件项目,用带类型的Python对象保存用户状态,普通函数定义其规则,使表示与推理在同一可执行介质中完成。核心是两阶段流水线:永不丢弃事实的追加日志,定期转为类型化代码。这改变了记忆能力。在标准长期对话基准上,UaC在召回率上达到78.8%(LOCOMO),媲美全上下文上限和最强基线。在聚合类问题上(如“去年我去了几次国际旅行?”),检索式记忆崩溃至6-43%,而UaC保持99%准确,因答案仅需对类型化状态的一行计算。由于规则在状态变化时确定性执行,UaC可主动提示如新药与旧过敏冲突等安全警报,这是查询驱动记忆无法提供的能力。
原文摘要 · Abstract (English)
A personalized AI agent needs a user memory: a persistent model of who the user is, built across many conversations and consulted on each new one. Today this memory is almost always stored as unstructured text, a knowledge graph, or a flat store of facts, and consulted by retrieval -- fetching the entries most similar to the current request. Such "bag-of-facts" memory recalls individual facts well, but because storing a fact and acting on it are separate steps, it struggles to resolve contradictions, aggregate over many records, or enforce rules. We argue that user memory should instead be executable. We introduce User as Code (UaC), a paradigm in which an agent's model of a user is a living software project: typed Python objects hold the user's state and ordinary Python functions encode the rules that govern it, so representing and reasoning about the user happen in one medium an interpreter can run. The enabling mechanism is a two-phase pipeline: an append-only log that never discards a fact, periodically checkpointed into typed code. This changes what memory can do. On standard long-term conversation benchmarks, UaC matches both a full-context upper bound and the strongest prior memory systems on recall (78.8% on LOCOMO). Its advantage emerges where representation matters most. On aggregate questions over a user's history -- "how many international trips did I take last year?" -- retrieval-based memory collapses (6-43%) while UaC stays near-perfect (99%), because the answer is a one-line computation over typed state rather than a search over text. And because its rules execute deterministically whenever the state changes, UaC can surface unsolicited, safety-critical alerts -- such as a newly prescribed drug that conflicts with an allergy recorded months earlier -- a capability query-driven memory cannot provide.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。