arXiv(opens in a new tab)cs.CLSep 2026Cited by 1 paper on PaperVantage

Manuscript Offprint

arXiv:2609.39765

MemCodex: Self-Programming Hierarchical Memory for Language Agents

Xiaoqiang Wang, Bang Liu

Xiaoqiang Wang 1,2^{1,2}, Bang Liu 1,2^{1,2}

1^{1}Université de Montréal 2^{2}Mila – Quebec AI Institute

Agent memory faces heterogeneous access needs: a single-hop question may require one piece of evidence, whereas a multi-hop question must combine evidence from multiple sources. Predefined memory workflows cannot adapt to these varying needs. Recent adaptive methods search or learn over memory components and their compositions, but the design space itself remains predefined. We introduce MemCodex, a self-evolving hierarchical memory system that organizes experience into executable memory programs for summaries, relational knowledge, reusable skills, and latent memory. Open-ended program evolution searches the open design space of layer programs by rewriting how each layer is constructed, indexed, retrieved, and routed, thereby adapting both within-layer implementations and cross-layer composition. At query time, reads traverse the hierarchy from coarse to fine and stop once sufficient evidence is found, descending to the original history when needed. We further develop MEMARENA, a unified runtime that places heterogeneous data and memory systems behind a common interface. MemCodex improves average task success by 10.1% relative to the strongest adaptive-memory baseline, with 3.4× fewer context tokens and 2.1× faster inference.

Date: October 1, 2026

Correspondence: bang.liu@umontreal.ca

Université de Montréal

Mila

Figure

Memory organization

Latency per query (s)

Figure 1 Memory systems by memory organization and design search (left), and average test accuracy against latency per query (right).

1 Introduction

Language agents (Yao et al., 2022; Wang et al., 2024a; Liu et al., 2025; Ding et al., 2026) increasingly act over long horizons in web navigation (Zhou et al., 2023), software engineering (Jimenez et al., 2023; Hong et al., 2023) and computer use (Xie et al., 2024; Wang and Liu, 2025), and self-evolve from experience (Tao et al., 2024) through reflection on failures (Shinn et al., 2023), skill accumulation (Wang et al., 2023; Zhao et al., 2024; Ouyang et al., 2025; Shi et al., 2026) and code rewriting (Zhang et al., 2025b). Across sessions, much of this experience survives only in memory. Hand-designed pipelines organize it through rolling summaries in systems such as MemoryBank and MemGPT (Zhong et al., 2024; Packer et al., 2023), facts and entity graphs (Xu et al., 2025; Rasmussen et al., 2025; Edge et al., 2024), skill stores (Zhao et al., 2024; Ouyang et al., 2025; Wang et al., 2023), or memory written into parameters or latent state (Wang et al., 2024c; Behrouz et al., 2024; Li et al., 2025a; Wang et al., 2025a). Yet these pipelines face access heterogeneity, because the memory organization a query needs depends on the query, the record and the task (Xu et al., 2026; Lu et al., 2026; Huang et al., 2026). This heterogeneity appears across query types, since single-hop questions need raw turns (Han et al., 2026), multi-hop questions need cross-session links (Gutiérrez et al., 2024), temporal questions need dated events (Ge et al., 2025), and repeated procedures need skills (Wang et al., 2025c). Adaptive computation in reasoning shows the same pattern, where dynamic shortcuts spend depth only on the steps that need it (Wang et al., 2026b). A hand-designed pipeline hard-codes both which organizations exist and the rule that chooses among them, and a study of fourteen memory systems (Zhou et al., 2026) finds that no architecture dominates across tasks.

Adaptive memory addresses this rigidity in two main directions. One direction searches a predefined design space by selecting and composing modules for encoding, storage, retrieval, and management (Du et al., 2026; Lu et al., 2026; Zhang et al., 2025a). The other, exemplified by Mem-alpha, learns policies for when and how to write, update, and retrieve memory (Wang et al., 2025b, 2026a; Sun et al., 2026; Wang et al., 2026c). Despite their different forms, both directions must jointly determine how experience is organized into memory and how queries access it, since retrieval can only use structures created during writing. As shown in Figure 1, this coupling places memory systems along two axes: memory organization and design search. Among prior systems, only MemPro (Liu et al., 2026d) opens the design itself to program search, but it evolves a single monolithic program over flat memory, without a hierarchy whose components can evolve and be accessed independently.

We therefore introduce MemCodex, a hierarchical memory system whose layers are executable memory programs behind a fixed interface and runtime. Program evolution adapts both the write and read code of individual layers and their cross-layer composition, that is, which layers are active and how items and reads pass between them. Within this hierarchy, MemCodex organizes SKILL, GRAPH, and SUMMARY above an append-only raw layer, with KV MEMORY providing latent memory.

Inspired by the Darwin Gödel Machine (Zhang et al., 2025b) and HyperAgents (Zhang et al., 2026b), which evolve an agent by rewriting its own code, we apply program evolution to this memory hierarchy. At each round, a meta agent draws a parent from the archive and rewrites one method of one layer or toggles one layer. Restricting each child to one change makes the effect of that change attributable. An acceptance test then keeps changes that improve accuracy or reduce token use without degrading the other, and accuracy-only gains must additionally pass a one-sided McNemar test (McNemar, 1947). Before this test, validity checks K reject changes that break the memory constraints.

The fixed interface exposes five methods in each layer, spanning both memory writing and reading. On the write side, admit filters candidates, index keeps a bounded active set, and propose derives

Figure

Figure 2 MemCodex workflow. In the task loop (upper), MEMARENA writes the record and poses each query q to the task agent, which reads a context C from the layer hierarchy, stopping at the coarsest sufficient layer or descending to source turns, and answers a. In the program evolution loop (lower), run once per round, a parent is sampled from the archive of architectures g=(λ,ψ)g = (\lambda, \psi), meta agent ρ\rho rewrites one method of one layer program ψℓ\psi_\ell, and validity checks and the acceptance test decide whether the child enters the archive.

items for the layer above. On the read side, score ranks candidates and route decides where the search proceeds next. Reads move from coarse to fine layers, with route choosing STOP, NARROW, or DESCEND until sufficient evidence is found or the search reaches the raw history. Because every stored item retains its exact source turns, the original evidence remains recoverable even after lossy summarization.

To evaluate these mechanisms systematically across diverse sources, interaction formats, and evaluation protocols, we further develop MEMARENA, a heterogeneous agent-source evaluation suite with a unified data interface, access interface, and metric suite covering benchmarks such as LoCoMo (Maharana et al., 2024), LongMemEval (LME) (Wu et al., 2025), and MemoryAgentBench (MAB) (Hu et al., 2025b). It separates the memory system from the task agent, as capability-level evaluation dissociates what a language model knows from how it reasons (Wang et al., 2024b). Under one protocol, MEMARENA spans seven sources, eleven baseline systems and two backbones, while each new source requires only one adapter. Under this evaluation, MemCodex gains 5.6 average accuracy points over the strongest baseline with Qwen3.5-27B (Qwen Team, 2026) and 3.6 with the API model GPT-5.4-mini, while reading 3.4× fewer tokens and answering 2.1× faster.

References (100)

Couldn't load all references. Reload the page to try again.