Why Autoregressive Generation Recomputes Work
Understanding one token ended holding a question, and this lesson answers it by counting. The tiny loop from the generation-loop lesson re-fed the entire growing sequence through the model on every trip; here we count exactly what that costs, name the two phases every generation splits into, and meet the fix. As in understanding one token, the decisive numbers are measured on the real model.
Counting the waste
Run the accounting for the loop from the generation-loop lesson at the length the graph below measures: the 6-token canonical prompt generating 32 tokens. Step one processes 6 positions, step two processes 7, and the last step processes 37. Total positions pushed through the model's 32 blocks: 6 + 7 + ... + 37 = 688.
Now count what the run actually needed. Each prompt position needs processing once: 6. Each new token needs its position processed once: 32. Necessary total: 38, the napkin convention that counts every token of the finished sequence through the model once. The naive loop did 688, eighteen times the work, and the ratio worsens as sequences grow, because the waste is a triangle: every step re-pays for the entire past. A 500-token answer on a 100-token prompt pushes 174,750 positions through the model against 600 necessary, nearly three hundred times too much.
The reason the recomputation is pure waste, and not just inefficiency, is a fact you already own. The mask from causal attention means position i can only see positions up to i, so nothing about an old position's computation changes when new tokens arrive; the loop reproduces bit-identical results for every old position, every step (measured: the shared six rows of logits agree to the last bit between a 6-position and a 7-position pass). What a new token actually needs from the past is only what attention reads: every earlier position's keys and values.
The fix, and its name
If the keys and values never change, compute them once and keep them. That is the entire idea: after each position is processed, its K and V tensors stay in memory next to the model, and every later step reads them instead of rebuilding them. The kept tensors are the KV cache.
Each step then feeds the model one token, not the whole sequence. The work per step stops growing, the triangle collapses to a line, and nothing about the output changes, because the cached values are exactly the ones the naive loop kept recomputing.
Prefill and decode
The cache splits generation into two phases with different shapes, and the field names them. The prompt's tokens are all known up front, so the model processes them together in one parallel pass, filling the cache as it goes: prefill. Generated tokens then come strictly one at a time, each conditioned on the last: decode. You can feel the boundary from outside any chat product: the pause before the first word is prefill, and the steady rhythm afterward is decode.
The measured run
Here is the whole argument on the real model, both loops generating the same 32 tokens from the canonical prompt:
The two phases now need separate treatment, because they stress the hardware in opposite ways. The next lesson measures prefill; decode measurement measures decode and lands the imbalance the course has been promising since inference engineering.