KV Cache Calculator

KV cache = 2 × layers × KV heads × head dimension × bytes × context × users. Llama 3.1 8B at FP16 needs 128 KiB per token, so 8,192 tokens take 1 GiB; Llama 3.1 70B at 128K context needs 40 GiB per user (model config.json, checked 16 October 2026).

Upper bound with every user at full context. Model shapes checked 2026-10-16.

Did this solve your problem?
Prefer us on Google

How to use

  1. Pick a model to fill in layers, KV heads and head dimension, or type the values from the model's config.json.
  2. Set the context length, the number of concurrent users (sequences) and the KV cache precision.
  3. Optionally enter the GPU memory left after loading the weights to see the longest context and the most users that fit.

How it works

KV cache per token = 2 × layers × KV heads × head dimension × bytes per value. The 2 is one key and one value tensor. Bytes per value is 2 for FP16 or BF16, 1 for FP8 or INT8, 0.5 for INT4.

KV cache = per token × context length × concurrent users. It is the same formula the GPU VRAM calculator uses, and it assumes every user fills the whole context (an upper bound; paged attention only allocates what is used).

Max tokens in a budget = free memory ÷ per token. Max users = free memory ÷ (per token × context). Results are in GiB (2^30 bytes).

Layer and head values come from each model's config.json on Hugging Face or its technical report, checked 16 October 2026. Models with sliding window or latent attention (such as Gemma 2 or DeepSeek MLA) store less than this formula says.

KV cache at FP16

ModelPer token8K context128K context
Llama 3.1 8B128 KiB1 GiB16 GiB
Llama 3.1 70B320 KiB2.5 GiB40 GiB
Qwen2.5 7B56 KiB0.44 GiB7 GiB
Qwen2.5 32B256 KiB2 GiB32 GiB

Sources: The Llama 3 Herd of Models (arXiv 2407.21783) · vLLM PagedAttention (arXiv 2309.06180)Last reviewed 2026-10-23

FAQ

How big is the KV cache of Llama 3.1 8B?

128 KiB per token at FP16: 2 × 32 layers × 8 KV heads × 128 × 2 bytes. An 8,192 token context is 1 GiB and the full 131,072 tokens is 16 GiB.

Why does the KV cache grow with users?

Each sequence keeps its own keys and values, so 8 users with 8K context need 8 times the memory of one. Shared prefixes with prefix caching can lower this; the calculator does not assume it.

Does FP8 KV cache halve the memory?

Yes. One byte per value instead of two, so the same memory holds twice the tokens. Quality loss is usually small but depends on the runtime.

Why is my real usage lower?

Runtimes such as vLLM allocate KV blocks as tokens arrive, and some architectures (sliding window, MLA) store less. This tool gives the full-context upper bound.