TL;DR
When an LLM reads a long document before any questions are asked, keeping its full Key-Value (KV) cache costs gigabytes of memory, while compressing it blindly throws away the very evidence later questions will need. HeadMem discovers that a frozen LLM already contains its own compact retriever: a handful of "retrieval heads" that can locate relevant evidence even when reading a question completely in isolation. By storing only the compressed keys of these 16 heads (290× smaller than the full KV cache), HeadMem lets the model re-read just the best 2,048 tokens per question—beating KV compression methods, matching or beating the full cache, and answering questions up to 13× faster.
What Problem Does This Paper Address?
Many real-world AI workloads follow a "read first, ask later" pattern: a model ingests a massive codebase, legal document, or agent history long before a user asks a single question, and that same context is then queried repeatedly.
The natural memory for an LLM is its KV cache (the stored key and value vectors from its attention layers), but it is brutally expensive—128 KiB per token on Llama-3.1-8B, or 16 GiB for a 128k-token context, taking nearly 18 seconds just to prefill. Existing workarounds all hit a wall:
- KV cache compression evicts tokens before any question exists, which inevitably discards the needles later questions are looking for.
- External retrievers (RAG) require running and indexing a separate embedding model.
- Attention-based retrievers usually need to read the question together with the context, forcing a fresh pass over the document for every new query.
The authors ask: Can we build a tiny, query-agnostic memory using only the LLM's own internal states, without any training?
Methods
HeadMem turns a frozen LLM's own retrieval heads into a three-step, training-free memory pipeline:
- Detect Retrieval Heads (Once per model): Using a small 80-question development split, the authors feed the context and the bare question separately (each prepended with a sink token to absorb initial attention). They rank all attention heads by how much softmax probability mass the bare question's tokens place on the true answer tokens. They keep the top 16 heads (or just the top 2 for a ultra-lightweight variant, HeadMem-2).
- Build the Key Memory (Once per context): The LLM processes the context in independent 4,096-token chunks, stopping at the deepest layer that hosts a detected head. For those few heads, it stores only the pre-RoPE keys compressed via Product Quantization (PQ-32)—shrinking the state to 448 B/token for HeadMem (290× smaller than FullKV) and 32 B/token for HeadMem-2 (4,096× smaller). No values, no other heads, and no cross-chunk state are kept.
- Score and Re-read (Per question): When a question arrives, it is forwarded alone through the detected heads' layers. Each head scores every stored context position using its own softmax. A span selector smooths these scores and picks the top 2,048 tokens, which the frozen LLM re-reads in original order to generate the answer.
Because HeadMem only proposes tokens to re-read, it is plug-and-play: it can share a 2,048-token budget with compressed KV caches (like StreamingLLM or SnapKV) or fuse its scores with BM25 or dense retrievers.
Datasets
The authors evaluate HeadMem across six frozen models (including Llama-3.1-8B, Qwen2.5-7B/14B/32B, Mistral-7B, and Qwen2-7B) on a diverse suite of benchmarks:
- RULER-MQ: A multi-question adaptation of RULER where 8 independent questions query the same 32k–128k context across 5 task families (multi-key/multi-value needle-in-a-haystack, variable tracking, SQuAD, and HotpotQA).
- NoLiMa: A benchmark where questions and their target needles share no overlapping words, testing latent association.
- LongBench QA & InfiniteBench: Standard long-document QA tasks and full-book comprehension.
- LongMemEval-S: Long user–assistant chat histories (~105k tokens) measuring both evidence recall and answer accuracy.
- 1M-Token Stress Test: A 5-family evaluation pushing far beyond the base model's native context window.
Key Insights and Ideas
- Retrieval heads are token-matchers: Mechanistic analysis reveals that the detected heads behave like induction heads on Llama and Mistral (attending right after matched question tokens) and direct token-matchers on Qwen2.5. Masking these 16 heads tanks the model's own full-cache long-context score far more than masking random heads in the same layers.
- Head selection and per-head softmax carry the signal: An ablation ladder shows that averaging over all heads or using raw dot products fails. Isolating the 16 detected heads and normalizing each head's scores with its own softmax is what unlocks retrieval—and compressing those keys 8× with PQ-32 barely changes accuracy.
- Reading across chunks works, if the evidence is found: Even though HeadMem encodes chunks independently, the LLM successfully combines evidence across chunks once those tokens are selected.
Key Results
Memory vs. accuracy and end-to-end cost at 128k tokens
- Dominates KV compression: On 128k RULER-MQ (Llama-3.1-8B), HeadMem alone scores 72.1, crushing StreamingLLM (6.9), SnapKV (10.3), PyramidKV (9.8), and KVzip (8.5), and even edging out FullKV (69.4). Fused with BM25, it reaches 73.9.
- Works without lexical overlap: On NoLiMa at 8k, HeadMem beats BM25 by +24.22 points (34.8 vs. 9.0) and matches the full cache.
- Plays well with others: Sharing half of a 2,048-token budget with HeadMem boosts StreamingLLM and SnapKV from ~7–14 points up to >66 points across 32k–128k contexts. Fusing HeadMem with a dense retriever beats reranked dense RAG at 128k on both Llama and Qwen2.5.
- Massive speedups: On a 128k context, HeadMem-2 builds its memory 27× faster than FullKV prefills, answers 8 questions 13× faster end-to-end (1.63 s vs. 21.73 s) with no statistically significant drop in accuracy, and scales to 1M tokens in 5.36 s (32 MiB) where FullKV runs out of memory.
Limitations and Future Work
- Chained reasoning gaps: Because HeadMem relies on token-matching, it reliably finds the first statement of a multi-hop variable-tracking chain (which shares tokens with the question) but misses subsequent statements in the chain. Forcing all chain statements into its 2,048-token budget jumps Llama's 128k score on that task from 35.8 to 98.8, showing the bottleneck is purely retrieval, not reasoning.
- Domain-specific gaps: On LongBench QA (with Mistral) and whole-book tasks in InfiniteBench, a dedicated dense retriever with a reranker still outperforms HeadMem.
Summary updated