Estimator
Context memory estimator
How much graphics memory does a long chat's context take? This page estimates the key and value (KV) cache from five numbers you supply. It is an estimate, not a measurement, and it covers the cache only, not the model's weights.
Memory check
Your model's numbers
Open the model's config.json and copy the layers, KV heads and head dimension below, then set the context length you plan to use and the bytes per value of your cache type. The boxes start with an example (Qwen3-32B at 32,768 tokens); replace them with yours.
KV cache estimate
Example: Qwen3-32B at 32,768 tokens with a 16-bit cache.
- Cache (binary units)
- 8.00 GiB
- Cache (decimal units)
- 8.59 GB
- Per token
- 256.00 KiB
Worked out: 2 × 64 × 8 × 128 × 32,768 × 2 = 8,589,934,592 bytes
| Context (tokens) | Cache | Relative size |
|---|---|---|
| 4,096 | 1.00 GiB | |
| 8,192 | 2.00 GiB | |
| 16,384 | 4.00 GiB | |
| 32,768 (yours) | 8.00 GiB | |
| 65,536 | 16.00 GiB | |
| 131,072 | 32.00 GiB |
The formula
KV cache bytes = 2 × layers × KV heads × head dimension × context tokens × bytes per value
The formula applies to one sequence in which every counted full-attention layer stores the same key and value shape at the same bytes per value. Each such layer keeps one key array and one value array for the tokens it has seen, which is where the 2 comes from. Each array holds one entry per KV head times the head dimension for each token (see the Transformers cache explanation). Models that share key and value heads between several query heads (grouped-query attention) list fewer KV heads than attention heads, so they need less cache.
What the estimate assumes
- Every counted layer attends to the full context. Models with other attention designs, such as sliding-window layers or a compressed cache, store a different amount; check the model's documentation.
- One chat at a time. A backend that serves several sequences at once keeps a cache for each.
- The cache only. Model weights and the backend's working buffers come on top.
- No rounding. Backends can round the allocation up. ExLlamaV3 v1.6.7 requires cache capacity in multiples of 256 tokens (see its cache.py and constants.py), and our EXL3 and KV cache guide discusses it.
- Plain bytes per value. Whether a quantized cache stores extra metadata, such as scale values, depends on the backend. ExLlamaV3 v1.6.7 allocates scale buffers for its quantized cache (see its quant.py), so that backend's real size is a little higher than this estimate. Check your backend's documentation.
- Context lengths in the table. The 4,096 to 131,072 token rows are arithmetic scenarios for the same settings. Whether a model or backend supports a given length depends on the model and the backend.
- Units. GB means 109 bytes and GiB means 230 bytes; KiB is 1,024 bytes.
Finding the numbers in config.json
num_hidden_layers: layers.num_key_value_heads: KV heads. If the file has no such entry, the default depends on the model architecture, so check the model's documentation or code for what a missing field means (for example the Transformers Qwen3 page lists that model's defaults).head_dim: head dimension. If the file has no such entry, it is commonlyhidden_size÷num_attention_heads; check the model's documentation to be sure.- Context length is your own setting, such as the context size you choose in your backend.
Worked example
Qwen3-32B's configuration lists 64 layers, 8 KV heads and a head dimension of 128, and its model card gives 32,768 tokens as its native context length. At 32,768 tokens with a 16-bit cache: 2 × 64 × 8 × 128 × 32,768 × 2 = 8,589,934,592 bytes, which is 8.00 GiB (8.59 GB). That matches the FP16 figure in our EXL3 bits per weight and KV cache chart.
Sources
- Attention Is All You Need, arXiv (1706.03762): the attention layer's keys, values and heads.
- GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints, arXiv (2305.13245): fewer key and value heads than query heads.
- Qwen3-32B model configuration (config.json), Qwen on Hugging Face: the example values.
- ExLlamaV3 v1.6.7 FP16 KV cache (fp16.py), ExLlamaV3 on GitHub: one backend's 16-bit cache layout.
- Qwen/Qwen3-32B model card, Qwen on Hugging Face: the native context length of the example.
- Caching: cache explanation, Hugging Face Transformers documentation: the shape of the key and value cache.
- Qwen3 model documentation, Hugging Face Transformers documentation: the model-specific configuration defaults.
- ExLlamaV3 v1.6.7 cache.py, ExLlamaV3 on GitHub: the cache capacity requirement.
- ExLlamaV3 v1.6.7 constants.py, ExLlamaV3 on GitHub: the 256-token page size.
- ExLlamaV3 v1.6.7 quant.py, ExLlamaV3 on GitHub: the quantized cache's scale buffers.
- Prefixes for binary multiples, NIST: GiB and KiB.
Nothing you type is stored or sent anywhere; the arithmetic runs in this page.