KV cache memory planner
When a transformer generates text, it keeps the keys and values of every earlier token so it doesn’t recompute them for each new token. That store is the KV cache, and at long contexts or high concurrency it can take more memory than the model’s weights.
Llama 3.1 8B with 8 sequences of 8.2K tokens needs 24.6 GB on each GPU, within the 72 GB usable.
- Weights 16 GB
- KV cache 8.6 GB
- Free 47.4 GB
- Reserved 8 GB
Model, workload and hardware
Prompt plus output, per sequence.
80 GB
The rest is left for activations and runtime overhead. vLLM’s default is 90%.
| GQA, 32 query heads share 8 KV heads | Layers | Tokens kept | Per sequence |
|---|---|---|---|
| Full attention · 8 KV heads × 128 | 32 | 8,192 | 1.1 GB |
Plain multi-head attention with every layer full would cache 524.3 KB per token here, 4× as much (75% saved).
What it shows
This calculator adds up the weights and the cache for a model, a context length and a number of concurrent sequences, then checks the total against your GPUs.
It handles the designs that shrink the cache: grouped-query and multi-query attention, multi-head latent attention, and sliding-window layers. Per token and per layer, standard attention caches 2 × KV heads × head dimension values. Latent attention caches one compressed vector plus a small positional key, shared by every head.
How to read it
- “Fits” or “Doesn’t fit” compares weights plus cache on each GPU with the memory you let the server use. The rest is reserved for activations and runtime overhead.
- The bar splits each GPU’s memory into weights, KV cache, free and reserved. The cache turns red when it doesn’t fit.
- “KV cache per token” is the average at your context length. For sliding-window layers it falls as context grows past the window.
- “Max sequences” is how many requests of this length fit at once. That number, not the weights, usually sets your throughput.
- Tensor parallelism splits the weights and KV heads across GPUs. With fewer KV heads than GPUs, heads are copied, so the cache stops shrinking.
- The table splits the cache by layer type, so you can see which layers dominate in hybrid models.
Questions
Why does the KV cache grow with context length?
Every new token attends to every earlier token in a full-attention layer, so the keys and values of all earlier tokens have to stay in memory. The cache grows linearly with context length and with the number of sequences served at once.
What is the difference between MHA, GQA and MQA?
Multi-head attention gives every query head its own key and value head. Multi-query attention shares a single key and value head across all query heads. Grouped-query attention sits between them: groups of query heads share one key and value head. Fewer KV heads means a proportionally smaller cache.
How does multi-head latent attention shrink the cache?
MLA, introduced in DeepSeek-V2, compresses keys and values into one low-rank latent vector per token per layer and caches that, plus a small decoupled key that carries positional information. The cache size no longer depends on the number of heads.
Does an 8-bit KV cache affect quality?
It halves cache memory, and the effect on output quality depends on the model and the quantisation method. Measure it on your own evaluations before relying on it.
What is a paged KV cache?
PagedAttention, from the vLLM project, stores the cache in fixed-size blocks instead of one contiguous buffer per sequence, which cuts wasted memory from fragmentation. The calculator rounds each sequence up to whole blocks when this is on.
Sources
- Shazeer (2019). Fast transformer decoding: one write-head is all you need.
- Ainslie et al. (2023). GQA: training generalized multi-query transformer models from multi-head checkpoints.
- DeepSeek-AI (2024). DeepSeek-V2: a strong, economical, and efficient mixture-of-experts language model.
- Kwon et al. (2023). Efficient memory management for large language model serving with PagedAttention.
- Jiang et al. (2023). Mistral 7B.
Explore further
- LMCacheStores and reuses KV caches across GPU, CPU and disk, so repeated prompts skip prefill.
Last updated
More tools
All tools- estimatorToken throughput estimatorEstimate decode speed, time to first token and total tokens per second for a model on your GPU.Open tool
- simulatorRequest batching simulatorRun the same LLM requests through static and continuous batching and compare latency and throughput.Open tool
- simulatorSpeculative decoding simulatorFind the draft length that speeds up LLM decoding most, and watch drafts get accepted or rejected.Open tool