KV cache calculator: how to calculate KV cache size

KV cache size = 2 × layers × KV heads × head dim × tokens × sequences × bytes per element. The 2 is for keys and values, and FP16 uses 2 bytes per element.

Qwen2.5-7B-Instruct has 28 layers, 4 KV heads and a head dim of 128. At 8,192 tokens in FP16:2 × 28 × 4 × 128 × 8,192 × 1 × 2 = 469,762,048 bytes (0.4375 GiB). At 32,768 tokens it is 1,879,048,192 bytes (1.7500 GiB). The model weights stay the same size.

These are estimates from each model's config, not measurements. How we calculate.

Calculate it for a model

2 × 28 × 4 × 128 × 8192 × 1 × 2

KV cache (raw)
0.4375 GiB

469,762,048 bytes

  • Attention: gqa. Calculator support: full. GQA formula applies.
  • Query heads 28 versus KV heads 4. Using query heads would count 3.06 GiB instead.
  • Context classification: native. Native window 32,768. No extended window is recorded.
  • This result is an estimate, not a measured trace.

What each term means

2
Keys and values
layers
Transformer layers
kvHeads
KV heads, not query heads
headDim
Head dimension
cachedTokens
Context length (cached tokens)
sequences
Concurrent sequences
bytes
FP16/BF16 = 2 · FP8/INT8 = 1

Use the KV head count from num_key_value_heads in the model's config.json. Most current models use grouped-query attention, so they have far fewer KV heads than query heads; plugging in the query heads overstates the cache several times over.

Worked GQA examples

ModelLayers × KV heads × head dim8,192 FP1632,768 FP16
Qwen2.5-7B-Instruct28 × 4 × 128469,762,048 (0.4375 GiB)1,879,048,192 (1.7500 GiB)
Qwen2.5-14B-Instruct48 × 8 × 1281,610,612,736 (1.5000 GiB)6,442,450,944 (6.00 GiB)
Qwen2.5-32B-Instruct64 × 8 × 1282,147,483,648 (2.00 GiB)8,589,934,592 (8.00 GiB)
Qwen3-8B36 × 8 × 1281,207,959,552 (1.1250 GiB)4,831,838,208 (4.5000 GiB)

One sequence, FP16. More concurrent sequences multiply the KV cache, not the weights. A 4-bit model does not get a 4-bit KV cache unless you set the KV cache type separately.

Models the basic formula does not cover

ModelAttention layersKV at 8,192KV at 32,768Fixed state
Qwen3.8-27B16 of 64536,870,9122,147,483,648150,994,944
Qwen3.8-Flash-Next12 of 48201,326,592805,306,36856,623,104

See Qwen3.8-27B VRAM and Qwen3.8-Flash-Next VRAM for the full totals, or the DGX Spark page for a 128 GB unified-memory budget.

FAQ

How do you calculate KV cache size?

Multiply 2 × layers × kvHeads × headDim × cachedTokens × sequences × bytesPerElement. For Qwen2.5-7B-Instruct at 8,192 tokens in FP16 (2 bytes per element) that is 469,762,048 bytes.

How big is the Qwen2.5-7B KV cache at 8K and 32K tokens?

469,762,048 bytes at 8,192 tokens and 1,879,048,192 bytes at 32,768 tokens, for one sequence in FP16. The weights do not change.

Do quantized weights shrink the KV cache?

No. A Q4 model still stores its KV cache in FP16 unless you also set a quantized KV cache type (for example FP8 in vLLM or q8_0 in llama.cpp).

Does this formula work for DeepSeek or Qwen3.8 models?

Not directly. DeepSeek's MLA attention stores a compressed latent instead of full keys and values. Qwen3.8 hybrids keep KV only on their attention layers, plus a fixed-size state that does not grow with context.

Open the full VRAM calculatorContext length guide

Related pages