MemCodex: Self-Programming Hierarchical Memory for Language Agents

Xiaoqiang Wang, Bang LiuarXiv 2026

TL;DR

MemCodex is a self-evolving hierarchical memory system that allows language agents to "self-program" their own memory management logic. By iteratively rewriting the code for how it summarizes, indexes, and retrieves information across multiple layers, MemCodex achieves a 10.1% improvement in task success while using 3.4x fewer tokens and running 2.1x faster than state-of-the-art adaptive baselines.

What Problem Does This Paper Address?

Modern AI agents face a "heterogeneous access" problem. A simple query might only need a single piece of evidence from a recent conversation, while a complex multi-hop question might require synthesizing facts spread across months of history.

Current memory systems are often too rigid to handle this variety. They typically use predefined workflows—like flat vector databases or fixed summarization pipelines—that can't adapt to the specific needs of a task. While some "adaptive" methods exist, their design space remains limited to choosing between a few human-written modules. The authors argue that for agents to truly scale, they need a memory system that can evolve its own internal programs to optimize for both accuracy and computational efficiency.

Methods

The core of the paper is MemCodex, a hierarchical memory system composed of four layers: Skill (executable procedures), Graph (relational knowledge), Summary (compact text), and Raw (original conversation turns).

What makes MemCodex unique is its Program Evolution Loop:

  1. Fixed Interface: Each layer follows a strict five-method interface (admit, index, score, propose, route). This ensures that even as the internal logic changes, the layers can still communicate.
  2. Self-Modification: A "meta-agent" (an LLM) examines failed memory traces and rewrites the Python code for specific methods to fix observed errors. For example, it might rewrite a route method to better decide when to stop searching at the "Summary" level versus descending to "Raw" evidence.
  3. Acceptance Testing: New "child" architectures are evaluated against their "parents." Only those that improve accuracy or reduce token usage (without hurting accuracy) are kept in an archive.
  4. Query-Adaptive Read: At runtime, the agent traverses the hierarchy from coarse to fine. It learns to "Stop" once it has enough evidence, "Narrow" the search to specific source turns, or "Descend" to the next layer.
The MemCodex workflow showing the Task Loop and the Program Evolution Loop.

Datasets

The authors evaluated MemCodex using MemArena, a new unified evaluation suite they developed to benchmark heterogeneous memory systems. They tested on:

  • LoCoMo: A benchmark for long-term conversational memory.
  • LongMemEval (LME): Focused on long-context retrieval and reasoning.
  • MemoryAgentBench (MAB): Tests core competencies of memory-augmented agents.
  • PersonaMind: A new benchmark introduced in this paper that poses preference and procedure questions based on long-term user records.
The MemArena evaluation suite, providing a unified interface for diverse data sources and memory systems.

Key Insights and Ideas

  • Evidence Preservation is Critical: A major "aha" moment is the distinction between the content channel and the routing channel. Instead of just reading a lossy summary, MemCodex uses the summary to route the agent back to the exact raw turns that support it. This preserves ground-truth evidence while benefiting from high-level organization.
  • Hierarchy as a Filter: Adding layers (like Graph or Skill) doesn't necessarily increase context cost. Instead, it allows the agent to find answers at a higher level of abstraction, enabling it to stop early and avoid processing thousands of "raw" tokens.
  • Open-Ended Evolution Beats Manual Tuning: The meta-agent often discovers non-obvious rules. For instance, it might learn to check timestamps to see if a summary is "stale" before deciding to descend to raw data—a nuance a human designer might overlook.
Comparison of content vs. routing channels. Routing allows the agent to recover specific details (like "Paris") that might be omitted in a summary.

Key Results

MemCodex consistently outperformed both hand-designed and adaptive baselines:

  • Accuracy: It improved average task success by 10.1% relative to the strongest adaptive baseline.
  • Efficiency: It achieved this while using 3.4x fewer context tokens and delivering 2.1x faster inference.
  • Positioning: As shown in Figure 1, MemCodex occupies a unique position in the design space, combining open-ended search with a hierarchical organization to achieve the highest accuracy at the lowest latency.
MemCodex achieves the highest average accuracy with significantly lower latency compared to other memory systems.

Limitations and Future Work

The authors identify a few key areas for growth:

  • Fixed Runtime/Layers: While the code within the layers evolves, the set of layers (Skill, Graph, etc.) and the overall runtime architecture are currently fixed. Future work could involve evolving the entire hierarchical structure itself.
  • Task Specificity: Evolved architectures tend to be somewhat task-specific. While some logic transfers across benchmarks, the most significant gains come from tuning the memory programs to the specific distribution of questions in a given domain.
  • Deployment: The current study focuses on evolving on a held-out slice of data; moving toward continuous, "on-the-fly" evolution during live deployment remains an open challenge.

Summary updated

BibTeX
@misc{wang2026memcodex,
  author = {Xiaoqiang Wang and Bang Liu},
  title = {{MemCodex: Self-Programming Hierarchical Memory for Language Agents}},
  year = {2026},
  eprint = {2609.39765},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2609.39765},
}
Figure