Manuscript Offprint

arXiv:2610.19472

MemCode: Discrete Semantic Representations for Long-Term Agent Memory

Xin Li, Liang Hu, Duoqian Miao, Ming Peng, Qi Zhang

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Long-term memory remains a fundamental bottleneck for large language model (LLM) agents across sessions. Existing systems typically adopt a write-then-retrieve paradigm, storing context-dependent dialogue fragments and retrieving them via nearest-neighbor search in continuous embedding space. This design yields unstable access units and systematically misses logically related but lexically distant evidence. We propose MemCode, a long-term agent memory framework that couples retrieval-oriented writing with discrete associative retrieval. MemCode rewrites dialogue into self-contained atomic memory units (AMUs), maps AMUs into a learnable codebook with isomorphic semantic quantization (ISQ), and uses codebook co-occurrence topology as a retrieval path complementary to vector search. For long-term memory stores, MemCode separates online atomic writes from asynchronous offline consolidation. On LongMemEval-S and LoCoMo with GPT-4o-mini and Qwen3-30B-A3B-Instruct-2507 backbones, MemCode achieves the best overall accuracy while keeping per-dialogue memory construction within roughly 40130 seconds, making it 321× faster than LightMem and over an order of magnitude faster than A-MEM and MemoryOS.

1 Introduction

Large language model (LLM) agents are rapidly evolving from single-turn systems into persistent assistants that interact with users over extended sessions [1, 2]. However, neither model parameters nor context windows provide a satisfactory substrate for this need. Parameters are largely static after training, while long contexts incur quadratic attention costs [3] and exhibit degraded utilization as sequences grow [4]. It fundamentally elevates the role of agent memory that, distinct from both parameters and context, must accumulate, organize, and recall information across sessions to support personalization, consistency, and long-horizon reasoning.

As a result, external memory has emerged as a central component for agent systems. Most memory systems follow a retrieval-augmented generation (RAG) paradigm [5, 6]: past interactions are written into storage, retrieved at query time, and re-injected into the model context. In this setting, two design choices are tightly coupled: 1) what to write, and 2) how to retrieve it. Prior systems address these choices primarily from memory persistence. Mem0 [7], LangMem [8], and A-MEM [9] improve writing and organization through Create, Read, Update, and Delete (CRUD) operations, typed memories, and evolvable note networks; MemoryOS [10] and LightMem [11] improve scalability through scheduling and compression. However, we argue that the key bottleneck for RAG-style long-term memory is not storage scale alone, but jointly modeling what to write and how to retrieve it, as illustrated in Figure 1.

This coupling becomes sharper as conversations grow. A memory item may be easy to store but expensive to use if it lacks the necessary context, such as missing speakers or times. Conversely,

∗^{*}Corresponding Author

40th Conference on Neural Information Processing Systems (NeurIPS 2026).

Figure

Figure 1: Traditional memory pipelines store raw dialogue fragments and retrieve them primarily by surface similarity. Ours rewrites dialogue into atomic memory units and retrieves them through complementary vector and codebook paths.

a compressed summary might answer broad questions but can erase the factual hooks needed for multi-hop recall. Our goal is to create independently retrievable memory units that maintain pointers to raw dialogue, avoiding irreversible abstraction bottlenecks.

Specifically, the writing pipeline is retrieval-oriented memory modeling, focusing on creating retrieval-accessible targets that remain understandable and indexable after separation from the original dialogue. Context-dependent fragments are weak storage units; instead, atomic facts are desirable. Long-term recall is usually cued by events and relations, not exact wording. In addition, the retrieval issue revolves around access representation. Most systems use continuous vectors for memory, retrieving content via top-K nearest-neighbor searches [12], as illustrated by the upper pipeline in Figure 1. This works for close memories in surface semantics but inherits the geometric bias of continuous representations: logically related but lexically distant content may be missed, consistent with evidence that dense retrievers can be brittle when retrieval conditions shift across tasks [13]. In contrast, human memory distinguishes itself through pattern separation, allowing the hippocampus to encode similar continuous inputs into discrete traces for associative recall of partial cues [14, 15]. Systems that just mimic human multi-level memory without discrete anchors may still misalign architecture and retrieval.

Our proposed MemCode retains the RAG-style pipeline while adding a discrete representation layer alongside continuous vectors, utilized throughout writing, retrieval, and long-term maintenance. During writing, we employ LLM-based ingestion and abstraction to transform dialogue into self-contained AMUs with source pointers. Each AMU serves as a minimal semantic unit that can be independently retrieved. Independent semantic quantization (ISQ) anchors each AMU to a discrete symbolic state within a learnable codebook. During retrieval, global codebook co-occurrence offers an alternative access path for logically related yet geometrically distant memories, while source pointers enhance context reconstruction. MemCode also separates online writing from offline consolidation, allowing clustering and synthesizing AMUs without disrupting real-time retrieval.

Contributions. Our contributions are fourfold. (1) We propose MemCode, a unified write-index-retrieve-consolidate framework that addresses the write-retrieve mismatch in long-term agent memory by organizing memory around atomic facts and discrete representations. (2) We introduce AMUs as the retrieval unit of this framework, turning raw dialogue into self-contained and source-grounded facts. (3) We pair AMUs with ISQ and codebook topology, giving each fact both a continuous vector interface and a discrete associative path for evidence beyond local embedding neighborhoods. (4) On LongMemEval-S, MemCode reaches 78.40%/74.60% Overall with GPT-4o-mini/Qwen3 after asynchronous consolidation; on LoCoMo it reaches 79.55%/76.88%, while keeping construction runtime roughly 3~21× faster than LightMem and over an order of magnitude faster than A-MEM and MemoryOS.

References (42)