KV cache size calculator
Every token costs 2 × layers × KV heads × head dim × bytes per value. For Qwen2.5-7B in BF16 that is 57,344 bytes, so a 110,016-token prompt holds 6.31 GB. Pick a model and see where it fits.
Last updated
Size the KV cache
Updates as you type. The page address keeps your inputs, so you can share it.
KV cache for one prompt
6.31GB
110,016 tokens of Qwen2.5 7B, keys and values, BF16
- Per token
- 57,344bytes
- All 8 sessions
- 50.5GB
- Pages of 64 tokens
- 1,719
- Bytes per page
- 3.67 MB
- KVStash blocks per prompt1
- 3,438
Does it fit?
1 The KVStash SGLang adapter on one GPU stores the keys and the values of a page as two blocks (one block per page for MLA).
Recompute vs read back
Reading the stored cache wins when its bytes arrive faster than the GPU can recompute them.
Estimate
13.7x
Faster to read back than to recompute this prompt.
An estimate, not a measurement. Real read-back also depends on the engine copying blocks into GPU memory, on the network and on the store.
Measured reference point
20.8 s→1.52 s
A 110,016-token prompt (6.31 GB of KV cache) took 20.8 s to recompute and 1.52 s to first token when read from KVStash, 13.7x faster. That is about 4.2 GB/s end to end, including the fixed 0.26 s that a GPU cache hit also pays.
How to calculate KV cache size
For every token, every layer keeps one key vector and one value vector per key-value head. Multiply those out and you have the cache.
- per token
- 2 × layers × KV heads × head dim × bytes per value
- per prompt
- per token × tokens
- total
- per prompt × concurrent sessions
- pages
- tokens ÷ page size, rounded up
- The 2 is one key plus one value. MLA models cache one shared latent instead, so it becomes 1.
- Bytes per value is 2 for BF16 or FP16 and 1 for FP8.
- Units are decimal: 1 GB is 109 bytes.
Worked example: Qwen2.5-7B, BF16, 110,016 tokens
- Read config.json28 layers, 4 KV heads, head dim 128
- Bytes per token2 × 28 × 4 × 128 × 2 = 57,344
- One prompt57,344 × 110,016 = 6.31 GB
- Pages of 64 tokens1,719 pages of 3.67 MB
- Eight sessions at once50.5 GB
- Same prompt in FP83.15 GB
Which numbers to read from config.json
Three fields from the model's Hugging Face config.json give the whole result. Two of them have traps.
Layers
num_hidden_layersOlder GLM configs call it
num_layers.KV heads
num_key_value_headsNot
num_attention_heads. With grouped-query attention several query heads share one cached head. If the field is missing, the two are equal.Head dim
head_dim, elsehidden_size / num_attention_headsGemma 2 9B sets
head_dimto 256; dividing 3584 by 16 would give 224.MLA models
kv_lora_rank + qk_rope_head_dimDeepSeek-V2 and V3 use multi-head latent attention: set the layout to MLA, KV heads to 1 and head dim to this sum. DeepSeek-V3 gives 70,272 bytes per token in BF16.
KV cache size per token for popular models
Each preset was read from the model's config.json on Hugging Face on 4 November 2026. Llama and Gemma repositories are gated, so we read the same file from a public copy.
| Model | Layers | KV heads | Head dim | BF16 bytes / token | Relative size | FP8 bytes / token | Source |
|---|---|---|---|---|---|---|---|
| Llama 3.1 8B | 32 | 8 | 128 | 131,072 | 65,536 | config.json (public copy) | |
| Llama 3.1 70B | 80 | 8 | 128 | 327,680 | 163,840 | config.json (public copy) | |
| Qwen2.5 7B | 28 | 4 | 128 | 57,344 | 28,672 | config.json | |
| Qwen2.5 72B | 80 | 8 | 128 | 327,680 | 163,840 | config.json | |
| Mistral 7B (v0.3) | 32 | 8 | 128 | 131,072 | 65,536 | config.json | |
| GLM-4-9B | 40 | 2 | 128 | 40,960 | 20,480 | config.json | |
| DeepSeek-V3 (MLA) | 61 | 1 (latent) | 576 | 70,272 | 35,136 | config.json | |
| Gemma 2 9B | 42 | 8 | 256 | 344,064 | 172,032 | config.json (public copy) |
What the KV cache formula leaves out
Treat the result as a floor for a full-attention model on one GPU. Five things move the real number.
Engine layout and padding
Engines allocate whole pages (PagedAttention), so a prompt's last page is usually part empty, and some kernels pad head dimensions.
Tensor parallelism
Across N GPUs each holds about 1/N of the KV heads. When N is larger than the number of KV heads, heads are copied and the total grows.
Sliding-window and hybrid layers
Gemma 2 and other models with local attention or linear layers need only recent tokens in some layers. Engines that use this store less.
FP8 scales
FP8 caches carry small scaling factors alongside the values. At long context they are a rounding error.
Activations and CUDA graphs
Engines reserve memory beyond the weights, so the GPU space left for the KV cache is less than the card minus the weights.
When it does not fit
Engines evict and recompute, or move the cache to host memory, disk or a shared store. KV cache offloading, explained covers the options and LMCache alternatives compares the stores.
Questions about KV cache size
Short answers, using the same formula as the calculator.
How do you calculate the KV cache size per token?
Multiply 2 (one key and one value) by the number of layers, the number of key-value heads, the head dimension and the bytes per value. For Qwen2.5-7B in BF16 that is 2 x 28 x 4 x 128 x 2 = 57,344 bytes per token. Multiply by the prompt length for one prompt, and by the number of concurrent sessions for the whole workload.
Why use KV heads and not attention heads?
Models with grouped-query attention share each key-value head across several query heads, and only the key-value heads are cached. Qwen2.5-7B has 28 attention heads but 4 key-value heads, so using 28 would overstate its KV cache seven times.
How much does an FP8 KV cache save?
Half. FP8 stores one byte per value instead of two, so a 110,016-token Qwen2.5-7B prompt drops from 6.31 GB to 3.15 GB. Engines also keep small scaling factors and may lose some accuracy; check your engine's documentation for the supported FP8 formats.
How is DeepSeek-V3 different?
DeepSeek-V3 uses multi-head latent attention (MLA). Each layer caches one compressed vector of 576 values per token (a 512-value latent plus a 64-value rotary key) that all heads share, with no separate keys and values. In BF16 that is 61 x 576 x 2 = 70,272 bytes per token.
Size it, then measure it.
If the numbers say your prompts outgrow GPU memory and come back often, tell us your engine, model, GPUs and prompt lengths. We will say what we would measure, and when KVStash is not worth it.
Or write to rudray@kvstash.com