KV cache size: the formula, with real models

When a language model generates text, it keeps the keys and values it computed for every earlier token, in every layer, so it does not have to compute them again for the next one. That store is the KV cache. It is the part of GPU memory rules of thumb leave out, and at long context or with many users it outgrows the model's weights.

The formula

For each token, every layer stores one key vector and one value vector per key-value head:

KV bytes per token = 2 × layers × key-value heads × head dimension × bytes per value
KV cache           = KV bytes per token × context length × concurrent sequences

The 2 counts the key and the value. A value takes 2 bytes at FP16 or BF16 and 1 byte at FP8. Layers, key-value heads and head dimension are fixed by the architecture — num_hidden_layers, num_key_value_heads and head_dim in the model's config.json — which is why two models of the same size can need very different caches. Models with multi-head latent attention replace the middle of the formula; see GQA, MQA and MLA.

A worked example: Llama 3.1 8B Instruct

Llama 3.1 8B Instruct has 32 layers, 8 key-value heads and a head dimension of 128. At FP16 that is 2 × 32 × 8 × 128 × 2 = 131,072 bytes, or 128.0 KiB, per token. One user at 8k tokens needs 1.00 GiB of cache; at its full 131,072-token context, 16.00 GiB — more than its 14.96 GiB of BF16 weights.

Parameter count is not what decides it. Qwen2.5 7B Instruct and Llama 3.1 8B Instruct are close in size — 7.62B and 8.03B parameters — but Llama 3.1 8B Instruct stores 128.0 KiB per token and Qwen2.5 7B Instruct 56.0 KiB, a 2.3× difference that comes from layers and key-value heads.

Per-token cache in popular models

ModelLayersKV heads × head dimPer token8k32k128k
Llama 3.2 3B Instruct288 × 128112.0 KiB896 MiB3.50 GiB14.00 GiB
Qwen2.5 7B Instruct284 × 12856.0 KiB448 MiB1.75 GiB—
Llama 3.1 8B Instruct328 × 128128.0 KiB1.00 GiB4.00 GiB16.00 GiB
Mistral Nemo 12B Instruct408 × 128160.0 KiB1.25 GiB5.00 GiB20.00 GiB
Qwen3 32B648 × 128256.0 KiB2.00 GiB8.00 GiB—
Llama 3.3 70B Instruct808 × 128320.0 KiB2.50 GiB10.00 GiB40.00 GiB
Qwen2.5 72B Instruct808 × 128320.0 KiB2.50 GiB10.00 GiB—
DeepSeek V361MLA68.6 KiB549 MiB2.14 GiB8.58 GiB

FP16, one sequence. —: beyond the model's maximum context. DeepSeek V3 caches a compressed latent rather than per-head keys and values. Gemma 2 and gpt-oss are left out of this table: their sliding-window layers stop growing at the window, so their cost per token falls as the context grows. The calculator counts that layer by layer.

Concurrency multiplies it

Every concurrent sequence holds a cache of its own. On one H100 SXM 80GB, with 72.82 GiB usable, Llama 3.1 8B Instruct at BF16 leaves room for 55 users at 8k tokens each, 13 at 32k and 3 at 128k. The weights set the floor; the cache decides how many people fit above it.

What shrinks it, and what does not