‹ All demos
25 Nov 2026 · AI

Read only the parts of an AI's memory that matter for this word

Every time a language model writes a word, it looks back over its whole memory of the conversation (the KV cache). With long documents that re-reading is the slow part. RefineKV splits the memory into blocks and keeps a one-line summary of each. For every new word it asks, block by block, "would the summary change the answer?" It opens the full block only when the answer could be yes. Below, the paper's error bound runs live on a simulated attention head: change the threshold and watch which blocks get opened, how much is read, and how far the answer drifts.

Kewei Lian, Jiayi Zhai, Anji Liu

Why it matters

Long-context chatbots, coding agents and reasoning models spend most of their time re-reading a huge memory for every word they write, which makes them slow and expensive to run. By opening only the blocks a provable error bound says are risky, RefineKV made the attention step up to 7.6× faster and decoding up to 1.66× faster on Llama-3.1 and Qwen-2.5, while matching full-attention quality at its cautious setting.

The paper

RefineKV: Query-Adaptive KV Cache Compression via Attention-Error Bounds

Kewei Lian, Jiayi Zhai and Anji Liu (National University of Singapore). arXiv:2611.28171 [cs.LG], submitted 20 November 2026. Read on arXiv · PDF · Project page

In plain language: attention mixes stored value vectors vi. Each is weighted by how well the current query q matches that token's stored key ki:

o = Σi αi vi,    αi = exp(q·ki/√d) / Σj exp(q·kj/√d)

The authors first measured how much of the cache each query really needs. On their data, keeping 90% of a query's attention weight needed only 17.6% of the cache. But the needed pieces were different for every query, and together they covered 96.4% of it. So throwing parts of the cache away for good (eviction) is bound to hurt some later word. The choice of what to read has to be made fresh for every query.

RefineKV cuts the cache into contiguous blocks C and stores each block's mean key k̄C and mean value v̄C. A summarised block acts like |C| copies of its mean, so it gets weight p̂C ∝ |C| exp(q·k̄C/√d). The output error splits into attention moving between blocks plus value blur inside them. Both are controlled by the block's distortion δC = maxi∈C |q·(ki − k̄C)|/√d, which says how differently the block's tokens respond to this query. The paper proves (Eq. 4):

‖o − ô‖ ≤ 2 ΣC RC,    RC = p̂C [ Vmax (cosh δC − 1) / ΣC′ p̂C′ cosh δC′ + VC tanh(δC/2) ]

Here VC is the largest value norm in the block and Vmax the largest overall. Blocks with RC > τ are opened and read token by token. The rest are read as their single summary. The risk mixes importance (p̂C) with distortion (δC). An important block must be very uniform to stay summarised, while an unimportant one can be messy. Computing δC exactly would mean reading the block, so the paper uses cheap upper bounds from stored per-block extremes. One is the axis-aligned box bound (Eq. 83), from each block's coordinate-wise min and max keys. The other is a diagonal-ellipsoid bound (Eq. 87).

Try it: one attention head over a 1,024-token document

The "document" is a string of topics, each 20–120 tokens long. A token's key is its topic's direction plus some scatter (head size d = 32). Each decoding step's query drifts from one topic to the next, the way a model's focus moves while it writes. Everything below is recomputed live: exact attention, the paper's risk score RC for every block, the mixed exact-plus-summary attention, and its error.

δC from: Decoding:
Cache entries read this step (opened tokens + summaries)
Output error ‖o − ô‖ / ‖o‖
Blocks opened this step
Blocks opened at least once, steps 0 → now

The cache this step: exact attention per token, and what RefineKV reads
exact attention αi (√ scale)opened (read token by token)summarised (read as 1 entry)opened at some step so fartopic boundaries
Importance vs distortion (cf. the paper's Fig. 2)
openedsummarisedRC = τ (typical VC)
Error vs share of cache read, this query
RefineKV (sweep τ)open top blocks by p̂C onlyoracle: top tokens only, rest dropped● current τ
Per-step reads vs everything ever needed
tokens read this stepunion of opened blocks so far
Under the hood this step

How to use it

Demo maths: a synthetic single head (no grouped-query attention). N = 1,024 tokens, d = dv = 32. There are 24 topic vectors with N(0, 1) components, and keys k = t + σ·N(0, I). Values are a per-topic mean plus noise, scaled so that a typical ‖v‖ ≈ 1. The query is |q| · unit(a blend of two topic vectors that hops every 8 steps, + 0.5·N(0, I) noise). Blocks are contiguous. Opened tokens enter the softmax with exp(q·ki/√d); each summarised block enters once with weight |C| exp(q·k̄C/√d) and value v̄C, as in the paper. "Entries read" counts opened tokens plus one per summarised block. The selection pass also reads per-block metadata (k̄C, plus min/max keys for the box bound), which is not counted. On these synthetic keys the summed bound 2ΣRC is loose, and can exceed the trivial bound 2Vmax. The paper reports how often that happens on real models (its Table 11). RefineKV only uses the per-block terms to rank and threshold blocks. The oracle curve keeps the top-n tokens by exact αi and renormalises. The 17.6% / 96.4% figures, τ = 0.01 and the speed-ups are from the paper; every other number on this page comes from the simulation.