Inference Engineeringestimator

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.

Fits

Llama 3.1 8B with 8 sequences of 8.2K tokens needs 24.6 GB on each GPU, within the 72 GB usable.

Per GPU · 1 × 80 GB24.6 GBNeeded of 72 GB usable
Memory on each GPU
  • Weights 16 GB
  • KV cache 8.6 GB
  • Free 47.4 GB
  • Reserved 8 GB
0 B20 GB40 GB05K10K15KContext length (tokens)KV cache per GPUCache budget8.2K tokens

Model, workload and hardware

Prompt plus output, per sequence.

Weights
KV cache

80 GB

The rest is left for activations and runtime overhead. vLLM’s default is 90%.

KV cache per token131.1 KBAverage at 8.2K tokens
KV per sequence1.1 GB8.2K tokens
KV total8.6 GB8 sequences
Weights16 GB8B parameters
Max sequences52At 8.2K tokens each
Max context53.4K tokensAt 8 sequences

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

Explore further

  • LMCacheStores and reuses KV caches across GPU, CPU and disk, so repeated prompts skip prefill.

Last updated

More tools

All tools

Your first senior-level
lesson is free.

Choose a course, open a free lesson, and explore an idea you’ve wanted to understand.