Sizing an LLM server by hand: the KV-cache maths
A worked example that sizes Llama 3.1 8B on one H100 on paper: weights, KV cache, how many requests fit, and how fast decoding runs once memory bandwidth sets the pace.
You can size most of a self-hosted inference server with a pencil. Two numbers decide it: how many bytes the model and its cache occupy, and how many bytes the GPU can read per second. Here is the arithmetic, every multiplication shown, for Llama 3.1 8B Instruct on one H100 SXM 80 GB.
The architecture comes from the model’s config.json. The official repository is gated on Hugging Face, so I read a public mirror whose file has the same git hash as the original. The hardware comes from NVIDIA’s H100 page: 80 GB of memory and 3.35 TB/s of bandwidth for the SXM card.
My assumptions, stated once:
- A chat workload: 512 prompt tokens, 512 output tokens.
- BF16 weights and BF16 KV cache.
- 90% of GPU memory available to the server, 1.6 GiB of runtime overhead.
- 70% of peak bandwidth and 50% of peak compute actually achieved.
- A target of 30 output tokens per second per user.
These are the defaults of InfraCheck, the planner I built for these sums, so you can check every number against it.
1. Weights: parameters times bytes
The repository metadata lists 8,030,261,248 parameters. You can rebuild that from the config: two 128,256 × 4,096 matrices (vocab_size × hidden_size) for the input embeddings and the untied output head, plus 32 layers of 218,112,000 parameters each.
BF16 2 bytes 8,030,261,248 × 2 = 16,060,522,496 bytes = 16.06 GB = 14.96 GiB
FP8 1 byte 8,030,261,248 × 1 = 8.03 GB = 7.48 GiB
INT4 0.5 byte 8,030,261,248 × 0.5 = 4.02 GB = 3.74 GiB
INT4 formats also store scales per group of weights. I ignore that here, so read the INT4 line as a lower bound.
2. KV cache per token
At every layer, attention turns each token into a key and a value that later tokens need. Keeping them is the KV cache, and the 2 in the formula is simply K plus V:
bytes per token = 2 × layers × KV heads × head dimension × bytes per element
The config has 32 layers (num_hidden_layers), 32 attention heads and 8 KV heads (num_key_value_heads). There is no head_dim field, so the head dimension is 4,096 / 32 = 128.
2 × 32 × 8 × 128 × 2 bytes = 131,072 bytes = 128 KiB per token
The difference between 32 attention heads and 8 KV heads is grouped-query attention. Ainslie et al. (2023) let several query heads share one key-value head, and report quality close to full multi-head attention at speeds close to a single shared head. Here, four query heads share each KV head. With 32 KV heads the cache would be 512 KiB per token, four times larger.
Per request, 1,024 tokens × 128 KiB = 128 MiB. At the model’s full 131,072-token window it is 16 GiB for one sequence, more than the weights.
3. How many requests fit
usable memory 80 GiB × 0.90 = 72.00 GiB
minus weights 14.96 GiB
minus overhead 1.60 GiB
KV budget 72.00 − 14.96 − 1.60 = 55.44 GiB
per request 1,024 tokens × 128 KiB = 0.125 GiB
fit 55.44 / 0.125 = 443 concurrent requests
vLLM’s engine arguments list a default --gpu-memory-utilization of 0.92 as of October 2026; I keep 0.9 to match InfraCheck. I also treat the card’s 80 GB as 80 GiB, as InfraCheck does. Read as 80 × 10⁹ bytes, the same sum gives 404. Trust what nvidia-smi reports on your card.
Context length changes everything. For a RAG workload with 4,096 prompt tokens and 512 output tokens, each request holds 4,608 × 128 KiB = 0.5625 GiB, and only 55.44 / 0.5625 = 98 fit.
4. Decoding is a bandwidth sum
Each decode step produces one token per active sequence. The GPU reads every weight once, plus every sequence’s KV cache. The arithmetic is cheap and the reading is slow, so step time is roughly bytes read divided by bandwidth.
A request’s context grows from 512 to 1,024 tokens, so on average it holds 768 tokens: 768 × 131,072 = 100.7 MB. Usable bandwidth is 3.35 TB/s × 0.7 = 2.345 TB/s.
users bytes read per step step time per user aggregate
1 16.06 GB + 1 × 0.1007 = 16.16 GB 6.89 ms 145 tok/s 145 tok/s
32 16.06 + 32 × 0.1007 = 19.28 GB 8.22 ms 122 tok/s 3,890 tok/s
350 16.06 + 350 × 0.1007 = 51.29 GB 21.87 ms 45.7 tok/s 16,000 tok/s
The weights are read once per step whatever the batch, so their cost is shared. Every extra user adds its own KV bytes. That is why aggregate throughput climbs steeply, then flattens.
When does compute become the limit? The roofline model says it happens when a workload does more arithmetic per byte than the chip’s ratio of compute to bandwidth. NVIDIA lists 1,979 BF16 teraFLOPS with sparsity, so about 989 dense. That gives 989 / 3.35 ≈ 295 FLOP per byte. Decoding does about 2 FLOP per parameter per token:
compute at 350 users 2 × 8.03e9 × 350 = 5.62 TFLOP
at 989 × 0.5 = 495 TFLOP/s → 11.4 ms, below the 21.9 ms memory time
Per extra user, the KV read costs 42.9 µs and the arithmetic 32.5 µs. At this context length, compute never catches up. It binds only with short contexts or a compressed cache.
5. Prefill shares the same GPU
Prefill processes the prompt in one pass. It is compute-bound:
2 × 8.03e9 × 512 tokens = 8.22 TFLOP (+ 0.03 for attention) → 16.7 ms at 495 TFLOP/s
Every millisecond spent on prefill is a millisecond not spent on decode. InfraCheck models a closed loop: each user sends a new request as soon as the previous one ends. At 30 tok/s, 350 users finish 350 × 30 / 512 ≈ 20.5 requests per second. Each new prompt needs 16.7 ms, so prefill takes 20.5 × 16.7 ms ≈ 34% of every second. Decode keeps the other 66%: 45.7 × 0.66 = 30.0 tok/s per user.
That is exactly the target. This is how the InfraCheck planner arrives at 350 concurrent requests for this setup: memory allows 443, and the 30 tok/s target stops it at 350. The closed loop has no thinking time between messages, so it is a pessimistic view of chat.
Serving engines soften this. In vLLM’s V1 engine, chunked prefill is on by default where possible: long prompts are split and batched with ongoing decodes, and max_num_batched_tokens trades inter-token latency against time to first token.
6. A contrast: DeepSeek-V3
The DeepSeek-V3 config breaks two assumptions above: it uses multi-head latent attention, and it is a mixture of experts.
Multi-head latent attention, introduced in DeepSeek-V2, caches one compressed latent per layer plus a small key that carries position information, not separate keys and values per head. So there is no factor of 2. The config gives kv_lora_rank 512, qk_rope_head_dim 64 and 61 layers:
(512 + 64) × 61 layers × 2 bytes = 70,272 bytes ≈ 68.6 KiB per token
That is about half of Llama 3.1 8B’s 128 KiB, for a much larger model. With plain multi-head attention over its 128 heads (keys of 192 dimensions, values of 128), it would be 61 × 128 × 320 × 2 = 4.77 MiB per token, 71 times more.
The technical report gives 671B total parameters, with 37B activated per token. The two numbers answer different questions:
- Memory uses the total. At FP8 the weights are about 671 GB. That is already more than an 8 × H100 node’s 640 GB, before any cache. Eight H200s at 141 GB each give 1,128 GB.
- Decode cost uses the active set, at small batch. One sequence reads about 37 GB per step. Eight H200s at 4.8 TB/s give 38.4 TB/s, so just under 1 ms per step at full bandwidth, before any communication between GPUs.
The second point has a catch. Each token picks 8 of 256 routed experts. Different users pick different experts, so a batch touches more of them. With uniform routing, the share of experts read per step is 1 − (1 − 8/256)^b: 22% at 8 sequences, 64% at 32 and 98% at 128. At large batches, a step reads close to all the weights, but compute per token stays at the active 37B. InfraCheck uses the active-parameter figure, so for MoE at high concurrency treat its decode speed as optimistic.
7. The levers, ranked by what they buy
For the 4,096 + 512 RAG workload, here is the same sum with one or two changes. The memory column is hand arithmetic; the last is InfraCheck’s result once the 30 tok/s target and prefill count.
| Configuration | KV budget | Per request | Fit by memory | Within 30 tok/s |
|---|---|---|---|---|
| BF16 weights, BF16 KV | 55.44 GiB | 0.5625 GiB | 98 | 51 |
| BF16 weights, FP8 KV | 55.44 GiB | 0.28 GiB | 197 | 67 |
| FP8 weights, FP8 KV | 62.92 GiB | 0.28 GiB | 223 | 116 |
| 2 × H100, tensor parallel | 125.84 GiB | 0.5625 GiB | 223 | 106 |
| Prompt halved to 2,048 | 55.44 GiB | 0.3125 GiB | 177 | 101 |
The FP8 KV cache halves bytes per token. vLLM enables it with kv_cache_dtype="fp8" and recommends calibrated scales for the best quality. FP8 weights halve both the footprint and the bytes read per step, so they help memory and speed at once. Tensor parallelism splits weights and cache across GPUs, adding memory and bandwidth at some communication cost; InfraCheck applies an 8% tax at two GPUs.
The lever underneath all of these is allocation. A server that reserved the full 131,072-token window for every request would fit 55.44 / 16 = 3 of them. PagedAttention (Kwon et al., SOSP 2023) stores the cache in fixed-size blocks, allocated as tokens arrive. The authors report “near-zero waste in KV cache memory” and 2 to 4 times the throughput of FasterTransformer and Orca at the same latency. Every sum above assumes paged allocation.
Common mistakes
- Forgetting the KV cache. The weights fit, so the model “fits”. At a 4k context, the cache for 98 users is nearly four times the size of the weights.
- Using total parameters for MoE decode at small batch. For one DeepSeek-V3 sequence, it overstates bytes per step by 18 times. At large batch, the error flips the other way.
- Mixing GB and GiB. Weights are 16.06 GB or 14.96 GiB. Reading the card as 80 × 10⁹ bytes moved the answer from 443 to 404.
- Ignoring prefill. Here it took a third of the GPU and turned 45.7 tok/s into 30.
- Assuming 100% bandwidth. That predicts 207 tok/s for one user. Databricks measured 60% bandwidth utilisation at batch size 1 on two H100s in 2023; measure your own stack.
What this looks like in practice
- Read
config.json: layers, KV heads, head dimension, vocabulary, and the attention type. - Weights = parameters × bytes. Use total parameters for memory.
- KV per token = 2 × layers × KV heads × head dim × bytes, unless the model uses MLA or another compressed cache.
- KV budget = usable memory − weights − overhead. Divide by the cache for one request at your real context.
- Step time ≈ (active weights + all KV) / (bandwidth × 0.6–0.7). Check compute with 2 FLOP per active parameter per token.
- Take prefill’s share off decode, then compare per-user speed with your target.
- Run a load test. The paper answer tells you which hardware to test, not what it will do.
Sources
- Meta, Llama-3.1-8B-Instruct model card, July 2024.
- Red Hat AI, Llama-3.1-8B-Instruct config.json (byte-identical mirror of Meta’s file), May 2025.
- NVIDIA, H100 Tensor Core GPU specifications, October 2026.
- NVIDIA, H200 Tensor Core GPU specifications, October 2026.
- Ainslie, Lee-Thorp, de Jong, Zemlyanskiy, Lebrón and Sanghai, GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints, EMNLP, May 2023.
- Kwon, Li, Zhuang, Sheng, Zheng, Yu, Gonzalez, Zhang and Stoica, Efficient Memory Management for Large Language Model Serving with PagedAttention, SOSP, September 2023.
- Williams, Waterman and Patterson, Roofline: An Insightful Visual Performance Model for Floating-Point Programs and Multicore Architectures, UC Berkeley technical report, October 2008 (published in Communications of the ACM, April 2009).
- DeepSeek-AI, DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model, May 2024.
- DeepSeek-AI, DeepSeek-V3 Technical Report, December 2024.
- DeepSeek-AI, DeepSeek-V3 config.json, December 2024.
- vLLM, Engine arguments, October 2026.
- vLLM, Optimization and tuning, October 2026.
- vLLM, Quantized KV cache, October 2026.
- Agarwal, Qureshi, Sardana, Li, Quevedo and Khudia (Databricks), LLM Inference Performance Engineering: Best Practices, October 2023.