The KV cache grows in a straight line with context length. Each token adds the same number of bytes, equal to 2 x layers x key-value heads x head dimension x bytes per element. For Llama 3.1 8B at 16-bit that is 128 KiB per token, so 8,192 tokens take 1.00 GiB and 131,072 tokens take 16.00 GiB, on top of the model weights. Double the context and the cache doubles; the weights do not change.
The layer counts, key-value head counts and head dimensions below are the ones recorded in the register behind our local AI hardware fit tool. For Qwen3-14B, Dory 1.0 4B and gpt-oss-20b we checked them against each model’s published configuration file on 23 November 2026, and they match. Llama 3.1 8B’s file needs a signed-in account and MiMo’s file does not list these fields, so for those two treat the numbers as the register’s record and not as a fresh check. Every figure comes from one script, compute.py, and the last section says what each source supplied and when we read it.
Why a longer context needs more memory
In each layer of a transformer model, every token produces a query, a key and a value, and attention compares the newest token’s query with the keys of the tokens before it. Wikipedia’s article on attention in machine learning describes attention as a method that weighs each component of a sequence against the others, and writes the calculation with a matrix of queries and matrices of keys and values that together hold n key-value pairs. A runtime that keeps each token’s keys and values does not have to rebuild them for every later token. That stored set is the KV cache, and it gains one entry per token in every layer. Its size is proportional to the context length.
The formula in words and symbols
Per token: 2 tensors (keys and values) x layers x key-value heads x head dimension x bytes per element. Total: the per-token figure x context length in tokens. Divide bytes by 1,073,741,824 to get GiB.
- 2 is one key tensor and one value tensor.
- Layers is the number of layers that keep a cache for the whole context. For most plain models that is every layer; the register counts fewer for some models, as the table notes say.
- Key-value heads is the number of heads that have their own keys and values, which can be smaller than the number of attention heads.
- Head dimension is the length of the key (or value) vector of one head.
- Bytes per element is 2 for a 16-bit cache, 1 for 8-bit and 0.5 for a nominal 4-bit cache.
Worked example: Llama 3.1 8B at 16-bit
The register records 32 layers, 8 key-value heads and a head dimension of 128 for Llama 3.1 8B Instruct. For Llama 3.1 8B at 16-bit, the cache needs 2 x 32 x 8 x 128 x 2 = 131,072 bytes per token, which is 128 KiB. At 8,192 tokens that is 1,073,741,824 bytes, or 1.00 GiB; at 131,072 tokens it is 17,179,869,184 bytes, or 16.00 GiB.
The same arithmetic runs backwards. If you can spare 4 GiB for the cache, 4 x 1,073,741,824 / 131,072 = 32,768 tokens fit. With 2 GiB it is 16,384 tokens and with 8 GiB it is 65,536.
Key-value heads and element size change the answer
Grouped-query attention
In plain multi-head attention each head has its own keys and values. A model built with grouped-query attention lets several query heads share one key-value head, so it lists fewer key-value heads than attention heads. Only key-value heads enter the formula. The register records 8 for both Llama 3.1 8B and Qwen3-14B. As an illustration, not a real configuration, the same Llama layout with 32 key-value heads would need 2 x 32 x 32 x 128 x 2 = 524,288 bytes per token, which is 512 KiB and four times as much: 64.00 GiB at 131,072 tokens instead of 16.00 GiB.
16-bit, 8-bit and smaller elements
Bytes per element scales the total directly. These are arithmetic options for the same Llama 3.1 8B layout, not statements about what any runtime does by default. Which cache precisions a runtime offers, and what it uses unless you change it, differs between runtimes. A format that stores extra values beside the elements, such as scaling factors, would take a little more than the bare element size shown here.
| Element size | Bytes per element | Per token | 8,192 tokens | 131,072 tokens |
|---|---|---|---|---|
| 16-bit | 2 | 128 KiB | 1.00 GiB | 16.00 GiB |
| 8-bit | 1 | 64 KiB | 0.50 GiB | 8.00 GiB |
| 4-bit, bare element size | 0.5 | 32 KiB | 0.25 GiB | 4.00 GiB |
KV cache size for five models at four context lengths
All figures are at 16-bit, in GiB, rounded to two decimals with halves rounded up. The second column gives the three architecture numbers from the register: cache layers, key-value heads and head dimension.
| Model | Layers x KV heads x head dimension | Per token | 4,096 tokens | 8,192 tokens | 32,768 tokens | 131,072 tokens |
|---|---|---|---|---|---|---|
| Llama 3.1 8B Instruct | 32 x 8 x 128 | 128 KiB | 0.50 | 1.00 | 4.00 | 16.00 |
| Qwen3-14B | 40 x 8 x 128 | 160 KiB | 0.63 | 1.25 | 5.00 | 20.00 (above its 32,768 maximum) |
| MiMo-V2.6-Distill-Qwen-9B | 8 x 4 x 256 (full-attention layers only) | 32 KiB | 0.13 | 0.25 | 1.00 | 4.00 |
| NVIDIA Nemotron Labs Dory 1.0 4B | 9 x 4 x 256 (full-attention layers only) | 36 KiB | 0.14 | 0.28 | 1.13 | 4.50 |
| gpt-oss-20b | 12 x 8 x 64 (full-attention layers only) | 24 KiB | 0.09 | 0.19 | 0.75 | 3.00 |
Three things stand out. First, the two plain models cost 128 and 160 KiB per token, while MiMo-V2.6-Distill-Qwen-9B costs 32 KiB, a quarter of Llama’s, because the register counts only 8 layers with a full-length cache, 4 key-value heads and a larger head dimension. Second, parameter count does not set the cache: gpt-oss-20b, which the register lists at 21 billion parameters, needs less than the 8.03 billion parameter Llama at every length. Third, the cache can outgrow the weights. The register’s 8.03 billion parameters at 2 bytes each come to about 14.96 GiB, and at 131,072 tokens the Llama cache alone is 16.00 GiB.
The register records 131,072 tokens as the model card’s maximum for Llama 3.1 8B and gpt-oss-20b, 262,144 for MiMo and Dory, and 32,768 for Qwen3-14B. Its note on Qwen3-14B adds that the model card lists 131,072 with YaRN, so that cell is arithmetic and not a supported setting. For MiMo, Dory and gpt-oss the register counts only the full-attention layers; the next sections cover what that leaves out.
How these figures differ from the hardware fit tool
The hardware fit tool opens at an 8,192-token context and lets you pick contexts from 4,096 tokens up to 1,048,576. It prices the cache at 16-bit (BF16), with no cache quantization, as context divided by 1,024 times a stored figure of KV GiB per 1,000 tokens. The register keeps that figure to two decimals, so the tool can sit up to 0.005 GiB per 1,024 tokens away from exact arithmetic, which is up to 0.64 GiB at 131,072 tokens. The tool then adds the model’s weights and a per-model runtime reserve, which is 1.5 GiB for each of the five models here, and compares the total with about 90% of the card’s memory. The table below puts the tool’s cache figure beside the exact one.
| Model | Register figure per 1,024 tokens | Tool at 8,192 | Exact at 8,192 | Tool at 131,072 | Exact at 131,072 |
|---|---|---|---|---|---|
| Llama 3.1 8B Instruct | 0.13 GiB | 1.04 | 1.00 | 16.64 | 16.00 |
| Qwen3-14B | 0.16 GiB | 1.28 | 1.25 | 20.48 | 20.00 |
| MiMo-V2.6-Distill-Qwen-9B | 0.03 GiB | 0.24 | 0.25 | 3.84 | 4.00 |
| NVIDIA Nemotron Labs Dory 1.0 4B | 0.04 GiB | 0.32 | 0.28 | 5.12 | 4.50 |
| gpt-oss-20b | 0.02 GiB | 0.16 | 0.19 | 2.56 | 3.00 |
The two methods agree on what is counted and differ only in rounding. The tool rounds Llama 3.1 8B up from 0.125 to 0.13 and MiMo down from 0.03125 to 0.03. Use the tool when you want a fit decision with weights and reserve included, and use this arithmetic when you want to see how the cache moves with context.
What the estimate leaves out
- Weights and working memory. The cache is one part of what a model needs. The weights and the runtime’s own buffers are separate, and the register’s reserve is a planning estimate, not a maker’s requirement.
- Layers that do not keep a full-length cache. For MiMo, Dory and gpt-oss the table counts only the layers the register lists as full attention. The register says gpt-oss-20b has 12 further layers with a 128-token sliding window; the same formula for those gives 2 x 12 x 8 x 64 x 2 x 128 = 3,145,728 bytes, or 3 MiB, if only the window is kept. Whether a runtime keeps only the window differs between runtimes. The register notes also name recurrent or convolution state for some hybrid models, and this guide does not size it.
- Layouts that break the 2-tensor rule. The register note for Gemma 4 31B IT says its global layers use one shared key-value tensor, and that 50 of its layers use a 1,024-token sliding window worth about 0.8 GiB at 16-bit. That model needs a different formula from the one above, so it is not in the table.
- More than one conversation. A server that holds several conversations at once needs a cache for each. Two full 8,192-token conversations on Llama 3.1 8B take 2 x 1.00 = 2.00 GiB at 16-bit. How a runtime sets this up differs between runtimes.
- When the memory is claimed. A runtime may reserve the cache for the full context when the model loads or grow it as the conversation grows. Check which yours does; runtimes differ.
- Speed. This guide sizes memory only. We state no speed or throughput figure.
This guide covers language models. Image, video and speech models have their own memory questions, and our image, video and speech model VRAM table compares makers’ stated figures with file sizes for those.
Estimating the cache for another model
- Find the layer count, the number of key-value heads and the head dimension. Model makers publish them in the model’s configuration file or card; the Hugging Face API record we read names the architecture class but did not carry these numbers.
- Check whether every layer keeps a full-length cache. If some use a sliding window or recurrent state, count only the full-length layers and treat the rest as a separate, smaller item.
- Multiply: 2 x layers x key-value heads x head dimension x bytes per element gives bytes per token.
- Multiply by the context length you plan to use, then divide by 1,073,741,824 for GiB.
- Add the weights and a reserve for the runtime, and compare the sum with your card’s memory. The hardware fit tool does that step for the models in its register, and our GPU power supply table covers the next question, which power supply a card needs.
Sources and dates read
- NeonControl hardware register (version 2026-11-11, read 22 November 2026): the layer counts, key-value heads, head dimensions, maximum contexts, per-model reserves and the tool’s stated method. Each model’s numbers come from the notes the register keeps for it.
- Hugging Face Hub API, read 22 November 2026: records for Llama 3.1 8B Instruct, Qwen3-14B, gpt-oss-20b, MiMo-V2.6-Distill-Qwen-9B and Dory 1.0 4B. They supplied the architecture class names and model types, not layer counts, key-value heads or head dimensions.
- Model configuration files on Hugging Face, read 23 November 2026: Qwen3-14B (40 layers, 8 key-value heads, head dimension 128), Dory 1.0 4B (9 full-attention layers of 72, 4 key-value heads, head dimension 256, a 1,024-token window on 27 sliding layers) and gpt-oss-20b (12 full-attention layers of 24, 8 key-value heads, head dimension 64, a 128-token window on the other 12). They confirm the register’s numbers for those three models.
- Wikipedia, Attention (machine learning), read 22 November 2026 through the Action API: the general definition of attention and its query, key and value matrices.