M: Every Task Deserves Its Own Memory Harness
Organizations: City University of Hong Kong · Microsoft
Abstract
Large language model agents rely on specialized memory systems to accumulate and reuse knowledge during extended interactions. Recent architectures typically adopt a fixed memory design tailored to specific domains, such as semantic retrieval for conversations or skills reused for coding. However, a memory system optimized for one purpose frequently fails to transfer to others. To address this limitation, we introduce M, a method that automatically discovers task-optimized memory harnesses through executable program evolution. Specifically, M models an agent memory system as a memory program written in Python. This program encapsulates the data Schema, the storage Logic, and the agent workflow Instructions. We optimize these components jointly using a reflective code evolution method; this approach employs a population-based search strategy and analyzes evaluation failures to iteratively refine the candidate programs. We evaluate M on four distinct benchmarks spanning conversation, embodied planning, and expert reasoning. Our results demonstrate that M improves performance over existing fixed-memory baselines robustly across all evaluated tasks. Furthermore, the evolved memory programs exhibit structurally distinct processing mechanisms for each domain. This finding indicates that specializing the memory mechanism for a given task explores a broad design space and provides a superior solution compared to general-purpose memory paradigms.
Figures & tables
| LoCoMo | ALFWorld | HealthBench | PRBench | |||||
| Method | F1 | L-J | Unseen | Seen | Data | Emerg. | Legal | Finance |
| No Memory | 0.036 | 0.030 | 0.738 | 0.640 | 0.242 | 0.429 | 0.431 | 0.269 |
| Retrieval-based Systems (Memorizing Episodes) | ||||||||
| Vector Search | 0.256 | 0.400 | 0.643 | 0.720 | 0.264 | 0.400 | 0.466 | 0.308 |
| G-Memory | 0.224 | 0.380 | 0.690 | 0.560 | 0.309 | 0.447 | 0.499 | 0.318 |
| Mem0 | 0.373 | 0.540 | 0.738 | 0.640 | 0.216 | 0.413 | 0.450 | 0.321 |
| Variant | F1 | |
|---|---|---|
| Mstar | 0.459 | — |
| Instruction | 0.353 | 0.106 |
| Code | 0.256 | 0.203 |
| Max parent selection | 0.381 | 0.078 |
| Diversity | 0.318 | 0.141 |
| Benchmark | Mean Std | CV | Best Baseline | Win |
|---|---|---|---|---|
| LoCoMo | 10.0 | 0.373 (Mem0) | 4/5 | |
| HB Data | 5.9 | 0.327 (GEPA+VS) | 5/5 | |
| PR Finance | 5.7 | 0.449 (GEPA) | 5/5 |
| (a) ALFWorld Unseen | (b) LoCoMo | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Simple | Clean | Cool | Heat | 2-Obj | Single | Temp. | Causal | Open | |||
| Min | Min | ||||||||||
| No Memory | 0.88 | 0.82 | 0.57 | 0.83 | 0.62 | 0.57 | 0.02 | 0.00 | 0.11 | 0.04 | 0.00 |
| Mem0 | 0.88 | 0.64 | 0.57 | 1.00 | 0.75 | 0.57 | 0.33 | 0.26 | 0.37 | 0.43 | 0.26 |
| Trajectory Retrieval | 0.75 | 0.73 | 0.71 | 0.67 | 0.75 | 0.67 | 0.18 | 0.33 | 0.34 | 0.28 | 0.18 |
| GEPA + Vector Search | 0.88 | 1.00 | 0.57 | 1.00 | 0.62 | 0.57 | 0.15 | 0.38 | 0.39 | 0.31 | 0.15 |
Appendix figures & tables10 assets
Supplementary material from the paper’s appendix.
Appendix
| Benchmark | Split | Train | Val | Test |
|---|---|---|---|---|
| LoCoMo | — | 272 sessions | 1,440 QA | 100 QA |
| ALFWorld | Unseen | 4,652 trajectories | 145 ep. | 50 ep. |
| Seen | 4,652 trajectories | 150 ep. | 50 ep. | |
| HealthBench | Data Tasks | 257 | 120 | 100 |
| Emergency | 260 | 122 | 100 | |
| PRBench | Legal | 100 | 100 | 50 |
| Benchmark | Config | Iter. | Task | Reflect. | Judge | Toolkit | Input | Output | Cost | Time |
|---|---|---|---|---|---|---|---|---|---|---|
| LoCoMo | Mstar | 20 | 5802 | 48 | — | 135 | 4.9M | 4.2M | $25.43 | 5.1h |
| Vector Search | 0 | 200 | 0 | — | 272 | 294K | 71K | $0.54 | 2m | |
| No Memory | 0 | 200 | 0 | — | 272 | 219K | 64K | $0.45 | 1m | |
| ALFWorld | Mstar | 20 | 3355 | 25 | — | 443 | 7.5M | 1.4M | $13.92 | 100h |
| (Unseen) | Vector Search | 0 | 80 | 0 | — | 497 | 711K | 50K | $0.76 | 14m |
| No Memory | 0 | 80 | 0 | — | 497 | 708K | 50K | $0.76 | 11m |
| Parameter | Value |
|---|---|
| Evolution iterations | 20 |
| Population seed programs | 3 |
| Selection strategy | Softmax ( ) |
| Static validation subset | 32–60 items ( -means clustering) |
| Rotating validation subset | 5 items per iteration |
| Train-to-validation ratio | 2 (train episodes = validation items) |
| Seed | Storage | Retrieval | LLM in read |
|---|---|---|---|
| Vector Search | ChromaDB chunks | Top- cosine similarity | No |
| LLM Summarizer | In-memory list | Concatenate + LLM summarize | Yes (1 call) |
| Experience Learner | Separate lesson/fact lists | Return all | No |
| Benchmark | Lines | SQLite | ChromaDB | LLM in read | Schema fields | Test |
|---|---|---|---|---|---|---|
| LoCoMo | 425 | Yes | Yes | No | 7 | 0.459 |
| ALFWorld (Unseen) | 276 | Yes | No | Yes | 8 | 0.881 |
| ALFWorld (Seen) | 441 | Yes | No | No | 8 | 0.700 |
| HB-Emergency | 393 | Yes | No | Yes | 7 | 0.493 |
| HB-Data | 769 | Yes | Yes | Yes | 12 | 0.390 |
| PR-Legal | 1184 | Yes | Yes | Yes | 22 | 0.660 |
| Simple | Movable | Clean | Light | Cool | Heat | 2-Obj | |
|---|---|---|---|---|---|---|---|
| No Memory | 0.88 | 1.00 | 0.82 | 0.00 | 0.57 | 0.83 | 0.62 |
| Vector Search | 0.62 | 1.00 | 0.64 | 0.00 | 0.71 | 0.67 | 0.62 |
| G-Memory | 1.00 | 1.00 | 0.55 | 1.00 | 0.29 | 0.83 | 0.75 |
| Mem0 | 0.88 | 1.00 | 0.64 | 0.00 | 0.57 | 1.00 | 0.75 |
| Trajectory Retrieval | 0.75 | 1.00 | 0.73 | 0.00 | 0.71 | 0.67 | 0.75 |
| Simple | Clean | Light | Cool | Heat | 2-Obj | Overall | |
|---|---|---|---|---|---|---|---|
| No Memory | 1.00 | 0.33 | 1.00 | 0.50 | 0.43 | 0.87 | 0.640 |
| Vector Search | 1.00 | 0.42 | 1.00 | 1.00 | 0.29 | 0.87 | 0.720 |
| G-Memory | 1.00 | 0.33 | 1.00 | 0.12 | 0.29 | 0.87 | 0.560 |
| Mem0 | 1.00 | 0.25 | 1.00 | 0.62 | 0.43 | 0.87 | 0.640 |
| Trajectory Retrieval | 1.00 | 0.58 | 1.00 | 0.88 | 0.43 | 0.93 | 0.780 |
| Single-hop | Temporal | Causal | Open-domain | Overall | |
|---|---|---|---|---|---|
| No Memory | 0.02 | 0.00 | 0.11 | 0.04 | 0.036 |
| Vector Search | 0.19 | 0.29 | 0.38 | 0.25 | 0.256 |
| G-Memory | 0.23 | 0.12 | 0.31 | 0.24 | 0.224 |
| Mem0 | 0.33 | 0.26 | 0.37 | 0.43 | 0.373 |
| Trajectory Retrieval | 0.18 | 0.33 | 0.34 | 0.28 | 0.276 |
| Test Score by Seed | |||||||||
|---|---|---|---|---|---|---|---|---|---|
| Benchmark | 0 | 1 | 2 | 3 | 4 | Mean | Std | CV | Best Baseline |
| LoCoMo | 0.459 | 0.463 | 0.364 | 0.394 | 0.423 | 0.421 | 0.042 | 10.0% | 0.373 |
| HB-Data | 0.390 | 0.382 | 0.357 | 0.411 | 0.413 | 0.391 | 0.023 | 5.9% | 0.327 |
| PR-Finance | 0.586 | 0.544 | 0.582 | 0.582 | 0.513 | 0.561 | 0.032 | 5.7% | 0.449 |
| Best Validation Score | |||||||
|---|---|---|---|---|---|---|---|
| Benchmark | 0 | 1 | 2 | 3 | 4 | Mean | Std |
| LoCoMo | 0.333 | 0.273 | 0.289 | 0.312 | 0.333 | 0.308 | 0.027 |
| HB-Data | 0.424 | 0.381 | 0.378 | 0.397 | 0.380 | 0.392 | 0.020 |
| PR-Finance | 0.521 | 0.522 | 0.559 | 0.554 | 0.515 | 0.534 | 0.020 |
Agent Memory
Agent memory lets AI systems retain, update, and use facts, experiences, and skills across sessions and long-horizon tasks.
See all 191 papers