Attention re-reads everything — unless you keep notes
To pick word #10, attention must compare against a key and value vector for every one of the 9 words before it. A naive model recomputes all of them, every step. A cached model computes each token's K/V once, files it away, and only computes the newest one. Step through generation and watch what actually gets computed (gold = computed this step, grey = pulled from the cache).
Each square is one token's key/value pair. Real models compute one K/V pair per layer per head — same story, multiplied a few thousand times.
The race — quadratic re-reader vs linear note-keeper
Give both models the same compute budget per second and let them race to generate N tokens. The no-cache model pays 1+2+3+…+N units (each token costs more than the last); the cached model pays 1 unit per token, flat. Watch the odometers — the gap is not a constant factor, it widens with every token.
At 19 tokens the cache is exactly 10× cheaper — drag the slider right and the ratio keeps climbing. Nothing is hardcoded: both odometers burn identical units/second.
The memory meter — your chat physically lives in VRAM
The cache isn't free: every token's K/V vectors sit in GPU memory for the whole conversation. Per token that's 2 × layers × model-width × bytes. Pick a model, a GPU and a precision, then drag the context slider and watch the cache eat the memory left over after the weights — until it hits the wall.
Why long chats get slow — and long-context APIs get expensive
Two consequences of the cache living in VRAM. Speed: to write each token, the GPU must stream the weights plus your entire cache through its memory bus — bigger cache, slower tokens. Money: each user's cache claims exclusive memory, so longer contexts mean fewer users per GPU. Drag the context slider and watch users vanish from the grid while the price climbs.
Back-of-envelope model: 7B weights at fp16 (14 GB), full-attention fp16 KV, decode assumed memory-bandwidth-bound. Real serving stacks add paging, GQA and batching tricks — the shape of the curve is what's real.
Evict the notes, forget the plot
When the cache won't fit, one fix is a sliding window: keep only the last W tokens' K/V and silently drop the rest. But attention can only look at entries that still exist. A secret door code is stated early in this chat. Keep chatting, shrink the window, then ask for the code — and watch what the model does when the answer has been evicted.
Conceptual demo — the "model" here is a lookup, but the eviction rule is exactly how sliding-window attention and StreamingLLM-style caches behave.