LLMs answer fast because they save work they already did. Here's the KV cache: what it fixes, what it costs, and how people shrink the cost.

One simple idea is why modern LLMs (large language models, the AI behind chatbots) reply fast: don't redo work you already did. It's like writing a note once and reading it later, instead of working the answer out again each time.
A token is a small piece of text, like a word or part of a word. For each token, the model makes three lists of numbers: a Query (Q), a Key (K) and a Value (V).
Attention is the step where a new token looks back at the earlier tokens. It compares its query with their keys, then mixes the matching values. Here's the math:
Query_i = Token_i x W_Q
Key_i = Token_i x W_K
Value_i = Token_i x W_V
Attention(i -> j) = dot(Query_i, Key_j) / sqrt(d)
Output_i = sum_j softmax(Attention(i -> j)) * Value_j
The model writes one token at a time, and token t+1 depends on token t. So it keeps looking back at the earlier text. This is where work is wasted. The K and V for old tokens never change, but the model keeps rebuilding them anyway.
Say the prompt is The capital of France is. Step 1 computes K/V for those tokens and predicts Paris. Step 2 computes the same K/V again. So does step 3. It's like solving problems 1 to 10 all over again every time you start problem 11.
| Without KV cache | With KV cache | |
|---|---|---|
| Step 1 | Compute K/V for all prompt tokens | Compute prompt K/V once and store it |
| Step 2 | Compute old tokens again, plus the new one | Compute only the new token's K/V |
| Step 3 | Repeat the old work again | Reuse all old K/V from memory |
A model answers in two phases. In prefill, it reads the whole prompt in one go, which GPUs (the chips that do the heavy math) are great at. In decode, it writes one token per step and reads the cache every time.
The KV cache stores the keys and values, for every layer, of tokens the model already processed. A new token makes a fresh query, reads the old K/V from the cache and adds only its own.
And this is exact. It doesn't quietly make the model dumber. Old token states never change in causal decoding (a token only looks at earlier tokens). It's called memoization, which just means saving an answer so you never work it out twice.
# Simplified decode step
for L in layers:
k_new = x_new @ W_K[L]
v_new = x_new @ W_V[L]
k_all = concat(cache[L].keys, k_new)
v_all = concat(cache[L].values, v_new)
y_new = attention(q_new, k_all, v_all)
cache[L].keys.append(k_new)
cache[L].values.append(v_new)
Nothing's free. The cache swaps repeated math for memory space. Its size grows in a straight line with tokens, layers and head dimensions. It works like phone storage: the more photos you keep, the fuller it gets. Each decode step also has to read big cached tensors (large blocks of numbers). So long contexts are often slowed by memory bandwidth, which just means how fast data moves in and out of memory.
| Concern | Impact | Typical fix |
|---|---|---|
| Cache size growth | Fewer requests at once per GPU | GQA/MQA, quantized KV |
| Memory bandwidth at decode | Slow generation at long context | Better kernels and cache layout |
| Fragmentation | Wasted VRAM, unstable throughput | PagedAttention-style blocks |
| Prefix mismatch | Less cache reuse in chat turns | Stable prompt templates |
The KV cache decides what products are even possible. Without it, long-context assistants, code copilots and document chat would be slower and cost more, especially in follow-up turns. Better caches cut the cost per useful token and keep latency (the waiting time) steady under load.
Long-context models still struggle to find facts buried in the middle, a bit like missing one key line in the middle of a long book. The rules for throwing out cache entries are still mostly guesses. And speculative decoding (a small model drafts tokens ahead) has to leave the cache consistent after rejected drafts.