Memorilla: Latent Semantic Memory for LLMs
Organizations: Stanford University · University of Toronto · Vector Institute · Arc Institute ·
Abstract
Memory enables large language models (LLMs) to use information beyond their parameters, such as past user conversations, long documents, or an agent's earlier actions. For a frozen LLM, access to this accumulated knowledge is limited to what a memory system places in its context window. However, gathering and combining relevant information needed for each query while keeping the model's context short remains challenging for existing systems. Here we introduce Memorilla, a memory system that gathers information from an entire document collection into a fixed number of query-dependent 'memory tokens' for a frozen LLM. Memorilla encodes documents (such as user chats, book chapters) once into reusable embeddings, then learns to retrieve and place query-specific information in the LLM's input embedding space. Using only 16 memory tokens, Memorilla achieves the best performance against baselines including Mem0 and MemoRAG on five of eight benchmarks spanning personalization, long-document and knowledge-intensive question answering. Notably, on FactKG, it reaches 84.33% accuracy versus 80.43% with full-context inference, while using 108x fewer tokens to represent the source documents. Furthermore, Memorilla can facilitate continual learning in agentic systems where the document count (the agent's past turns) increases throughout the trajectory. In a TextWorld environment, we show that Memorilla reduces the agent's tendency to wander between rooms it has already explored, and improves its average success rate from 18.6% with full-history in-context learning to 24.2%. Overall, Memorilla aids frozen LLMs in accumulating and using knowledge effectively across long-context interactions and takes a step towards building continually learning AI systems.
Figures & tables
Appendix figures & tables20 assets
Supplementary material from the paper’s appendix.
Appendix
| Stage | Source | Category | Num. Queries |
| Pretraining | EnWiki Articles | Paraphrase | 1,979,809 |
| Finetuning | SQuAD v2 ( Rajpurkar et al., 2018 ) | Reading Comprehension | 86,821 |
| DROP ( Dua et al., 2019 ) | Reading Comprehension | 77,400 | |
| CoQA ( Reddy et al., 2019 ) | Reading Comprehension | 108,647 | |
| PubMedQA ( Jin et al., 2019 ) | Reading Comprehension | 900 | |
| QuAIL ( Rogers et al., 2020 ) | Reading Comprehension | 10,246 |
| Benchmark | Chunk unit | Tokens per chunk (mean / median) |
| PMv2 | One conversation session | 280 / 262 |
| PV4 | One chat dialogue | 252 / 224 |
| PubMedQA | One abstract section | 84 / 78 |
| TriviaQA | Evidence passage, split sentence-greedily at 128 tokens | 70 / 77 |
| NarrativeQA | Story paragraph, split on sentences above 256 tokens | 94 / 35 |
| FactKG | One knowledge-graph triple | 13 / 13 |
| Component | Setting | Value |
| Memory module | Pooling mode | Adaptive cross-attention with softmax over documents |
| Memory slots | ||
| Cross-attention layers | 2 layers, slots documents | |
| Attention heads | 8 | |
| Encoder-side dimension | 2560 | |
| Decoder-side dimension | 2560 for 4B decoder, 4096 for 8B decoder |
| Setting | Stage 1 | Stage 2 | Stage 3 |
| EnWiki pretrain | Multi-task pretrain | Downstream fine-tune | |
| Initialization | Random | Warm-start from Stage 1 | Warm-start from Stage 2 |
| Data | EnWiki paraphrase reconstruction | Mixed supervision corpus | Unified downstream mixture |
| Tasks | Paraphrase reconstruction | Reading comprehension, open-domain QA, summarization | PV4, PMv2, FactKG, TriviaQA, NQA, PubMedQA, LaMP-4, LaMP-7 |
| Optimizer | AdamW | AdamW | AdamW |
| LR scheduler | Cosine annealing with warmup | Cosine annealing with warmup | Cosine annealing with warmup |
| Item | Configuration |
| Stage 1 corpus | 1.98M EnWiki paraphrase reconstruction examples, chunked at approximately 128 tokens with mean 1.73 documents per row |
| Stage 2 corpus | Mixed supervision over SQuAD v2, DROP, CoQA, PubMedQA, QuAIL, PwC, CNN/DailyMail, SAMSum, DialogSum, and MS MARCO |
| Stage 3 corpus | Unified concatenation of PV4, PMv2, FactKG, TriviaQA, NarrativeQA, PubMedQA, LaMP-4, and LaMP-7 |
| PV4 and PMv2 specialization | After multi-task fine-tuning, PV4 and PMv2 are each further fine-tuned for 5 epochs on the corresponding benchmark training split using the same recipe |
| Other downstream tasks | FactKG, TriviaQA, NarrativeQA, PubMedQA, LaMP-4, and LaMP-7 use the multi-task checkpoint directly with no additional task-specific training |
| Evaluation split | Official test split for every task except LaMP-4 and LaMP-7, which use their development splits; TriviaQA, which uses a deterministic seed-23 validation subsample with ; and PubMedQA, which uses the 100-example test portion of a 900/100 split derived from PQA-L |
| Setting | Mem0 | MemoRAG |
| Implementation | mem0ai 0.1.118 with its stock prompts and ADD/UPDATE/DELETE/NONE loop, unmodified | Official repository with its prompts unchanged |
| Memory-writing model | Frozen Qwen3-4B-Instruct-2507 extracts facts and consolidates memories | Frozen Qwen3-4B-Instruct-2507 reads the collection as its memory |
| Memory-model decoding | Greedy, up to 2,048 tokens | Greedy, up to 128 tokens per clue call |
| Write path | One add() per document, with each extracted fact reconciled against its 5 nearest stored memories | Collection read once per entity and cached, front-truncated only where it exceeds the context window (NarrativeQA) |
| Memory scope | One Qdrant collection per user (PMv2, PV4), story (NarrativeQA) or question (others) | One memory per user, story or question |
| Retrieval query | The question | Clue spans and surrogate questions longer than 3 words, plus the question |
| Dataset | Task | docs/q | tok/q | Train | Val | Test |
| PersonalizationV4 (PV4) (Ours) | Personalization SA | 200 | 50,476 | 15,058 | 1,755 | 4,237 |
| PersonaMem-v2 (PMv2) ( Jiang et al., 2025 ) | Personalization SA | 116 | 31,618 | 18,521 | 2,059 | 5,000 |
| NarrativeQA (NQA) ( Kočiskỳ et al., 2018 ) | Long-document generative QA | 1,420 | 76,225 | 32,747 | 3,461 | 10,557 |
| FactKG ( Kim et al., 2023 ) | KG fact verification | 129 | 1,665 | 86,367 | 13,266 | 9,041 |
| TriviaQA ( Joshi et al., 2017 ) | Open-domain factoid QA | 164 | 11,861 | 138,384 | 17,944 | — |
| PubMedQA ( Jin et al., 2019 ) | Biomedical QA | 4 | 324 | 900 | — | 100 |
| Persona | Name Maya Patel. Age 34. Location Austin, Texas, USA. Occupation Certified Welder & Part-Time CNC Operator at a regional aerospace supplier. |
| … Maya grew up in a multicultural household where her Indian-American parents emphasized discipline, education, and the importance of physical well-being. After completing a two-year technical diploma in welding technology, she entered the workforce in a traditionally male-dominated industry, where she quickly earned a reputation for precision, reliability, and an unflappable calm under pressure. Though she never pursued a university degree, her practical knowledge of metallurgy, robotics-aided fabrication, and safety protocols rivals that of many engineers. … | |
| Categories | Aerospace welding expertise & mental rehearsal habits. Gaming genre preferences & playstyle strategies. Morning mango ritual & mobility routine. … (3 of 10) |
| Chat topic | Maya asks for a list of cooperative building-and-craft games she can play online with classmates after welding class. |
| User turn | … looking for coop building-and-craft games I can jump into with the welding class after tonight’s session. Need stuff that’s accessible for mixed skill levels, supports voice chat, and has meaningful base-building or automation mechanics. Toss me a short list with platform notes and why each one fits. |
| Assistant turn | (seven games with platform notes, from Minecraft to Raft) … Each supports voice via Discord/Steam, scales with mixed experience, and offers clear building goals for group sessions. |
| Question | In a new open-world crafting RPG, players can specialize in one mastery for unique bonuses. Considering Maya’s welding expertise, love for base-building, and ergonomic focus from the Iron Heart project, which specialization best aligns with her likely in-game identity? |
| Questions | Each question must require synthesizing information or understanding nuance from the persona. |
| Questions must require combining two or more facts from the persona profile. Avoid questions that can be answered by recalling a single fact. | |
| Embed each question in a specific, realistic scenario that requires applying persona knowledge to a novel situation rather than simple recall. | |
| The question stem must not hint at characteristics of the answer. | |
| Distractors | Every choice (A-E) must be a reasonable, professional, or logical preference for a human. Never use obviously wrong, absurd, or incompetent distractors. |
| Ensure distractors include industry-standard best practices that most people in the persona’s role would choose, but which this specific persona rejects. | |
| Use information that is true about the persona but applies it to the wrong situation or frequency. |
| Benchmark | Docs/q | Fits 32K | RAG 1 | RAG 5 | RAG 50 | Full Context | LLM Lingua-2 | xRAG | Mem0 | Memo RAG | Memorilla |
| PMv2 | 116 | Partial | 125 | 652 | 9,009 | 31,587 | 250 | 1 | 665 | 9,254 | 16 |
| PV4 | 200 | No | 201 | 1,021 | 10,832 | 50,455 | 180 | 1 | 978 | 11,046 | 16 |
| PubMedQA | 4 | Yes | 69 | 321 | 324 † | 324 | 290 | 1 | 8 | 324 | 16 |
| NarrativeQA | 1,420 | Partial | 69 | 354 | 3,612 | 67,385 | 596 | 1 | 493 | 800 | 16 |
| TriviaQA | 164 | Partial | 51 | 226 | 2,357 | 11,888 | 243 | 1 | 269 | 303 | 16 |
| FactKG | 129 | Yes | 13 | 64 | 568 | 1,727 | 279 | 1 | 300 | 573 | 16 |
| Task | Metric | Closed book | RAG 1 | RAG 5 | RAG 50 | Full Context | LLM Lingua-2 | xRAG | Mem0 | Memo RAG | Memorilla | Compression |
| PMv2 | Acc (SA) | 25.64 | 26.10 | 25.92 | 26.72 | 27.92 | 26.92 | 30.90 | 27.48 | 28.76 | 44.60 | 1,974 |
| PV4 | Acc (SA) | 58.30 | 62.21 | 64.98 | 65.78 | 59.12 | 52.14 | 62.64 | 65.07 | 67.88 | 66.65 | 3,153 |
| Acc (SA, Hard) | 31.96 | 38.00 | 39.27 | 41.97 | 36.57 | — | 34.98 | 44.20 | 45.95 | 41.97 | ||
| PubMedQA | Acc | 21.00 | 34.00 | 67.00 | 67.00 | 67.00 | 69.00 | 36.00 | 24.00 | 67.00 | 59.00 | 20 |
| NarrativeQA | BLEU-1 | 7.09 | 7.36 | 7.85 | 8.29 | 5.89 | 6.87 | 16.86 | 7.77 | 9.14 | 16.36 | 4,212 |
| ROUGE-1 | 11.54 | 13.32 | 15.60 | 18.45 | 19.12 | 7.26 | 17.33 | 14.24 | 21.80 | 19.68 |
| Method | Tok. | LaMP-4 | LaMP-7 |
| ROUGE-L | ROUGE-L | ||
| ComMer | 128 | 17.50 | 45.00 |
| Memorilla (4B) | 16 | 17.72 | 47.04 |
| Memorilla (8B) | 16 | 18.15 | 48.37 |
| Method | Query aware | Params | PMv2 SA |
| Global mean-pool | no | 6,566,400 | 28.94 |
| Segmented mean-pool | no | 6,566,400 | 29.68 |
| Segmented, parameter-matched | no | 190,149,120 | 31.10 |
| Cosine top-16, parameter-matched | yes (hard) | 190,149,120 | 33.86 |
| Documents only | no | 268,856,320 | 34.72 |
| Q-Former | yes (soft) | 268,856,320 | 43.34 |
| Path | Quantity | Growth | ||
| Write | Document encoding | 22.92 ms | 2,588.84 ms | linear in |
| Storage per document | 5 KB | 5 KB | flat | |
| Read | Memory module forward | 2.01 ms | 1.96 ms | ms |
| CPU to GPU transfer | 0.014 ms | 0.060 ms | ms | |
| Decoder | Context tokens | 16 | 16 | flat |
| Memorilla end to end | 740 ms | 739 ms | ms |
| Component | Setting | Value |
| Environment | Task suite | TextWorld held-out games up to 65 rooms |
| Splits | 800 train prompts; 40-prompt validation and test splits (30 games each) | |
| Agent state | Initial description plus prior observations/actions | |
| Reward | TextWorld game score; no shaping reward | |
| Model interface | Decoder | Frozen Qwen3-4B-Instruct-2507 |
| Trajectory encoder | Frozen Qwen3-Embedding-4B |
Memory-Augmented Language Models
Memory-augmented language models combine persistent or temporary memory with neural computation to retain, retrieve, and use information beyond the active context.
See all 291 papers