How context length changes KV cache memory

Raw KV is linear in cached tokens and in concurrent sequences. Weights stay put for a shared replica.

Updated 2026-09-19. Figures use calculator 0.1.0 unless the article says otherwise.

For uniform full-attention layers the raw KV payload is:

2 × layers × KV heads × head dim × cached tokens × sequences × bytes per element

Independent check used in tests: 32 layers, 8 KV heads, head dim 128, 8,192 tokens, 1 sequence, 2 bytes/element = 1,073,741,824 bytes (1 GiB). Four times the context is four times that raw KV. Four concurrent sequences on one replica is also four times the raw KV, with weights unchanged.

GQA must use KV heads. Qwen2.5-7B-Instruct has 28 query heads and 4 KV heads. Pretending there are 28 KV heads overstates cache by 7×.

KV dtype is independent of weight quant. A Q4_K_M weight file with FP16 KV is normal in llama.cpp. Hugging Face documents cache layouts in the Transformers KV cache guide.

MLA models such as DeepSeek-V2-Lite-Chat are marked unsupported for this formula. This version will not fake a GQA number for them.

Open the calculator