Understanding Transformers · Part 14 of 16
KV Caching
Why generation stays fast even as the text grows — by not recomputing the past.
The intuition: the past doesn't change, so don't recompute it
During training, the model processes a whole sequence in parallel. During generation it produces one token at a time, feeding each new token back in to produce the next. Done naively, every new step would recompute the Keys and Values for every earlier token, in every layer — repeating almost all of the previous step's work.
But under a causal mask (Part 8), an earlier token's representation never depends on a token that comes after it. Once the model has processed “The cat sat on the,” adding “mat” doesn't change anything about “cat.” So “cat”'s Key and Value, computed once, stay valid for the rest of the generation. Storing them and reusing them is the KV cache.
Cache the Keys and Values, not the old Queries
A Query is only used while its own token is the current position being processed. Once that step is done, no future token needs that old Query again. Keys and Values are different: every future token will compare its new Query against the old Keys and pull from the old Values. So the cache holds K and V, and discards each Query after it's used.
At generation step t, for each layer: compute the new token's qt, kt, vt; append kt, vt to that layer's stored tensors; score qt against all cached Keys; blend the cached Values.
outputt = softmax(scorest) [V1 … Vt]
What it saves, and what it costs
| Benefit | Cost |
|---|---|
| Skips recomputing every past token's K/V at each step, so token-by-token generation stays fast. | Memory grows with context length — every layer must retain all past Keys and Values. |
| Lets each new token attend to the full history. | Reading a large cache eats memory bandwidth and eventually becomes the bottleneck. |
Longer contexts need proportionally more cache. Grouped-query attention trims the cost by using a smaller number of Key–Value heads, each shared by a group of Query heads (Ainslie et al., 2023).