AI engineering and inferenceLoading progress…

Growth with context

Calculate KV-cache memory and explain how layers, KV heads, context length and concurrent sequences affect it.

The cache grows with every token

Every retained token adds a fixed amount of state: a key and a value for each KV head in each layer. In bytes:

bytes per token = 2 × layers × KV heads × head size × bytes per value

For L = 32, H_KV = 8, head size 128 and 2 bytes per value that is 2·32·8·128·2 = 131,072 bytes, or 128 KiB. A 4096-token sequence therefore needs 4096 × 128 KiB = 512 MiB, and doubling the context doubles it.

layers L32KV heads8head size128bytes per value2context tokens4,096sliding window (optional)1,024
per token = 2 (K and V) × 32 × 8 × 128 × 2 = 131,072 bytes (128 KiB) full cache for 4,096 tokens = 512 MiB with a 1,024-token window = 128 MiB
contextcache
1,024128 MiB
2,048256 MiB
4,096512 MiB
8,1921 GiB
16,3842 GiB

Doubling the context doubles the cache, if every retained token contributes the same state. Windowed or hybrid layers cap what they retain and need their own accounting.

Change the model shape and the context. Add a sliding window to see a capped cache.

Predict first. If every retained token contributes the same state, what happens when the context doubles?

Chapter context

Causal attention can reuse keys and values for earlier tokens rather than recomputing them at every generated step. This saves work but creates state that grows with the number of retained token positions.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

For a uniform full-attention decoder:

MKV=2LHKVdhTBb.M_{KV}=2L H_{KV}d_h T B b.

L is layers, H_KV KV heads, d_h head dimension, T retained tokens per sequence, B equal-length unshared sequences and b bytes per value. The factor 2 counts keys and values. For unequal lengths, replace TB by the sum of retained lengths.

Worked example

For L=32, H_KV=8, d_h=128 and b=2, each retained token needs 2×32×8×128×2=131,072 bytes, or 128 KiB. At T=4096, one sequence needs 512 MiB. Four equally long independent sequences need 2 GiB, before allocator overhead and other memory.

Reference code

The following example uses the Python standard library and does not call a remote service.

layers,heads,dim,bytes_per_value=32,8,128,2
per_token=2*layers*heads*dim*bytes_per_value
assert per_token == 131072
assert per_token/1024 == 128
assert per_token*4096/1024**2 == 512
assert per_token*4096*4/1024**3 == 2

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

Use KV heads rather than query heads. Distinguish decimal GB from binary GiB. Hybrid, sliding-window and quantized-cache architectures require their own detailed accounting.