▚VRAM Calculator▋

// guides

KV Cache Explained — Why Long Context Eats Your VRAM

What the key-value cache is, the formula behind it, and why GQA, sliding-window, hybrid and MLA models differ by up to 40 times at long context.

reviewed 2026-11-15 · by Bedino

A transformer language model generates text one token at a time. To produce token n, every attention layer needs the keys and values of all the previous tokens. Recomputing them each step would be hopelessly slow, so the runtime stores them: that store is the KV cache. It lives in GPU memory next to the weights, and for long contexts it can be the largest thing in there.

The formula

For standard multi-head or grouped-query attention, the cache costs, per token:

2 (keys and values) × layers × KV heads × head dimension × bytes per element

Multiply by the context length and by the number of concurrent sequences for the total. The “bytes per element” is 2 for the default FP16 cache, 1 for an 8-bit cache and about 0.5 for a 4-bit one.

Take Llama 3.1 8B: 32 layers, 8 KV heads, head dimension 128, FP16:

2 × 32 × 8 × 128 × 2 bytes = 131,072 bytes = 128 KiB per token

At 8,192 tokens that is 1 GiB. At 131,072 tokens (128k) it is 16 GiB — more than the entire Q4 model.

Why models of the same size differ so much

The formula has one lever the model designers control: how many KV heads there are, and whether every layer keeps a full-length cache at all. Modern architectures pull both.

Multi-head attention (MHA) keeps a key/value head for every query head. This is the old, expensive design and is rare in new models.

Grouped-query attention (GQA) shares each KV head between several query heads. Llama 3 8B has 32 query heads but only 8 KV heads — a 4× saving. Almost every current dense model uses GQA. The cache is still linear in context.

Sliding-window attention (SWA) limits most layers to a window of recent tokens, so those layers stop growing once the window is full. Gemma 3, gpt-oss and several others interleave a few global layers (which keep everything) with many windowed ones.

Hybrid attention + state-space models (Qwen3-Next, Qwen3.8, Nemotron-H, Jamba and others) replace most attention layers with recurrent or state-space layers whose state has a fixed size. Only the few remaining attention layers hold a growing cache.

Multi-head latent attention (MLA) — used by DeepSeek V2/V3/R1, Kimi K2 and GLM — does not store full keys and values. It stores one small latent vector per token per layer and reconstructs keys and values from it. The cache per token is layers × (latent size + RoPE key size) × bytes, with no head multiplier.

The difference at long context

Same FP16 cache, a single sequence, GiB — computed from each model’s real configuration:

Model Attention 8k 32k 128k
Llama 3.1 8B GQA 1.0 4.0 16.0
Qwen3 32B GQA 2.0 8.0 32.0
Llama 3.3 70B GQA 2.5 10.0 40.0
Gemma 3 27B sliding window 1.0 2.9 10.4
Qwen3.8 27B hybrid 0.5 2.0 8.0
DeepSeek-V3 (671B) MLA 0.5 2.1 8.6

A 671-billion-parameter DeepSeek model has a smaller 128k cache (8.6 GiB) than Llama 3.1 8B (16 GiB), and Qwen3.8 27B beats the 8B too. That is why you cannot read memory needs off the parameter count alone — and why a calculator that applies one GQA formula to every model gets MLA and hybrid models badly wrong. This site branches the maths on each model’s attention type.

Shrinking the cache

  1. Quantize it. The 70B model’s 32k cache is 10.0 GiB in FP16, 5.0 GiB at 8-bit and 2.5 GiB at 4-bit. In llama.cpp this is the --cache-type-k / --cache-type-v options; vLLM offers an FP8 cache.
  2. Use less context. Context you do not use costs nothing if the runtime allocates lazily, but most runtimes reserve the full window up front, so set it to what you need.
  3. Pick an efficient architecture. For long-context work the cache is a better reason to choose a model than its parameter count.
  4. Share prefixes. Serving engines such as vLLM use paged attention and prefix caching so that requests with a common system prompt do not each store their own copy.

Concurrency multiplies it

The cache is per sequence. Serving eight users at 8k with Llama 3.1 8B means eight caches, 8 GiB in total, before activations. Sizing a server is mostly sizing the cache: the weights are a fixed cost, the cache is what you spend on users. Our sister site, finetune-calc.com, has a serving concurrency calculator built on exactly this.

Try it

The KV cache calculator shows the bytes per token for any model in the list and the longest context that fits in your spare VRAM. The VRAM estimator adds the cache to the weights and overhead for a full total.

faq
Does the KV cache exist during prompt processing too?
Yes. As the model reads your prompt it writes one key and one value entry per layer for every token. By the time generation starts, the cache already holds the whole prompt, and every new token adds one more entry. Size the cache for prompt plus expected output.
Is it safe to quantize the KV cache?
For most models an 8-bit cache is very close to lossless and halves the memory. A 4-bit cache saves more but costs measurable quality on some models and tasks, particularly long-context retrieval. Test on your own workload before adopting it.
Why is the cache per token so different between models of the same size?
Because it depends on the attention design, not the parameter count. Grouped-query attention shares key/value heads, sliding-window layers stop growing after the window, hybrid models have only a few attention layers, and multi-head latent attention stores a compressed vector instead of full keys and values.
Does a bigger context window in the model card mean I can use it?
Only if you have the memory. A model that supports 128k tokens still needs the cache for 128k tokens when you fill it. The advertised window is a limit, not a reservation.
more guides