kv-eviction -- a constant-size KV cache over a growing context

A model's notes on a conversation cannot grow forever, so eventually a server has to throw some of them away. Keeping only the most recent stretch is the obvious choice and it is the one that breaks: models put a large share of their attention on the opening few words whatever those words actually say, so discarding them wrecks everything after. Keeping a handful of opening words permanently, alongside the recent stretch, repairs most of it — on real models the published finding is that four of them suffice and that one or two do not. The other family of policies keeps whichever words the model has been looking at most, mixed with recent ones.

Interactive page: the context keeps growing, the KV cache must not. Three eviction policies decide what to forget, all holding the same number of slots, and the page scores each one by how much of the real attention mass its survivors still carry. Anchor: A3 attention pattern / KV cache (Family B).

What it shows

A row of token slots is the cache. Above it, this step's full-cache attention distribution -- the truth every policy is graded against. Below it, each token's accumulated attention, which is what makes some of them worth keeping. Evicted slots are struck through and visibly gone; the mass they were carrying turns grey in the bar above.

PolicyKeepsWhy it is here
Sliding windowthe last B tokensthe obvious policy, constant memory, and the one that fails -- the first tokens go first
Sinks + sliding windowthe first S tokens forever + the last B−SStreamingLLM (arXiv:2309.17453): the same budget, rescued by keeping a handful of initial tokens. On real models the paper reports FOUR suffice and that one or two do not — this page's toy recovers at two, which is a property of the toy, not the published result
Heavy-hitter evictionthe top B tokens by accumulated attention, wherever they sitH2O (arXiv:2306.14048): evict the lowest accumulated score when the cache fills. This is the greedy rule alone — H2O as evaluated splits the same budget between heavy hitters and recent tokens, and its paper says keeping either alone does not hold up

The score panel shows all three at once, so the ranking is visible on load rather than after three clicks: kept attention mass, as a percent of the full-cache baseline (100% = the policy discarded nothing this step wanted). Cache size cannot separate the policies -- it is constant and identical for all three -- so the score is the only thing that does.

The point the page is built to land: the naive policy is the one that looks most reasonable and is the one that breaks. Attention concentrates on the first few positions regardless of their content, so a window that evicts them loses a large share of the distribution while looking perfectly well-behaved. Dragging the sink bracket from 0 to 4 hands most of it back at the same budget.

The attention is synthetic, and shaped on purpose

There is no model behind the page. The distribution is built from three additive logit terms that stand in for the reported behaviour: a strong, content-independent pull toward the first positions (the attention-sink phenomenon -- this is exactly why dropping token 0 is catastrophic), a recency term, and a fixed per-token content salience drawn from the seed (which is what plants a heavy hitter in the middle where no window can see it). Everything downstream -- the eviction order, the scores, the tooltips -- is then computed honestly from that distribution, so the picture and the number can never disagree. The seed slider redraws the content without touching the shape.

How this differs from the sibling pages

PageSubject
attention-patternsthe mask: which keys a query is allowed to read, per pattern. Sliding-window and sink appear there as attention patterns over a full N×N grid; nothing is ever deleted.
kv-cachethe cache filling: K/V columns appended per decode step, and the memory that grows with them. Append-only -- no policy, no eviction.
kv-eviction (this page)what happens when the cache is capped: which entries are thrown away, in what order, and what that costs in retained attention mass.

The distinction is worth stating because it is the one readers collapse: a sliding-window mask and a sliding-window eviction policy look alike on a diagram, but only the second one destroys state it can never get back. This page starts where kv-cache stops growing.

Render tier

T2 (WebGL2-tier surface; slots, bars and handles are drawn as Canvas2D overlays for the kept / sink / heavy-hitter / evicted colouring).

Wiring

layout.mount + controls (policy, budget B, sinks S, N, seed) + a per-token Transport that auto-plays and loops, so the cache visibly fills, hits its cap and starts evicting. Per the interactivity contract:

Headless hooks (--screenshot has no pointer, so every handle has a URL twin): ?step=N, ?play=1, ?policy=window|sink|h2o, ?w=N (budget), ?sinks=N, ?n=N, ?seed=N, ?hover=x,y. Source: page.js.