The KV cache costs 2 × layers × kv_heads × head_dim × 2 bytes per token in bf16. Nothing else goes into it — not the parameter count, not the hidden size, not the number on the model page.
For Qwen3-32B that works out to 256 KiB per token. At its full 40,960-token context, one sequence holds 10.0 GiB of cache, on top of roughly 64 GB of weights. Most "why does a 4-bit 32B model still not fit" threads are about that second number, and it is not on the model card.
Four numbers, all in the model's own config.json:
num_hidden_layers how many times K and V are stored
num_key_value_heads how many independent K/V heads that layer keeps
head_dim width of each head (hidden_size / num_attention_heads if absent)
torch_dtype bf16 = 2 bytes per element
Multiply layer × kv_heads × head_dim, double it (one K and one V), double again for two bytes. That is bytes per token.
The table
I read the configs of seven public checkpoints and did the arithmetic. head_dim and num_key_value_heads are as they appear in each file; where head_dim was missing I used hidden_size / num_attention_heads.
| Checkpoint | layers | KV heads | head_dim | per token | at max context |
|---|---|---|---|---|---|
Qwen/Qwen3-32B |
64 | 8 | 128 | 256 KiB | 10.00 GiB @ 40,960 |
Qwen/Qwen3-8B |
36 | 8 | 128 | 144 KiB | 5.62 GiB @ 40,960 |
Qwen/Qwen2.5-72B |
80 | 8 | 128 | 320 KiB | 40.00 GiB @ 131,072 |
mistralai/Mistral-7B-v0.3 |
32 | 8 | 128 | 128 KiB | 4.00 GiB @ 32,768 |
microsoft/Phi-3.5-mini-instruct |
32 | 32 | 96 | 384 KiB | 48.00 GiB @ 131,072 |
openai/gpt-oss-20b |
24 | 8 | 64 | 48 KiB | 6.00 GiB @ 131,072 |
google/gemma-2-9b-it |
— | — | — | not measured | HTTP 401, gated |
Two rows in that table are worth more than the formula.
A 3.8B model can cost more cache than a 32B one
Phi-3.5-mini is about 3.8B parameters. Qwen3-32B is ten times larger. Per token, the small model costs 1.5× more cache — 384 KiB against 256 KiB — and at its own maximum context, 4.8× more, 48 GiB against 10 GiB.
The reason is one number: Phi keeps 32 key/value heads where Qwen3-32B keeps 8. Layer count is nearly the same, so the KV-head ratio decides it.
That is the useful shape of the formula. Parameters are spread over an MLP that the cache never touches. Cache is spread over attention, and only the KV side of it, and only at the depth the architecture chose.
If you are picking a model for long context, num_key_value_heads is the field to look at first. It is also the field most often missing from the marketing page.
Where I got this wrong
First version: cache grows with model size. I ordered the table by parameter count and was surprised twice. Then I ordered it by layers × kv_heads × head_dim and the surprise went away. A model's size tells you what its weights cost; only these three numbers tell you what its context costs.
One row I would have published and should not have. deepseek-ai/DeepSeek-V3 lists 61 layers, 128 KV heads and head_dim 56, which the formula turns into 1,708 KiB per token and 267 GiB at 163,840 tokens. That number is arithmetic, not truth: V3 stores a compressed latent per position rather than a separate K and V per head, so the four-number recipe does not describe its cache at all. I can compute it and I cannot defend it, so it is not in the table. If your architecture name contains "latent attention", stop and find out what it actually stores.
One row I could not read. google/gemma-2-9b-it returned 401 — the config sits behind a license acceptance. That is not measured, which is a different fact from zero, and it is the kind of gap that turns into a wrong answer if you fill it in with a guess from a blog post.
What to do with the number
Multiply by concurrency, not just by context. Eight concurrent requests at 8,192 tokens on Qwen3-32B is 8 × 8,192 × 256 KiB ≈ 16 GiB of cache — on top of weights, and before anything the runtime adds for block tables and fragmentation.
Then ask whether the runtime can store the cache in fewer bytes. An fp8 cache halves everything above, and that is a serving flag, not a config field, which is exactly why the model card cannot answer this for you.
And remember the dtype you read is the dtype the weights were trained in. Serving in 4-bit quantizes the weights; unless you also quantize the cache, the KV term stays where the table says.
What I did not measure
I did not run any of these models. These are config files and arithmetic, so the numbers are the size of the state a standard attention implementation must keep — not the memory a specific engine reports. I did not measure block-table overhead, fragmentation, or prefix-cache sharing, and I did not test any model whose cache is not two tensors per layer.
That is the honest boundary of the table, and it is also why it stays useful: it is a floor you can compute in a minute for any checkpoint you can read.
Before you size a GPU for long context: read num_hidden_layers, num_key_value_heads, head_dim and torch_dtype, and multiply.
Comments
Join the discussionNo comments yet. Be the first to comment.