Datasets:
Download articles/kv-cache-math.md from inference-engineering-academy/llm-inference-notes: direct link, hf CLI and curl.
- Browser
- Download file 3.74 kB
-
https://huggingface.co/datasets/inference-engineering-academy/llm-inference-notes/resolve/main/articles/kv-cache-math.md
- Command line
-
hf download hf://datasets/inference-engineering-academy/llm-inference-notes/articles/kv-cache-math.md
-
curl -L -o kv-cache-math.md https://huggingface.co/datasets/inference-engineering-academy/llm-inference-notes/resolve/main/articles/kv-cache-math.md
title: 'KV cache math: why Llama 3.1 8B needs ~128 KB per token'
tags: llm, inference, gpu, machinelearning
If you serve LLMs, the KV cache, not the weights, usually decides how many users fit on one GPU. The good news is that its size is easy to calculate from config.json, and the calculation explains a lot of serving behaviour.
What the KV cache stores
In every attention layer, each token produces a key vector and a value vector. During decoding, a new token's query has to attend over the keys and values of all earlier tokens. Because of the causal mask those earlier K/V vectors never change, so engines compute them once and keep them in GPU memory. That stored state is the KV cache. Without it, every decode step would recompute the whole prefix.
The formula
bytes per token = 2 × num_layers × num_kv_heads × head_dim × bytes_per_element
- 2: one key plus one value
- num_layers:
num_hidden_layers - num_kv_heads:
num_key_value_heads. This is the number of KV heads, not query heads. - head_dim:
head_dim, orhidden_size / num_attention_headsif the field is missing - bytes_per_element: 2 for BF16/FP16, 1 for FP8
Llama 3.1 8B, worked out
From its config: 32 layers, 32 query heads, 8 KV heads (grouped-query attention), hidden size 4096, so head_dim = 4096 / 32 = 128. At BF16:
2 × 32 × 8 × 128 × 2 = 131,072 bytes = 128 KiB per token
Now scale it:
| Workload | KV cache |
|---|---|
| 1 request × 8,192 tokens | 1 GiB |
| 1 request × 131,072 tokens (full context) | 16 GiB |
| 32 requests × 8,192 tokens | 32 GiB |
The BF16 weights of the 8B model take about 16 GB (~15 GiB). A single full-context request needs more memory for cache than the model needs for weights. A model that "fits" on a 24 GB card can still go out of memory as soon as real traffic arrives.
The most common mistake: using query heads
If you plug in 32 attention heads instead of 8 KV heads, you get 512 KiB per token, which is 4× too high. Grouped-query attention lets 4 query heads share each KV head, and that sharing is exactly why Llama 3 can offer long contexts. For Llama 3.1 70B (80 layers, 8 KV heads, head_dim 128) the same formula gives 320 KiB per token, about 40 GiB at full 128K context.
What this means when you serve
- Concurrency budget. Free memory after weights ÷ (bytes per token × average sequence length) ≈ how many requests you can hold at once. On an 80 GB GPU running the 8B model with roughly 60 GB left for cache, that's about 480K tokens in total: ~60 requests at 8K, but only 3 at full context.
- FP8 KV cache halves it. At 1 byte per element you get 64 KiB per token. Engines like vLLM and SGLang support this; check quality on your own workload.
- Paging matters. Engines that preallocate the maximum length waste most of this memory. PagedAttention (vLLM) and radix-tree caches (SGLang) allocate in blocks, so memory follows actual length.
- Decode speed falls with context. Every decode step reads the whole cache for that sequence, so long contexts cost bandwidth as well as capacity.
A quick calculator
def kv_bytes_per_token(layers, kv_heads, head_dim, dtype_bytes=2):
return 2 * layers * kv_heads * head_dim * dtype_bytes
per_tok = kv_bytes_per_token(32, 8, 128) # Llama 3.1 8B
print(per_tok / 1024, "KiB/token") # 128.0
print(per_tok * 131072 / 2**30, "GiB @ 128K") # 16.0
Run this for any model before you choose a context length, a GPU, or --max-num-seqs.
If you want to go deeper, the free Inference Engineering Foundations course at inference-engineering.xyz works through these numbers for Llama 3.1 8B with runnable NumPy code.