MemCode: Discrete Semantic Representations for Long-Term Agent Memory
Organizations: Tongji University · WorldMind
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 a 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 times faster than LightMem and over an order of magnitude faster than A-MEM and MemoryOS.
Figures & tables
| Method | Runtime | Total Tokens | Calls | Overall | Temporal | Multi-Session | Know.-Upd. | Single-User | Single-Asst. | Single-Pref. |
|---|---|---|---|---|---|---|---|---|---|---|
| (s) | (k) | ACC (%) | ( ) | ( ) | ( ) | ( ) | ( ) | ( ) | ||
| GPT-4o-mini | ||||||||||
| Full Text | - | - | - | 56.80 | 31.58 | 45.45 | 76.92 | 87.14 | 89.29 | 36.67 |
| Naive RAG | - | - | - | 61.00 | 39.85 | 48.48 | 67.95 | 90.00 | 98.21 | 53.33 |
| LangMem | 2293.70 | 1102.16 | 520.62 | 37.20 | 15.79 | 20.30 | 66.67 | 60.00 | 46.43 | 60.00 |
| A-MEM | 5132.06 | 1605.81 | 986.55 | 62.60 | 47.36 | 48.87 | 64.11 | 92.86 | 96.43 | 46.67 |
| Method | Runtime | Total Tokens | Calls | Overall | Multi | Open | Single | Temp |
|---|---|---|---|---|---|---|---|---|
| (s) | (k) | (ACC %) | ( ) | ( ) | ( ) | ( ) | ||
| GPT-4o-mini | ||||||||
| Full Text | - | - | - | 73.83 | 68.79 | 56.25 | 86.56 | 50.16 |
| Naive RAG | - | - | - | 63.64 | 55.32 | 47.92 | 70.99 | 56.39 |
| A-MEM | 6060.73 | 1149.43 | 1175.47 | 64.16 | 56.03 | 31.25 | 72.06 | 60.44 |
| MemoryOS (eval) | 2422.05 | 287.00 | 553.45 | 58.25 | 56.74 | 45.83 | 67.06 | 40.19 |
| Retrieval path | Without consolidation | With consolidation |
|---|---|---|
| Dense only | 59.57 (168) | 65.25 (184) |
| Dense + codebook | 64.89 (183) | 69.15 (195) |
| Method | Evidence budget | Overall accuracy (%) |
|---|---|---|
| Dense-20 | 20 | 73.18 |
| Dense-40 | 40 | 74.61 |
| Dense-20 + BM25-20 | 20+20 | 76.75 |
| Dense–sparse hybrid + reranker | 40 | 76.36 |
| HyDE + reranker [ 40 ] | 40 | 76.69 |
| Dense-20 + codebook-20 (MemCode) | 20+20 | 78.12 |
Appendix figures & tables12 assets
Supplementary material from the paper’s appendix.
Appendix
| Stage | Use | Hardware |
|---|---|---|
| Memory runtime | Ingestion, retrieval, consolidation, and benchmark evaluation | 12th Gen Intel Core i5-12500H @ 3.10GHz; NVIDIA GeForce RTX 3050 Ti Laptop GPU, 4 GB; 16 GB RAM |
| Offline ISQ training | VQ-VAE codebook learning before memory evaluation | 12 vCPU Intel Xeon Platinum 8352V @ 2.10GHz; one 32 GB vGPU; 90 GB RAM |
| Block | Item | Setting |
|---|---|---|
| Data | Training pairs | DailyDialog context–response pairs; the context contains the two previous utterances |
| Data | Sequence length | Source and target sequences are truncated or padded to 128 tokens |
| Model | Base encoder | T5-base encoder; code dimension equals the T5 hidden size |
| Model | Projection and codebook | Stride-1 Conv1d projection, kernel size 3, padding 1; 1024 Euclidean code vectors |
| Optimization | Optimizer and schedule | AdamW, weight decay 0.01; batch size 16; gradient accumulation 2; 10 epochs; cosine schedule with 10% warmup; gradient clipping at 1.0 |
| Optimization | Learning rates | for T5 parameters; for VQ, projection, and decoder-side parameters |
| Dataset | Ingestion unit | Extraction background | Dense path | Codebook path | Final evidence budget |
|---|---|---|---|---|---|
| LoCoMo | A–B dialogue turn | 10 prior sentences | 20 AMUs | 20 AMUs | 40 AMUs + 25 raw dialogue turns |
| LongMemEval-S | User–Assistant turn | 5 prior sentences | 20 AMUs | 20 AMUs | 40 AMUs + 10 raw dialogue turns |
| Hyperparameter | Value | Accuracy (%) | vs. default |
|---|---|---|---|
| 1 | 77.40 | -0.72 | |
| 3 (default) | 78.12 | +0.00 | |
| 5 | 77.47 | -0.65 | |
| 7 | 77.47 | -0.65 | |
| 9 | 76.49 | -1.63 | |
| 0.1 | 77.79 | -0.33 |
| Run | Correct / Total | Overall | Multi-Hop | Temporal | Open | Single-Hop |
|---|---|---|---|---|---|---|
| 1 | 1200 / 1540 | 77.92 | 64.54 | 77.88 | 48.96 | 85.73 |
| 2 | 1199 / 1540 | 77.86 | 64.89 | 76.95 | 50.00 | 85.73 |
| 3 | 1210 / 1540 | 78.57 | 62.77 | 78.19 | 48.96 | 87.40 |
| 4 | 1207 / 1540 | 78.38 | 65.25 | 76.32 | 47.92 | 87.04 |
| 5 | 1212 / 1540 | 78.70 | 65.60 | 77.26 | 47.92 | 87.16 |
| 6 (main table) | 1203 / 1540 | 78.12 | 64.89 | 76.64 | 43.75 | 87.04 |
| Benchmark | Backbone | Overall | Temporal | Multi | Open | Single | Know.-Upd. | Single-Asst. | Avg. Ans. |
|---|---|---|---|---|---|---|---|---|---|
| LoCoMo | GPT-4o-mini | 78.12 | 76.64 | 64.89 | 43.75 | 87.04 | – | – | 73.12 |
| LoCoMo | Qwen3 | 76.36 | 67.91 | 59.22 | 52.08 | 88.11 | – | – | 128.88 |
| LongMemEval-S | GPT-4o-mini | 77.20 | 69.92 | 65.41 | – | 92.86 | 82.05 | 92.86 | 75.52 |
| LongMemEval-S | Qwen3 | 73.80 | 57.89 | 64.66 | – | 94.29 | 78.21 | 92.86 | 146.37 |
| Benchmark | Backbone | AMUs / dialog | Active codes | Code assignments | Max code freq. | Avg. AMU words |
|---|---|---|---|---|---|---|
| LoCoMo | GPT-4o-mini | 591.1 | 216.9 | 3678.6 | 294.0 | 16.48 |
| LoCoMo | Qwen3 | 940.4 | 266.1 | 6150.2 | 461.4 | 15.92 |
| LongMemEval-S | GPT-4o-mini | 1553.4 | 420.4 | 9941.1 | 798.0 | 21.41 |
| LongMemEval-S | Qwen3 | 2130.3 | 450.2 | 14532.2 | 1356.1 | 21.80 |
| Benchmark | Shared | Both correct | Both wrong | GPT-only | Qwen-only | Largest GPT-only source |
|---|---|---|---|---|---|---|
| LoCoMo | 1540 | 1055 | 216 | 148 | 121 | Temporal: 55 vs. 27 |
| LongMemEval-S | 500 | 322 | 67 | 64 | 47 | Temporal: 28 vs. 12 |
| Category | Accuracy | Paired Wins | BM25 Injection | |||||
|---|---|---|---|---|---|---|---|---|
| Codebook | BM25 | C-only | B-only | New Base | New Time | |||
| Overall | 1540 | 78.12 | 76.75 | +1.36 | 105 | 84 | 18.58 | 10.96 |
| Multi-Hop | 282 | 64.89 | 60.64 | +4.26 | 33 | 21 | 18.50 | 10.65 |
| Temporal Reasoning | 321 | 76.64 | 70.41 | +6.23 | 35 | 15 | 18.66 | 11.34 |
| Open Domain | 96 | 43.75 | 50.00 | -6.25 | 5 | 11 | 18.59 | 10.78 |
| Single-Hop / Basic | 841 | 87.04 | 87.63 | -0.59 | 32 | 37 | 18.58 | 10.93 |
| Metric | MemCode | Full HippoRAG2@40 |
|---|---|---|
| Additional model calls | 0 | 12,779 |
| Additional tokens | 0 | 8.47M |
| Explicit graph construction | None | 34.49 min |
| Graph size | None | 17,275 nodes / 109,779 edges |
| Query latency | 0.060 s | 2.864 s |
| Category | bge Acc. | MiniLM Acc. | bge-only | MiniLM-only | Shared correct | ||
|---|---|---|---|---|---|---|---|
| Overall | 1540 | 78.12 | 76.23 | -1.88 | 90 | 61 | 1113 |
| Multi-Hop | 282 | 64.89 | 62.77 | -2.13 | 23 | 17 | 160 |
| Temporal Reasoning | 321 | 76.64 | 73.83 | -2.80 | 20 | 11 | 226 |
| Open Domain | 96 | 43.75 | 43.75 | +0.00 | 4 | 4 | 38 |
| Single-Hop / Basic | 841 | 87.04 | 85.37 | -1.66 | 43 | 29 | 689 |
| Asset | Role in this paper | Citation/source | License or terms |
|---|---|---|---|
| LoCoMo | Long-term conversational-memory benchmark for QA evaluation | Maharana et al. [32] | CC BY-NC 4.0 release license |
| LongMemEval-S | Dynamic multi-session memory benchmark for QA evaluation | Wu et al. [33] | MIT-licensed public release |
| DailyDialog | Conversation data used to train the discrete codebook component | Li et al. [25] | CC BY-NC-SA 4.0 release license |
| T5-base | Encoder initialization for isomorphic semantic quantization | Raffel et al. [36] | Apache-2.0 release license |
| bge-base-en-v1.5 | Dense embedding backbone for vector retrieval | Xiao et al. [37] | MIT public model-release terms |
| GPT-4o-mini API | Memory writing, answer generation, and LLM-as-a-judge evaluation | OpenAI [34] | OpenAI service terms |
LLM Agent Memory
Large language model agent memory lets agents retain, organize, retrieve, and update information across interactions and long-running tasks.
See all 358 papers