Memory as code boosts long term memory retrieval in llm agents
Memory as Code: Declarative Retrieval over Codified Long-Term Memory
Computation and Language
Summary
Long-term memories help AI agents remember past information to answer questions better. The authors noticed that current systems struggle when memories are spread over many sessions and change over time. They introduced Memory as Code (MaC), which treats memories like pieces of computer code that can be built and queried more flexibly. Their method improved accuracy by up to about 16% and used fewer resources in tests. This approach helps AI better understand and organize complex, long-range information.
What this means in practice
- •For ai system developers: Implement improved memory modules that handle evolving facts over time for more accurate AI reasoning.
- •For chatbot platform teams: Create chatbots that maintain coherent long-term context across multiple conversations by using code-like memory structures.$Commercial implications: Enables offering more context-aware and accurate chatbot services to customers by applying advanced memory retrieval techniques.
Authors
Aoting Zeng, Yuxin Du, Jianwei Wang, Yizhang He, Shan Jiang, Xuemin Lin, Kai Wang
Abstract
In long-term memory systems, memories are constructed from interaction histories and retrieved in response to queries, enhancing the reasoning ability of LLM agents. In complex scenarios, facts are scattered across sessions and their relationships evolve over time, making it difficult for existing memory systems to compose them to adapt to diverse queries. Motivated by the modularity and composability of code, we propose Memory as Code (MaC), a unified memory framework that jointly designs memory construction and retrieval through a shared code abstraction. MaC couples codified construction, which establishes executable memory abstractions, with declarative retrieval, which translates queries into structured retrieval programs. Specifically, codified construction builds a three-layer executable memory structure that progresses from an atomic function layer, through a relational layer, to a modular composition layer. Declarative retrieval recasts retrieval from relevance-based fact selection into declarative query processing over memory. Experiments on LoCoMo and LongMemEval show relative gains of up to 15.91% in overall LLM-judge accuracy over the strongest baseline in each setting, while substantially reducing token consumption, highlighting the effectiveness of memory as code for long-horizon memory tasks.