KV Cache Calculator

Size the key-value cache for any transformer. Pick a model or enter your own architecture, then set context length, concurrent requests and precision to see what fits on the GPU you have.

Please verify you are human to use the calculator.

What the calculator models

Byte-exact arithmetic

Two tensors per layer, per KV head, per token. The tool shows the substituted formula under the chart so you can check the number rather than trust it.

Three attention families

Cache size follows KV heads, not attention heads, and latent attention drops heads entirely. Grouped-query, MLA and windowed models each get their own formula.

Batch and headroom

Every concurrent request carries its own cache. Set a GPU and the tool reports the longest context that fits and how many requests you can serve at your chosen length.

Quantisation trade-off

BF16, 8-bit, 4-bit and 3-bit are drawn together, so the memory a lower precision buys is visible against the model weights line before you commit to it.

KV cache memory in practice

Autoregressive generation recomputes attention over the whole sequence at every step, which costs time proportional to the square of the sequence length. Caching the key and value tensors removes that recomputation and makes each new token cheap. The cost moves to memory: every token you keep in context occupies bytes in the cache for as long as the request is alive.

The arithmetic is unforgiving at long context. Llama 3.1 8B holds 16 GB of weights in BF16, and across its full 131,072-token window the cache also reaches 16 GB, the size of the model itself. Add a second concurrent request at that length and the cache, not the weights, becomes the largest object on the card. This is why long-context serving is usually a memory-capacity problem rather than a compute problem, and why grouped-query attention and cache quantisation arrived when they did.

Two levers change the answer. Architecture sets the per-token cost: grouped-query attention cuts it by the ratio of query heads to KV heads, which for Llama 3.1 is four. Precision scales it directly, so an 8-bit cache halves the memory of BF16 and a 4-bit cache quarters it. Both are visible in the chart above. For the derivation, the timing experiment behind it and a NumPy implementation of the cache filling token by token, see KV cache memory and LLM inference. The architectural variants that shrink the cache further are covered in KV sharing, MQA, GQA and MLA, and the compression side in TurboQuant vector quantisation. The other way the cache grows is a loop that keeps appending to its own context, as a ReAct agent does on every step. For how these numbers behave under real serving load, with continuous batching and paged attention, see continuous batching and paged attention.

Sizing the cache is one half of an inference budget; the other half is what the tokens cost you if someone else runs the model. The LLM cost calculator covers that side, and teams weighing a self-hosted deployment against a frontier API generally need both numbers in front of them before committing to hardware.

Related Tools

Stay in the loop

Get notified when new tutorials and deep-dives are published. No spam, unsubscribe anytime.

KV cache FAQ

Common questions about inference memory, GQA and cache quantisation