KV cache calculator: how many agents fit on your GPU?
Pick one of 29 open models, or paste any Hugging Face model id. Choose a weight quantization, a card and a context length. The calculator works out the KV cache each agent session needs and how many sessions fit in the memory that is left after the weights.
| Measure | Value |
|---|---|
| Agents that fit | - |
| KV cache per agent | - |
| Fixed: weights plus buffers | - |
| Used of total VRAM | - |
The KV cache formula
Every token an agent has in its context keeps a key and a value vector in every attention layer. That store is the KV cache, and it is why long-context agents run out of memory long before the GPU runs out of compute.
KV cache bytes = 2 × layers × KV heads × head dim × bytes per value × context tokens
The 2 counts keys and values. Layers, KV heads (num_key_value_heads)
and head dim (head_dim, or hidden_size divided by
num_attention_heads when it is missing) all come from the model's config.json.
Bytes per value is 2 for f16, about 1 for an 8-bit cache, and about half for a 4-bit cache.
Worked example: Qwen3-8B at 32k context
- Qwen3-8B's config.json lists 36 layers, 8 KV heads and a head dim of 128.
- Per token, with an 8-bit cache: 2 × 36 × 8 × 128 × 1 = 73,728 bytes (72 KiB).
- For 32,768 tokens: 73,728 × 32,768 = 2,415,919,104 bytes, which is 2.25 GiB per agent.
- With an f16 cache the same agent needs 4.5 GiB. On a 24 GB card the Q4_K_M weights (4.7 GiB) plus 2 GiB of buffers leave about 16.3 GiB, so 7 agents fit with an 8-bit cache and 3 with f16.
Your numbers
Three kinds of model need a tweak to the formula, and the calculator handles each one:
- Sliding-window layers (gpt-oss, Gemma 3) only remember the last few hundred or thousand tokens, so their cache stops growing once the context passes the window.
- Linear-attention layers in hybrid models such as Qwen3-Next keep a small fixed-size state instead of a cache, so only the full-attention layers grow with context.
- DeepSeek V3 style models use multi-head latent attention: each layer stores one compressed
vector of
kv_lora_rank + qk_rope_head_dimvalues per token instead of separate keys and values, which makes their cache unusually small for their size.
All models in the list
Numbers read from each model's config.json on Hugging Face. The last column is the cache for one agent
at 32,768 tokens with an 8-bit cache. Pick a model name to load it into the calculator.
| Model | Params | Full-attn layers | Other layers | KV heads | Head dim | Max context | KV per 32k agent |
|---|
Notice that parameter count is a poor guide to cache size. Qwen3-8B needs 2.25 GiB per 32k agent while the much larger Qwen3-30B-A3B needs 1.5 GiB, because it keeps half as many KV heads.
Things that catch people out
- Weights are the cheap part once cache dominates. With many long-context sessions on a large card, moving from Q4_K_M to Q8_0 weights costs a fixed amount of VRAM and only a little decode speed, because every step is bound by reading KV, not weights.
- Context beyond the trained window needs RoPE scaling. Qwen3's config sets
max_position_embeddingsto 40,960; running 65k or 131k sessions requires a scaling method such as YaRN enabled in your inference server. The calculator warns you when you go past it. - Gated models cannot be loaded by id. Llama and some Gemma repos only share files with signed-in users who accepted the license, so a browser cannot read their config.json. Use an ungated copy or the numbers from a model with the same architecture.
- Verify the arithmetic against a real run. Expect measured usage to land a GB or two above the prediction; the gap is compute buffers and allocator overhead.
Method
Architecture numbers (layers, KV heads, head dim, which layers use full, sliding or linear attention, and maximum
context) come from each model's public config.json on Hugging Face, and parameter counts come from the
Hugging Face model API (the safetensors total). The same code reads the bundled models and any id you paste.
Llama models are not in the list because their repos are gated. Gemma 3's config leaves out the layer pattern and
maximum context, so the calculator uses the Gemma 3 defaults from the Hugging Face transformers library
(one full-attention layer in every six, 131,072 tokens).
Weight sizes are real GGUF file sizes where we have them (Qwen3-8B, Qwen3-32B, Qwen3-30B-A3B from the official Qwen GGUF repos, and gpt-oss from ggml-org). For every other model they are estimated from the parameter count at the average bits per weight those GGUF files show: 4.82 for Q4_K_M, 5.67 for Q5_K_M, 6.56 for Q6_K, 8.5 for Q8_0 and 16 for BF16. The calculator adds 2 GiB of compute buffers per card, treats 96% of each card as usable, and counts an 8-bit cache as 1 byte per value. Linear-attention state is counted at 4 bytes per value.
Estimates, not guarantees: real usage depends on the inference server, batch settings and driver.