An exercise to try
Estimate the KV cache
A model that loads can still run out of memory in a long chat. Four numbers from its configuration, with the length of the chat, tell you roughly how much memory its saved keys and values need.
- Time
- 20 minutes
- What you need
- This page and a calculator. No chatbot or code is needed.
Illustration. Model T in Part 1 is invented teaching material. The models in Part 2 and the different case are real, and their numbers are quoted from their published configuration files.
Words used here
- GPU. The graphics processor that runs the model. Its own memory has to hold the model's learned numbers, its weights, and this cache.
- Layer. One of the stages every token's numbers pass through in turn. A model stacks dozens of them.
- Head. One of several side-by-side ways an attention layer compares tokens. Query heads do the asking; each key-value head holds keys and values that one or more query heads use.
- Bits and bytes. A computer stores each number in a fixed count of bits, binary digits. Eight bits make one byte, so a 16-bit number takes 2 bytes and an 8-bit number takes 1.
The rule
For a model whose layers all use full attention, the cache for one token is 2 × layers × key-value heads × head size × bytes per number:
- 2. One key and one value for each token.
- Layers. Each layer keeps its own keys and values.
- Key-value heads. The number of separate key and value sets in each layer. With grouped-query attention it is smaller than the number of query heads.
- Head size. How many numbers each key or value holds.
- Bytes per number. 2 for 16-bit numbers, 1 for 8-bit numbers.
Multiply by the tokens in the chat, counting instructions, the conversation so far and the reply, to get the cache for the whole conversation. Use decimal units: a megabyte (MB) is a million bytes and a gigabyte (GB) a billion.
Part 1. Model T
Model T has 16 layers, 32 query heads, 4 key-value heads and a head size of 64, and it stores its cache in 16-bit numbers. A chat with it has reached 3,000 tokens.
Step 1. Work out bytes per token
Use the rule. Decide first which head count goes in, 32 or 4.
Step 2. Scale to the chat
Multiply by 3,000 tokens. Give the answer in bytes and in megabytes.
Part 2. A real configuration
The published configuration file of Qwen3-8B, an openly released model, lists these fields. They were checked on 19 November 2026 (Qwen3-8B config.json):
"num_hidden_layers": 36, the layers."num_attention_heads": 32, the query heads."num_key_value_heads": 8, the key-value heads."head_dim": 128, the head size."torch_dtype": "bfloat16", a 16-bit format, so 2 bytes per number."use_sliding_window": false, so every layer uses full attention.
Step 3. Estimate for the real model
Work out bytes per token. Then work out the cache for one chat of 30,000 tokens, and for four such chats running at once on the same GPU. Assume the cache is kept at 2 bytes per number.
Step 4. Name what the estimate leaves out
Someone measures the GPU memory used while Qwen3-8B handles one 30,000-token chat and gets a different number from yours. Write down one reason a measured number would differ from your estimate. To find one, look back at what the rule assumes (every layer uses full attention, 2 bytes per number, nothing shared between chats) and at what else the GPU's memory holds.
Check your work
- Step 1. 2 × 16 × 4 × 64 × 2 = 16,384 bytes per token. The head count is 4, the key-value heads. Using 32, the common wrong answer, gives 131,072 bytes, eight times too much: query heads are computed fresh for each new token and are not stored.
- Step 2. 16,384 × 3,000 = 49,152,000 bytes, about 49 MB.
- Step 3. 2 × 36 × 8 × 128 × 2 = 147,456 bytes per token. For 30,000 tokens that is 4,423,680,000 bytes, about 4.4 GB. Four chats at once need four caches: 17,694,720,000 bytes, about 17.7 GB, before the weights are counted.
- Step 4. Any of these is a good reason. The weights, the working space and the serving software use memory too. Some software reserves cache memory ahead of time, for the longest chat it allows (Hugging Face cache guide). The cache may be stored in 8 bits, which halves it. Serving software can share cached keys and values between requests that start the same way (PagedAttention paper). The chat may hold more or fewer tokens than you assumed.
A common wrong answer to Step 4 is “the rule forgot the queries”. It did not: queries are not cached, which is why the rule uses key-value heads.
Try a different case
gpt-oss-20b is the model the attention long read uses. Its configuration lists 24 layers, 8 key-value heads and a head size of 64. Its "layer_types" alternate between "sliding_attention" and "full_attention", with "sliding_window": 128 (gpt-oss-20b config.json). A sliding layer's cache stops growing once it holds its last 128 tokens. Estimate the cache for a 30,000-token chat at 2 bytes per number, treating the 12 full layers and the 12 sliding layers separately.
Check this one too
In one layer, one token saves 2 × 8 × 64 = 1,024 numbers, the count the attention long read gives, and at 2 bytes each that is 2,048 bytes. The 12 full layers keep every token: 12 × 2,048 × 30,000 = 737,280,000 bytes. The 12 sliding layers keep at most 128 tokens each: 12 × 2,048 × 128 = 3,145,728 bytes, about 3.1 MB, however long the chat. Together that is 740,425,728 bytes, about 0.74 GB. Treating all 24 layers as full would give 1,474,560,000 bytes, about 1.5 GB, double the estimate. Read the layer types before you multiply.
Where this sits: Why a longer chat can need more memory in the GPU guide, and reading a model config in the attention long read.