cd .. / course
Part 2 · Lesson 2/8

The KV cache: don't recompute the past

Every earlier token's Key and Value are computed once and stored. Without that cache, each decode step re-derives the K and V of every token before it — and the wasted work piles up as a triangular number. Count it live.

⏱ ~6 min 🎚 the triangular blow-up, counted math: some

In attention (Part 1, lesson 6) a new token's Query is scored against every earlier token's Key, then blends every earlier token's Value. Here's the thing: those earlier Keys and Values never change as you generate the next word, and the next, and the next.

So compute each token's K and V once and store them — per token, per layer, per attention head — in the KV cache. Each new step then computes only its own K and V and reads the rest straight out of memory. The Query is not cached: you only ever use the current token's Query, and a past token's Query is never needed again.

Without the cache, step t has to recompute the K/V of all t−1 tokens before it. Do that for a whole generation and the redundant work is 1 + 2 + … + n = n(n+1)/2 — the triangular blow-up. With the cache it's just n.

Recompute vs cache · counted livesaves computecosts memory
computed this step already in cache not generated yet
step 1 of 24 · generating token 1
work this step
1 K/V
cumulative so far
1 K/V

186425610242048
K/V computes · with cache
2,048
one new token per step → n
K/V computes · no cache
2,098,176
recompute the prefix → n(n+1)/2
wasted-work ratio
×1024.5
the cache does 1024.5× less work

TRY → Flip to no cache · recompute and watch the grid re-light the whole prefix every step while the "work this step" bar climbs — then drag n to 2048 and read the ratio hit ×1024.5. The two big numbers are computed straight from recomputeWork(n), not from the animation.

Show me the actual numbers (the triangular blow-up)

Generating n tokens, counting each token's K/V computation once:

n tokenswith cache (n)no cache · n(n+1)/2ratio (n+1)/2
8836×4.5
64642,080×32.5
25625632,896×128.5
10241,024524,800×512.5
20482,0482,098,176×1024.5

At n = 2048: without a cache you'd perform 2048·2049/2 = 2,098,176 token-K/V computations; with the cache, exactly 2,048 — the ratio is (2048+1)/2 = 1024.5×. The waste grows with n, because each new step drags the entire history along with it.

What's actually stored is the computed Key and Value vectors for every past token — one K and one V per token, per layer, per attention head — never the Query (a past Query is never reused) and never the raw text. The cache trades compute for memory: you stop recomputing K/V, but now you have to store them, and that storage grows linearly with context — which is exactly the next page's problem.

MythThe KV cache stores the text of the conversation so the model can "remember" it.
RealityIt stores computed Key/Value vectors for every past token — per layer and per head — so a decode step does one token's attention work instead of the whole sequence's. Never the Query, never the raw tokens.
Takeaway

Past Keys and Values never change, so cache them once and reuse them. That turns each decode step's attention work from the whole prefix into a single token — collapsing the triangular n(n+1)/2 down to n.

What does the KV cache let a decode step skip?
Why: past K/V don't change as you generate, so they're computed once and reused — turning n(n+1)/2 redundant computes into n. The weights (the next lessons' bottleneck) still stream every single step.