Inference

KV cache

A KV cache stores attention keys and values from earlier tokens during autoregressive decoding.

t1t2t3step 1newstep 2savednewstep 3savedsavednewOnly the new token is computed at each step.
A KV cache stores attention keys and values from earlier tokens during autoregressive decoding. Values shown in the worked visual are illustrative unless the labels state otherwise.

How it works

Without the cache, every new token would recompute projections for the full prefix. The cache saves compute but grows with sequence length, layer count, and key-value width.

A concrete example

Illustrative: 32 layers, 32 KV heads, head width 128, 2 bytes, and 2,048 tokens require about 1 GiB.

Do not confuse it with Prefix cache

a KV cache belongs to an active sequence; prefix caching reuses shared prefixes across requests.

Study the full lesson