DEV Community

Cover image for KV cache size: the formula, worked out on real models, and how many users fit on one GPU
RONI DAS
RONI DAS

Posted on Originally published at systemdesign.academy

KV cache size: the formula, worked out on real models, and how many users fit on one GPU

Sooner or later, everyone who hosts a language model asks the same question: how many people can this one GPU serve at the same time? The model's size does not answer it. The answer sits in a much smaller thing that grows with every word of every conversation, the KV cache.

In this article I work out the KV cache sum the way an engineer needs it: from the model's config file, to the memory one conversation takes, to the number of conversations that fit on a card. Every measured number here comes from a lesson in our Hands-On Inference Engineering course, run on an RTX 4090 we rented on RunPod. I copied them from those runs, so you can check each one.

The short version. KV cache per token = 2 × layers × key-value heads × head size × bytes per number, all read from the model's config.json. For Qwen3-8B that is 2 × 36 × 8 × 128 × 2 = 147,456 bytes, 144 KiB a token, so a conversation of 32,768 tokens needs about 4.5 GiB. On a 24 GB RTX 4090, vLLM had 4.56 GiB left for the cache after the weights, room for 33,216 tokens: about 16 conversations of 2,000 tokens at once.

Part 1: The formula, worked out on real models

What is the KV cache, in plain words?

When a model writes a new token, the attention step in every layer looks back at all the earlier tokens. To do that it needs two things for each earlier token. The key lets the new token find an earlier one, and the value is what the earlier token hands over once it is found.

Working these out again for every new token would waste most of the GPU's time, so the server keeps them in GPU memory. That store of notes is the KV cache. It grows by one entry for every token of every conversation. That is why the cache, and not the model itself, usually decides how many people one GPU can serve at the same moment.

What is the KV cache size formula?

Bytes per token = 2 × layers × key-value heads × head size × bytes per number.

The 2 is there because every layer keeps both a key and a value. The other four numbers are in the model's config.json on Hugging Face:

  • num_hidden_layers, the number of layers
  • num_key_value_heads, the key-value heads
  • head_dim, the head size
  • the data type, which is 2 bytes for the usual 16-bit formats

Then multiply by the number of tokens you want to keep. For a whole server, that means every token of every conversation it holds at once.

Two columns, one per model, each showing the lines read from config.json and the sum worked from them. Qwen3-0.6B: 28 layers, 8 key-value heads, head size 128, 2 bytes, so 114,688 bytes, 112 KiB per token; one chat of 32,768 tokens needs 3,584.0 MiB of notes, against the model's 604 MiB of weights. Qwen3-8B: 36 layers, 8 key-value heads, head size 128, so 147,456 bytes, 144 KiB per token, and 4,608.0 MiB for 32,768 tokens, against 15.27 GiB of weights.
The KV cache per token, worked out from each model's config.json. For the small Qwen3-0.6B, the notes of one long chat outweigh the model itself.

How big is the KV cache for Qwen3-8B?

Qwen3-8B has 36 layers, 8 key-value heads and a head size of 128, stored in 2 bytes a number. So each token needs 2 × 36 × 8 × 128 × 2 = 147,456 bytes, which is 144 KiB. A conversation of 32,768 tokens therefore needs 4,608 MiB, about 4.5 GiB, on top of the model's weights.

What we measured: on the rented RTX 4090, vLLM 0.33 loaded Qwen3-8B's 16-bit weights in 15.27 GiB and set aside 4.56 GiB for the cache, which it reported as room for 33,216 tokens.

Can the cache be bigger than the model?

Yes, and for small models it often is. Qwen3-0.6B has 28 layers, 8 key-value heads and a head size of 128, so it keeps 112 KiB for every token. One conversation of 32,768 tokens then needs 3,584 MiB of notes. That is about six times the model's own 604 MiB of weights.

Does the sum match what a real server reserves?

It matched exactly in our run. That is worth checking once yourself, because it tells you the formula is not a rough guide. It is what the server really does.

What we measured: llama-server on our RTX 4090 reserved 448.00 MiB for 4,096 tokens of Qwen3-0.6B, which is exactly 4,096 × 112 KiB.

Part 2: From bytes per token to users per GPU

The question behind every KV cache sum is a capacity question: how many conversations fit on this card at once? It comes down to two divisions, and the server prints the first one for you.

How much GPU memory does each user need?

Multiply the bytes per token by the length of that user's conversation. Count the question, the earlier turns and the answer being written. For Qwen3-8B at 144 KiB a token, a conversation of 2,000 tokens needs about 281 MiB, and one of 32,768 tokens needs about 4.5 GiB.

So the honest answer to "how much memory per user?" is always another question: how long are their conversations? Ten short support chats can cost less than one person pasting in a long document.

Why is the cache only 4.56 GiB on a 24 GB card?

Because the cache gets whatever is left after everything else. The RTX 4090 reports 23.52 GiB to programs. vLLM takes 92% of it by default, which is 21.64 GiB. From that it pays for the weights, 15.27 GiB, and its own working space, about 1.8 GiB on a first start. The 4.56 GiB that remains becomes the KV cache.

That is also why a small change in the share changes the cache a lot. At 80%, the cache in the course's lesson on sizing GPU memory fell to 1.74 GiB, because the weights take the same room whatever the share.

How many users can one 24 GB GPU serve?

Divide the cache vLLM reports at start-up by the tokens one conversation holds. For Qwen3-8B in 16 bits on the RTX 4090, the cache held 33,216 tokens. That is enough for about one conversation of 32,768 tokens, 8 of 4,000, or 16 of 2,000, if they all reach full length at the same time.

vLLM will start more people than that when their questions are short, and pause some of them later if their answers grow and the cache runs out. The sum gives you the safe number, not a hard limit.

What we measured: in the course's crowd test, 64 conversations of about 340 tokens filled 66.8% of that cache. 160 filled it completely, and vLLM paused and resumed requests 64 times, but every answer still came back complete.

What does "Maximum concurrency" in vLLM's log mean?

It is the cache size divided by the longest conversation the server allows, which you set with --max-model-len. With a limit of 32,768 tokens and room for 33,216, our server printed 1.01x. It is a worst case: the number of full-length conversations that fit. It is not a limit on how many people vLLM will serve.

Why does vLLM count the cache in blocks of 16 tokens?

vLLM stores the cache in small pages of 16 tokens, taken from a shared pool only as a conversation grows. This is PagedAttention. It means a conversation wastes at most part of its last page, instead of reserving room for the longest conversation it might ever have. For Qwen3-8B one page is 16 × 144 KiB = 2.25 MiB, and our 4.56 GiB cache was 2,076 pages.

Part 3: What a bigger cache costs in speed

The cache is not only a memory question. Every new token has to read it, so it also sets how fast a long conversation can go.

Why do long conversations make the model answer more slowly?

To write each new token, attention reads the notes of every earlier token as well as the model's weights. A longer conversation means more notes to read for every single token, and writing is limited by how fast the GPU's memory can deliver bytes, not by how fast it can do arithmetic.

What we measured: on our RTX 4090, Qwen3-8B wrote 164.1 tokens a second with an empty cache and 114.9 tokens a second with 16,384 tokens already in it.

A chart of writing speed against the number of tokens already in the KV cache, from llama-bench runs on an RTX 4090. At 0, 2,048, 8,192 and 16,384 tokens, the small model's line falls from 639.4 to 277.8 tokens a second, and the 8B model's line falls from 164.1 to 114.9.
A fuller cache means slower writing. Every new token reads the weights once and every earlier token's notes once.

Does keeping the cache make the second turn of a chat faster?

Yes, because the earlier turns do not have to be read again. In the course's run, the second turn of a conversation reused the notes of 2,480 of its 2,524 prompt tokens. Reading its prompt took 6 ms, where the first turn had needed 94 ms.

Part 4: Making the cache smaller

There are two kinds of saving. One is chosen when the model is designed. The other is a setting you can change on your own server tonight.

How does grouped-query attention shrink the KV cache?

In grouped-query attention, several query heads share one key and one value. Only keys and values go in the cache, so it shrinks by the size of each group: num_attention_heads divided by num_key_value_heads in config.json. Qwen3-8B has 32 query heads in groups of 4, so it keeps 144 KiB per token instead of 576 KiB.

This is the one mistake I see most often in KV cache answers. Read num_key_value_heads, not num_attention_heads, when you do the sum. Using the wrong one makes the result four or eight times too big.

What we measured: the same 4.56 GiB that held 33,216 tokens of Qwen3-8B would hold only 8,304 if every head kept its own key and value.

Four panels, one for each way of keeping notes, each from a real model's config file. Multi-head attention, Qwen1.5-7B: 512 KiB per token. Grouped-query attention, Qwen3-8B: 32 query heads sharing 8 keys and values in groups of 4, 144 KiB. Multi-query attention, Falcon-7B: one key and value for all heads, 8 KiB. Latent attention, DeepSeek-V3: one compressed note of 512 numbers and a small position key of 64, 68.6 KiB.
Four ways a model can keep its notes, and what each costs per token. Sharing keys and values across heads is why a newer 8B model needs far less cache than an older 7B one.

Does an FP8 KV cache double the number of users?

Nearly. --kv-cache-dtype fp8 stores each number in 1 byte instead of 2, so Qwen3-8B needs 73,728 bytes a token instead of 147,456. The numbers are kept more coarsely, so check the quality of the answers on your own test questions before you keep it.

What we measured: on our RTX 4090 the cache held 59,856 tokens instead of 33,216. That is 1.80 times as many, not quite 2, because that start needed 0.45 GiB more working space.

Do smaller weights help the cache too?

Yes, directly. Whatever the weights stop using, the cache gets. The 8-bit copy of Qwen3-8B took 8.9 GiB instead of 15.27 GiB, and its cache held 79,184 tokens, 2.38 times as many as the 16-bit model on the same card.

Does a shorter max model length make the cache bigger?

No. --max-model-len only changes the check vLLM makes at start-up: that one conversation of that length fits. If the full length does not fit, vLLM refuses to start and names the longest length that would. Lowering it lets the server start, but the cache stays the same size.

What to take away

Work in tokens. Find the bytes one token costs from config.json. See how much memory is left after the weights and the working space, divide to get the tokens that fit, and divide again by the length of a typical conversation. Use num_key_value_heads, not the number of attention heads, and check your answer against the cache size the server prints when it starts. If your sum and the server's number agree, you understand your GPU's capacity.

The full page, with a link to the lesson each number comes from, is on our site: KV cache size: the formula, and how much GPU memory each user needs.

Go deeper: Hands-On Inference Engineering

I teach the runs behind this article in Hands-On Inference Engineering on System Design Academy. You rent an RTX 4090, work out the cache for Qwen3-8B on paper, start vLLM and check your sum against the budget it prints, then fill the cache with real users and watch what happens when it runs out. It suits backend, ML platform and AI Engineering people who plan to host their own models and want to explain every number on their dashboard: see the Hands-On Inference Engineering course.

Top comments (0)