Explainer
KV cache size per token: one formula does not fit every model
The KV cache formula, computed from real configs for 12 model types, and what it means for capacity, prefix hits and handoff.
- LLM inference
- KV cache
- Disaggregated serving
- GPU infrastructure
- Attention
The key-value (KV) cache holds the keys and values that attention has already computed, so a model does not compute them again for every new token. Its size per token is usually given as one formula: 2 × layers × KV heads × head dimension × bytes per element. For Qwen3-8B in BF16 that is 2 × 36 × 8 × 128 × 2 = 147,456 bytes, or 144 KiB, for every token of every request.
That formula is correct for Qwen3-8B. For many models released since 2024 it is wrong, each in a different way, and the error flows into every number built on it: how many requests fit on a GPU, whether a prefix cache hits, and how long the handoff takes between prefill and decode workers. This essay computes the cache for twelve model types from their published configurations, and shows where the formula breaks.
Hover a part to see its nameHow big is the KV cache per token?
Five numbers decide it. Attention stores a key and a value (the 2) for every layer, for every KV head, across the head's dimension, at the size of one element.
\begin{aligned}
\text{KV bytes per token} &= 2 \times \text{layers} \times \text{KV heads} \\
&\quad \times \text{head dimension} \times \text{bytes per element}
\end{aligned}For Qwen3-8B in BF16:
2 \times 36 \times 8 \times 128 \times 2 = 147{,}456 \text{ bytes} = 144 \text{ KiB}
Hover a part to see its namePrecision is the factor an operator controls. An FP8 cache holds the same tokens in half the memory, 73,728 bytes per token for Qwen3-8B. NVFP4 stores 4-bit values plus one FP8 scale for every 16 of them, so it takes 41,472 bytes, about 28 percent of BF16. The other four come from the model.
The per-token number grows quickly into the numbers that matter.
Hover a part to see its nameWhy a long request can outweigh the model
Qwen3-8B has about 8.2 billion parameters, or 15.3 GiB of weights in BF16. Its native context is 32,768 tokens, where one request's cache is 4.5 GiB. With YaRN scaling to 131,072 tokens, one request's cache is 18 GiB: larger than the model that produced it.
How many requests fit on one GPU
Capacity is what remains after the weights. An H100 80GB reports about 79.6 GiB. With vLLM's gpu_memory_utilization set to 0.9 (its default before v0.20; 0.92 since), the engine takes 90 percent of that: about 71.6 GiB. Other engines count differently. SGLang computes mem_fraction_static at startup from the GPU and its settings. TensorRT-LLM's free_gpu_memory_fraction (default 0.9) is a share of the memory left after the weights. The weights take 15.3 GiB. The remainder, about 56 GiB, also holds activations and CUDA graph buffers, so at most about 50 requests of 8K tokens fit at once, not the 64 that 72 GiB would suggest.
Hover a part to see its nameTwo more effects change the real number. Engines allocate the cache in fixed blocks of tokens, so the last block of every request is partly empty. Tensor parallelism also splits KV heads across GPUs only while there are enough heads to split.
Hover a part to see its name
Hover a part to see its nameHere is the formula as code. It reads a model's config.json from Hugging Face and prints the first answer. The sections that follow show when that answer is wrong.
import json
import sys
config = json.load(open(sys.argv[1]))
config = config.get("text_config", config) # Multimodal configs nest the text model.
element_bytes = 2 # BF16. Use 1 for FP8.
if "kv_lora_rank" in config: # Latent attention (MLA): one shared row per token per layer.
per_layer = (config["kv_lora_rank"] + config["qk_rope_head_dim"]) * element_bytes
elif config.get("multi_query"): # Older configs, such as Falcon-7B, mark one shared KV head.
head_dim = config["hidden_size"] // config["num_attention_heads"]
per_layer = 2 * 1 * head_dim * element_bytes
else:
# Falcon-40B and its relatives name the KV heads num_kv_heads.
heads = config.get("num_key_value_heads") or config.get("num_kv_heads") or config["num_attention_heads"]
head_dim = config.get("head_dim") or config["hidden_size"] // config["num_attention_heads"]
per_layer = 2 * heads * head_dim * element_bytes
print(per_layer * config["num_hidden_layers"], "bytes per token, if every layer keeps a full cache")Which model types break the formula
Fewer KV heads: MHA, GQA and MQA
Older models gave every query head its own key and value head: multi-head attention (MHA). Grouped-query attention (GQA) lets several query heads share one KV head. Multi-query attention (MQA) lets all of them share a single one. The formula still holds; only the head count changes.
Hover a part to see its nameLlama-2-7B, with 32 KV heads, stores 524,288 bytes per token: 3.6 times Qwen3-8B. A concurrency estimate reused across these designs is off by that factor.
Why MLA does not split across GPUs
DeepSeek-V3 uses multi-head latent attention (MLA). It stores one compressed row per token per layer: a 512-wide latent and a 64-wide position part. In decode, every query head reads that same row directly. Across 61 layers in BF16 that is 70,272 bytes per token, less than half of Qwen3-8B for a model about 80 times larger.
Hover a part to see its nameThe row belongs to no single head, so tensor parallelism cannot split it by head. Under tensor parallelism alone, each GPU holds the whole row, and per-GPU cache does not shrink as GPUs are added. Decode context parallelism (vLLM's --decode-context-parallel-size) shards the cache across tokens instead. In practice, MLA models usually run with data-parallel attention (SGLang's --enable-dp-attention, or data and expert parallelism in vLLM), which keeps each request's cache on one GPU and avoids the copies.
Hover a part to see its nameDeepSeek-V3.2 adds sparse attention: a small indexer chooses which earlier tokens each new token reads. That reduces the work per step, not the storage. Every token is still stored, plus an indexer key: 78,324 bytes per token in BF16, a little more than DeepSeek-V3. In FP8 the number depends on the GPU. On Hopper, vLLM's FlashMLA sparse backend stores the fp8_ds_mla row: the latent in FP8, the position part kept in BF16, and four scale values, for 48,068 bytes per token. On Blackwell, vLLM prefers FlashInfer's sparse backend, which keeps a standard FP8 row: 43,188 bytes. Plain MLA, such as DeepSeek-V3, stores a standard FP8 row on both: 576 bytes per layer, 35,136 bytes per token. An engine that packs the FP8 row differently stores a slightly different number.
Hover a part to see its nameSliding windows
In gpt-oss-120b, half of the 36 layers attend only to the last 128 tokens. Those layers stop growing once the window is full, at 4.5 MiB per request in total. The other 18 layers grow at 36,864 bytes per token. The formula counts every layer as full, so it overstates the cache by about two times at long context.
Hover a part to see its nameMixture of experts
A mixture-of-experts (MoE) model routes each token through a few of many feed-forward experts. The experts hold most of the weights, but they keep no cache. The cache is set by attention alone. Qwen3-30B-A3B, a model nearly four times larger than Qwen3-8B, stores 98,304 bytes per token, less than Qwen3-8B.
Hover a part to see its nameHybrids keep a fixed state
Hybrid models replace most attention layers with layers that keep a fixed-size state instead of a growing cache: Mamba layers in Nemotron-H, linear attention in Qwen3-Next. RWKV-6 keeps no attention cache at all. What a request holds is no longer one thing: a small cache that grows, plus a state that does not.
Hover a part to see its nameOf Nemotron-H-8B's 52 layers, 4 are attention layers and 24 are Mamba layers; the rest hold nothing per request. Its cache grows at 16,384 bytes per token. With the state in BF16, the state is 49.4 MiB per request and equals 3,162 tokens of its own cache. For short requests the state is most of its memory. At 8K tokens the request holds 177.4 MiB, still 6.5 times less than Qwen3-8B. vLLM keeps Nemotron-H's state space model (SSM) state in FP32 by default, for accuracy. Then the state is 97.4 MiB and equals 6,234 tokens of cache, and an 8K request holds 225.4 MiB, 5.1 times less than Qwen3-8B.
Hover a part to see its nameSharing and images
Cross-layer attention (CLA) lets adjacent layers read one cache. Sharing between pairs halves it, and the paper also tests groups of three. YOCO (You Only Cache Once) goes further: half of its layers read a single shared cache, and the other half keep a cache of constant size, a short window in its sliding-window variant. Vision-language models turn an image into ordinary cache positions: a 1024 by 1024 pixel image becomes 1,024 tokens, 144 MiB on Qwen3-VL-8B.
Hover a part to see its name
Hover a part to see its nameAll twelve, side by side
| Model | Type | Bytes per token | Fixed state per request |
|---|---|---|---|
| Llama-2-7B | MHA | 524,288 | none |
| Qwen3-8B | GQA | 147,456 | none |
| Qwen3-VL-8B | GQA, images as tokens | 147,456 | none |
| Qwen3-30B-A3B | GQA, mixture of experts | 98,304 | none |
| DeepSeek-V3 | MLA | 70,272 | none |
| DeepSeek-V3.2 | MLA, sparse | 78,324 | none |
| gpt-oss-120b | GQA, sliding window | 36,864 | 4.5 MiB once the window is full |
| Qwen3-Next-80B-A3B | Linear attention hybrid | 24,576 | 37.7 MiB |
| Nemotron-H-8B | Mamba hybrid | 16,384 | 49.4 MiB in BF16 (97.4 MiB in vLLM's default FP32) |
| Falcon-7B | MQA | 8,192 | none |
| CLA models | Shared across layers | 2 times less (pairs), up to 3 | none |
| RWKV-6 | Recurrent | 0 | about 33 MiB for the 7B model (FP32 state) |
Hover a part to see its nameEvery number here is computed from public configurations, not measured. Every cache figure is BF16, so the rows compare designs, not precisions; BF16 bytes per token are model math, the same on every engine. Nemotron-H's state is shown in BF16, where vLLM's default keeps it in FP32. RWKV-6's state is shown in FP32, as its row says. Real engines add block padding, a memory reserve, CUDA graph buffers and, for FP8, scale tensors. Treat the table as the floor a measurement starts from. The data is available as CSV.
Why a hybrid model's prefix match is fragile
A prefix cache lets a new request reuse the cache of an earlier request that started with the same tokens, such as a long system prompt. The shared part is not computed again.
Hover a part to see its nameA hybrid model cannot reuse its attention cache alone. It also needs the fixed state as it was at the end of the shared part. vLLM saves that state only at a few block boundaries, and its blocks for hybrid models are large: 528 tokens for Qwen3.5-4B in BF16. In plain single-node vLLM, if the saved state is missing, the engine discards the attention match too. A KV connector that accepts partial hybrid hits can keep it.
Hover a part to see its namevLLM issue #45238 shows how sharp the geometry is. With Qwen3.5-4B, 528-token blocks and prompts of about 2,200 tokens, a build from before vLLM's Marconi-style admission policy (#37898) kept saved states at tokens 1,584 and 2,112. With a 1,600-token shared prefix, the state at 1,584 fell inside the shared part, and 52 of 64 requests hit. With a 1,500-token shared prefix, both fell in the unique part, and none hit. #37898 saves a state at the last block boundary inside a shared prefix once it has been seen, here token 528. The issue's author re-measured with it: 40 of 64 requests hit, but cache hits covered only 25.5 percent of the queried prompt tokens, against 54.0 percent with the longer prefix. The total loss is gone, but much of the reuse is still lost, and nothing in vLLM's metrics shows why. --prefix-match-unit can add saved states inside a short prefix, but it is off by default.
Hover a part to see its namePrefix caching on hybrids also has a cost when nothing hits. vLLM issue #60008 measured 13.4 to 16.2 percent lower output throughput (medians of six rounds; the issue's title says 11 to 16) on one NVFP4 Mamba hybrid on four GB200 GPUs. Prefix caching was on and nothing hit, on a vLLM build with changes not yet merged. Later comments report a prototype that closes most of the gap. Both issues were open when this essay was published.
Hover a part to see its nameImages add one more rule. Two requests with the same text and different images must not share a cache, so the cache key must include the image.
Hover a part to see its nameWhat crosses the wire between prefill and decode
In disaggregated serving, one group of GPUs runs prefill and another runs decode, so the cache moves between them once per request. Its size is the per-token number times the prompt length, plus any fixed state.
Hover a part to see its name| Model | At 8K tokens | One 400G NIC | Eight 400G NICs | NVLink 4 |
|---|---|---|---|---|
| Llama-2-7B | 4.0 GiB | 86 ms | 10.7 ms | 9.5 ms |
| Qwen3-8B | 1.125 GiB | 24 ms | 3.0 ms | 2.7 ms |
| Qwen3-30B-A3B | 768 MiB | 16 ms | 2.0 ms | 1.8 ms |
| DeepSeek-V3 | 549 MiB | 11.5 ms | 1.4 ms | 1.3 ms |
| gpt-oss-120b | 292.5 MiB | 6.1 ms | 0.8 ms | 0.7 ms |
| Qwen3-Next-80B-A3B | 229.7 MiB | 4.8 ms | 0.6 ms | 0.5 ms |
| Nemotron-H-8B | 177.4 MiB | 3.7 ms | 0.5 ms | 0.4 ms |
Real transfers are slower than line rate.
Hover a part to see its nameThe layout on each side matters as much as the size. When prefill and decode use different numbers of GPUs, KV heads must be regrouped in flight. For MLA, every decode GPU needs the whole row, so the same bytes are sent to each one.
Hover a part to see its nameWhen reloading beats recomputing
A cache that does not fit in GPU memory can move to host memory, a local SSD or a remote store, and come back when a request needs it.
Hover a part to see its nameMoving it back is worth it only when it is faster than computing it again. Reload time is the cache size divided by the tier's bandwidth, plus a fixed cost to start the transfer. Recompute time is the prefill time for those tokens: the model's compute per token times the tokens, plus an attention term that grows with the square of the length and matters mostly past tens of thousands of tokens. So the comparison is bytes per token against compute per token. The fixed start cost favors recomputing very short prompts. A slower tier or a larger cache per token favors recomputing. A model with a small cache and heavy compute per token, such as an MLA mixture of experts, favors reloading. Where the line falls depends on numbers that only your hardware gives.
Hover a part to see its nameFor the same reason, sparse attention does not make reloading a whole offloaded prefix cheaper: it reads fewer tokens per step, but the prefix must carry all of them.
Questions to ask any new model's config
Before you size a deployment for a new model, answer these from its config.json and its paper:
- What attention type does each layer use: full, sliding window, linear, Mamba or none?
- How many KV heads does each attention layer have, and is the head dimension stated separately?
- Does it store a latent row (MLA) instead of separate keys and values?
- Which layers have a window, and how long is it?
- Is there a fixed state per request, and how large is it in the dtype the engine uses?
- Do any layers share a cache?
- At your tensor-parallel size, is the cache split, or copied to every GPU?
The KV cache calculator runs these numbers for your model, context, GPUs and link.
Hover a part to see its nameQuestions engineers ask
Does an FP8 KV cache halve memory?
It halves the bytes per token, from 2 to 1 per element. Scale tensors add a small amount, and accuracy needs its own check for each model.
Why does my engine report fewer tokens than the formula?
The formula ignores block padding, the memory reserve, activations and CUDA graph buffers. Use the formula as the upper bound and the engine's startup log as the measurement.
Is the KV cache the same as the prefix cache?
No. The KV cache holds one request's keys and values. A prefix cache keeps those blocks after the request ends, so a later request with the same start can reuse them.
References
- Qwen3-8B config (opens in a new tab) and the Qwen3 technical report (opens in a new tab)
- Llama-2-7B config (opens in a new tab), the public copy of Meta's gated repository
- Falcon-7B config (opens in a new tab)
- DeepSeek-V3 config (opens in a new tab); MLA in DeepSeek-V2 (opens in a new tab)
- DeepSeek-V3.2 config (opens in a new tab)
- gpt-oss-120b config (opens in a new tab)
- Qwen3-30B-A3B config (opens in a new tab)
- Nemotron-H-8B config (opens in a new tab) and the Nemotron-H paper (opens in a new tab)
- Qwen3-Next-80B-A3B config (opens in a new tab)
- Qwen3-VL-8B config (opens in a new tab)
- RWKV-6: Eagle and Finch (opens in a new tab)
- MQA: Fast Transformer Decoding (opens in a new tab); GQA: Ainslie et al. (opens in a new tab)
- Mamba: Gu and Dao (opens in a new tab)
- CLA: Brandon et al. (opens in a new tab); YOCO: Sun et al. (opens in a new tab)
- vLLM issues #45238 (opens in a new tab) and #60008 (opens in a new tab), both open on 7 October 2026
- Related work: Sebastian Raschka's KV cache calculations (opens in a new tab), which covers the attention families; this essay adds precision, blocks, tensor-parallel copies, transfer cost and the prefix-cache behavior of hybrids.