When generation pushes the KV cache past its memory budget at long context, the server must evict cache entries to keep going. Name two production grade eviction policies, describe what each keeps and what each drops, and note the bookkeeping cost of each.
Two production policies: sliding-window (keep last W, free bookkeeping) and heavy-hitter (keep tokens with highest accumulated attention weight, per-token score tracking).
Picture a chef's prep station that can only hold 20 ingredients at a time. As new ingredients come in, old ones have to go. The simple rule is 'whatever has been sitting longest, throw it out',that is sliding-window. The smarter rule is 'whatever the recipes keep reaching for, keep it; whatever has been ignored, throw it out',that is heavy-hitter. The smarter rule needs the chef to keep a tally of how often each ingredient gets used, but it preserves the important stuff better. Production kitchens often blend the two: always keep a few staples like salt at the start, and otherwise rotate the recent and the popular.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
KV cache eviction is one of the most important and least-discussed long-context serving topics. The KV cache grows linearly with sequence length, and at 1M-token contexts a single request can blow through the entire GPU memory budget for the model. Eviction policies decide which past tokens' K, V to keep and which to drop, and the choice has direct quality and throughput implications. Sliding-window is the cheapest and most-deployed; H2O-style heavy-hitter is the smartest and most quality-preserving; StreamingLLM is the production-favored hybrid.
This deep dive walks each policy in detail, explains the attention-sink phenomenon that makes the first few tokens un-evictable, surveys the orthogonal optimization of KV quantization, and closes with a decision guide for picking the right policy in 2026.
Why eviction is necessary
The KV cache stores key and value tensors for every past token at every transformer layer. For a typical decoder-only model with L layers, H heads, d_head per head, sequence length T, and batch size B at FP16:
The first factor of 2 is for K and V; the second is for FP16.
Concrete numbers
For a Llama 3 8B class model (32 layers, 32 query heads with GQA reducing to 8 KV heads, d_head = 128) at T = 100,000 tokens and B = 1:
bytes = 2 * 32 * 8 * 128 * 100_000 * 1 * 2 = 13.1 GB
For a single 1M-token request: 131 GB. That exceeds the memory of any single GPU. With batch size > 1, the situation compounds linearly in batch.
The eviction question
Eviction is the question: at the moment the KV cache cannot grow further, which entries do we drop? The answer determines both how much memory we save and how much quality we keep.
What eviction is not
- Not KV quantization: that reduces per-token bytes; eviction reduces token count.
- Not architectural KV compression like MLA: that changes the model's KV structure; eviction operates at serving time on an unchanged model.
- Not sliding-window attention: that is an architectural training-time choice; sliding-window eviction is a serving-time analog.
Eviction is a serving-time policy on top of a model whose architecture is fixed. The model does not know its KV is being trimmed.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Mistral 7B: sliding-window attention with W = 4096 is the architectural form of sliding-window eviction.
- H2O (NeurIPS 2023): the canonical heavy-hitter eviction paper; showed near-lossless quality at 20% cache size on long-context QA benchmarks.
What an interviewer would ask next. Try answering before peeking at the approach.
QWhy does dropping the first few tokens collapse the attention distribution in long generations?
The attention-sink phenomenon: softmax always sums to 1, so some 'pressure release' position must absorb excess weight when no other position is a great match. The first tokens become this sink because they are visible from every later position and tend to absorb 10-30% of total attention mass regardless of content. Dropping them forces that mass to redistribute onto semantically wrong tokens and the distribution becomes noisy.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Treating KV cache eviction as a pure capacity problem with one right answer. Quality varies by eviction policy because not all cached tokens are equally useful for future generation.
60 second bullets to scan on the way to the call.
Describe how sliding-window eviction decides which KV entries to drop
Describe the heavy-hitter / H2O scoring rule and what it retains
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.