arXiv:2610.21375cs.LGOct 2026

Manuscript Offprint

arXiv:2610.21375

Memorilla: Latent Semantic Memory for LLMs

Adibvafa Fallahpour, Parsa Idehpour, Vignesh Kothapalli, Nikita Mounier, Shirley Wu, Shayan Pardis, Jure Leskovec

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

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 average performance against baselines including Mem0 and MemoRAG across 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 108×108 \times 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.

1 INTRODUCTION

Memory is central to how large language models (LLMs) are used. Personal assistants must recall a user's history across months of conversations, question-answering systems must reason over documents far longer than the model's context window, and agents must act on everything they have observed. None of this information exists in the model's parameters, and a frozen LLM can use it only through its context (Hu et al., 2025). A memory system bridges this gap by encoding accumulated information, storing it persistently, and retrieving what each query needs into the context (Wu et al., 2025). The central challenge for such a system is to give a frozen LLM access to an entire, growing collection of knowledge while keeping its context short (An et al., 2025; Du et al., 2025).

Existing memory systems address these issues only in part. Agentic memory systems such as MemGPT (Packer et al., 2023), Mem0 (Chhikara et al., 2025) and A-MEM (Xu et al., 2025) organize past interactions into summaries or structured records, and MemoRAG (Qian et al., 2025) uses a

∗^{*}Equal contribution, sorted alphabetically.

†^{\dagger}Correspondence to: {adib, vigneshk, jure} @stanford.edu, ididea@seas.upenn.edu

Figure

Figure 1: Performance versus context length. Mean score over eight benchmarks against the geometric mean of document tokens given to a frozen Qwen3-8B decoder. MEMORILLA lies on the Pareto frontier (dashed) with the highest score at 16 memory tokens, 394×394 \times fewer than full context.

compressed global memory to guide retrieval, but each ultimately places selected text in the context and inherits the limits of selection. Latent compression methods such as ICAE (Ge et al., 2024), IC-Former (Wang et al., 2024a), AutoCompressors (Chevalier et al., 2023) and ComMer (Zeldes et al., 2025) represent documents as a few soft tokens in the LLM embedding space, but they form these tokens without the query, so the same tokens must serve every question. xRAG (Cheng et al., 2024) incorporates query relevance but only by compressing a single retrieved document. Methods that store model-specific hidden or key-value states, such as LongMem (Wang et al., 2023), MemoryLLM (Wang et al., 2024b), M+ (Wang et al., 2025), and Cartridges (Eyuboglu et al., 2026), tie the memory to one model and require training the LLM, a side network, or a separate cache for each corpus. What is missing is a memory system that will retrieve query-relevant information from document embeddings directly into the input embedding space of a frozen LLM. Benefits of such an approach would be that the frozen LLM can retain all of its capabilities, and rely on only a few query relevant latent embeddings to answer questions or solve tasks instead of processing long textual contexts in a slow and lossy manner (Liu et al., 2024). In essence, improving the speed and quality of LLM responses and also the decision making in LLM agents based on past experiences.

Present work. Here we introduce MEMORILLA, a memory system that learns to retrieve query-relevant information from document embeddings directly into the input embedding space of a frozen LLM (Figure 2). First, a frozen embedding model encodes each document, such as a user conversation or a book chapter, once into a persistent memory store. Next, for each query, a trainable memory module attends to every embedding in the persistent memory store and performs a query conditioned memory readout for the LLM. Unlike RAG which retrieves a subset of documents as textual tokens, MEMORILLA learns to recall what matters from the entire collection for a query, and expresses it as K=16K = 16 latent memory tokens for a frozen LLM, and does not require the documents to enter the context as text. Because the store uses the same embeddings as a dense retrieval index, encoding adds no cost over retrieval, and with 512 documents on one H100, MEMORILLA has 27×27 \times lower end-to-end latency than full context answer generation (0.74s against 19.9s, Appendix F).

Our benchmarks mirror three settings in which deployed LLMs need memory (Figures 1 and 3). In personalization, an assistant must adapt to preferences a user rarely states directly, whether answering questions that depend on traits revealed across past conversations (PersonaMem-v2, PersonalizationV4) or writing in the style of the user's earlier text (LaMP-4, LaMP-7). Because a user's preferences rarely share wording with the query, similarity-based retrieval struggles to find and combine relevant information. Notably, on LaMP-4, MEMORILLA reaches 18.15 ROUGE-L against 14.21 for full context while using 474×474 \times fewer document tokens. In knowledge-intensive question answering, such as checking a claim against a knowledge base, MEMORILLA outperforms full context on FactKG (84.33% against 80.43%) with 108×108 \times fewer document tokens. In long-document understanding, the answer is buried in thousands of tokens of text and often phrased differently from the question, so a system must find and connect relevant content across the entire document. On

Figure

Figure 2: MEMORILLA architecture. A frozen encoder embeds source documents (User Chats) once into a persistent memory store. For each Query, K query-conditioned slots attend over the store and are projected into the frozen LLM's embedding space as a memory readout.

TriviaQA, whose questions were written independently of their source pages, MEMORILLA reaches the highest exact match (57.00) with 743×743 \times fewer document tokens than full context.

Beyond answering questions, MEMORILLA allows agents to learn continually from their own experience. In this work, we consider the TextWorld environment (Côté et al., 2018) where an agent must explore rooms, find and use objects, and complete a multi-step goal within 50 turns, which requires remembering where it has been and what it has already tried. After every turn, MEMORILLA adds that turn to its document store and draws on this experience at the next turn. With our approach, the agent completes 37.5% of held-out games in at least one of three attempts, against 25.0% for the same agent given its full history in context. Overall, MEMORILLA improves the agent's ability to escape navigation loops and execute action sequences that are crucial to reaching the target.