The KV Cache

Layer 2 · Mechanics

The KV Cache

Prefill vs decode bottlenecks, cache growth mechanics, and why GQA and paging exist.

8 min read70 XP

  1. Prompt tokens arrive

  2. Prefill: parallel forward pass

    compute-bound

  3. Cache populated

  4. Decode: one token per step

    memory-bandwidth-bound

  5. Cache appended each step

The lifecycle of one request's KV cache.
PhaseParallelismBottleneckWhat grows
PrefillAll prompt tokens at onceCompute (large matmuls)Cache filled once
DecodeOne token at a timeMemory bandwidth (weights reread every step)Cache grows by one slot per step
Go deeper: L3 Code