Generating text is autoregressive: the model produces one token, appends it, and runs again.
At each step, self-attention needs a key and a value for every token in the sequence so far.
Computing those from scratch every step would repeat almost the entire prefix's work each time.
For a thousand-token response that is roughly a thousand redundant passes over the same text.
The KV cache removes the redundancy by storing each token's keys and values the first time they are computed.
A generation step then computes a key and value for one new token and appends them to the cache.
Attention reads the whole cache but only writes one column, so the per-step cost becomes linear.
This is why the first token of a response is slow and the rest arrive quickly.
That first pass, filling the cache from the prompt, is called prefill and is compute-bound.
The steps after it are decode, and they are bound by memory bandwidth rather than arithmetic.
The cache is not small: its size scales with layers, heads, context length and batch size together.
On long-context serving it routinely exceeds the memory taken by the model weights themselves.
That is the pressure behind multi-query and grouped-query attention, which share keys and values across heads.
It is also why serving systems such as vLLM page the cache in blocks rather than one contiguous slab.
Understanding the cache explains most of what is otherwise mysterious about inference cost and latency.