KV Cache Memory Calculator
The KV cache is the part of an LLM that grows with every token you ask it to remember - and the first thing that runs out when your context gets long. This calculator sizes it exactly: pick a model (or enter layers, KV heads and head dimension yourself), set your context length, batch size and cache precision, and get the cache in GB, its cost per token, what quantizing it saves, and how much context still fits on your GPU.
1 · Model architecture
Presets carry real published values from each model's config.json. Standard models use grouped-query attention (GQA): the cache is sized from KV heads and head dimension. DeepSeek-style models use multi-head latent attention (MLA), where K and V share one compressed latent vector per token - enter its rank plus rope dimension instead.
2 · Context, batch & precision
The cache grows linearly with context length and batch size. Precision sets bytes per element - quantized caches carry a small per-block scale overhead that this calculator includes.
3 · GPU budget
Used for the VRAM-share verdict and the max-context column. Only 92% of a GPU's VRAM is treated as usable (driver and display reserve). The reserve is what you set aside for weights and runtime overhead.
Where the cache lands
Your KV cache against the usable VRAM of the selected GPU after the weight reserve.
Precision ladder
The same context at every cache precision: what it costs, how much you save versus F16, and the longest context that still fits in your VRAM budget. The selected precision is highlighted.
| Precision | Bytes / token | Cache @ your context | % of F16 | Max ctx in VRAM |
|---|
How the cache grows with context
Total cache at your chosen precision and batch size, from a 4k chat up to a 1M-token document. The row matching your current context is highlighted.
| Context | Cache size | % of usable VRAM |
|---|
Run it
Ready-to-run settings for the context and precision you chose. In llama.cpp, --cache-type-k / --cache-type-v quantize the key and value caches independently; F16 is the default when the flags are omitted.
Why the KV cache deserves its own budget
A transformer does not re-read its whole history on every new token. Instead, as it generates, it stores one key vector and one value vector per layer, per attention head, per token - the KV cache - and attends to those stored vectors instead of recomputing them. That trade is what makes autoregressive generation linear in the number of tokens rather than quadratic, but it has a price: the cache is live memory, allocated in GPU VRAM or system RAM, and it grows with every token of context you keep. On a long-document workload the cache can exceed the model's own weights, which is why "my 8B model fits in 6 GB" stops being true the moment you ask for a 128k window. Sizing the cache before you launch the server - rather than discovering the limit from an out-of-memory crash or a silent runtime offload - is what this calculator is for.
How the size is calculated
For standard grouped-query attention, each token costs 2 × layers × KV heads × head dimension elements per sequence: one key and one value (the factor of 2), each with head dimension, replicated across the model's transformer layers. Multiply by the bytes-per-element of your cache precision and by context length, then by batch size for parallel sequences. Two details keep the estimate honest. First, grouped-query models with few KV heads - Llama 3.1 and Qwen2.5 use 8 or 4 where the attention head count is 32 or 40 - cache far less per token than multi-head models of the same size, which is why the preset list carries each model's real KV head count rather than assuming one. Second, the quantized precisions are not exact byte counts: llama.cpp stores Q8_0 and Q4_0 caches in blocks of 32 elements with a floating-point scale per block, so the effective cost is about 1.125 bytes per element for Q8_0 and 0.625 for Q4_0, not a clean 1.0 and 0.5. This calculator uses the overhead-inclusive figures so your plan does not run out of room at load time. For DeepSeek-style multi-head latent attention the formula changes shape entirely: keys and values are both derived from a single compressed latent vector per token per layer, sized by the latent rank plus the rope head dimension (512 + 64 for DeepSeek-V3 and R1), with no factor of two. That compression is why a 671B-parameter model keeps a cache of roughly 70 KB per token at F16 - about a thirtieth of what an equivalently deep GQA model would need.
How to use this calculator
- Pick your model from the preset list. For a custom model, read the three numbers from its
config.json:num_hidden_layers,num_key_value_heads(fall back tonum_attention_headsfor full multi-head models), and head dimension ashidden_size ÷ num_attention_heads. If the model card mentions a compressed KV cache or latent attention, switch the attention type to MLA and use its latent rank and rope dimension instead. - Set the context length you actually intend to run - not the model's advertised maximum. The quick chips cover 4k through 256k; type any value for longer windows. Set batch size above 1 only if you run parallel sequences (multiple users or concurrent requests against one server process), because each sequence keeps its own cache.
- Choose the precision your server will use. If you are unsure, Q8_0 is the safe default: about half the size of F16 with negligible quality loss in practice. FP8 gives the same halving on hardware that supports it natively.
- Select your GPU preset and enter how much of its VRAM the weights and runtime overhead will occupy (the reserve). The verdict cards then tell you the total cache, its per-token cost, and what fraction of usable VRAM it claims; the precision ladder adds the longest context that fits at each precision.
- Take the numbers with you: copy the report as text for a ticket or chat, download the CSV with both ladders, or copy the share link so someone else sees the exact same setup.
A worked example: Llama 3.1 8B at 32k context
Llama 3.1 8B has 32 layers, 8 KV heads and 128-dimensional heads, so each token costs 2 × 32 × 8 × 128 = 65,536 elements - 128 KiB at F16. A 32,768-token context is therefore 4.29 GB of cache for a single sequence: on a 12 GB card with roughly 6 GB reserved for a Q4_K_M copy of the weights and overhead, that leaves almost nothing, and the verdict card flags the overflow before you have burned an evening on it. The precision ladder does the useful work: at Q8_0 the same 32k window drops to about 2.44 GB and fits comfortably, while the max-context column shows that F16 would only manage roughly 17k tokens in that budget and Q4_0 would stretch to about 70k. The context ladder then prices the rest of the roadmap - 64k at Q8_0 is 4.9 GB, 128k is 9.8 GB - so the decision "do we need 128k?" comes with a number attached instead of a feeling.
A second example: DeepSeek-V3 and why MLA changes the game
DeepSeek-V3 has 61 layers but caches only its 576-element latent (512 rank + 64 rope) per token per layer - 70.3 KB per token at F16, versus the roughly 2 MB per token a GQA model of the same depth would need. A 64k context is about 4.4 GB instead of over 100 GB. If you are planning to run any MLA model, use the MLA attention type here: plugging its latent numbers into the standard GQA formula (using its 128 "KV heads" as if they were real) would overstate the cache by a factor of about 28 and send you shopping for a GPU you do not need.
Common mistakes when budgeting the KV cache
- Planning only for the weights. "The model fits, so it runs" is true at 4k context and false at 128k. The cache is a second, independent memory line item that scales with how you use the model, not with how big it is - budget it explicitly every time.
- Using attention heads instead of KV heads. Grouped-query models share KV heads across groups of query heads. Entering 32 instead of 8 (or 40 instead of 4) inflates the estimate fourfold and can push a perfectly fine plan into "impossible". The
num_key_value_headsfield is the one that matters. - Forgetting the batch multiplier. The per-token figure is per sequence. A server handling 8 parallel conversations holds 8 caches; the total is batch size times the single-sequence number, and it is the total that has to fit.
- Assuming quantized cache means exact halves. Q8_0 costs about 1.125 bytes per element once the per-block scales are counted, and Q4_0 about 0.625. Small per token, but at 1M tokens of context the difference between 0.5 and 0.625 bytes is a full gigabyte.
- Planning against 100% of VRAM. Drivers reserve memory before your process starts, and a desktop display holds a share on top. This calculator checks against 92% for exactly that reason; plans that fit at 100% fail at load time.
- Ignoring MLA models with the standard formula. DeepSeek-style latents are not "KV heads times head dimension". Using the GQA formula on their config values overstates the cache by an order of magnitude - switch the attention type and use the latent rank.
Privacy
This tool is entirely client-side. The cache math, the precision ladders and the command generation all run in JavaScript inside your own browser tab, so nothing you enter touches a server. There is no account, no tracking of your inputs, and no data retention - close the tab and everything is gone.
Related tools
Once the cache is sized, the rest of the local-LLM sizing workflow:
Suggested
Luke Keam | techgeek.biz