What the KV cache is and why it fills GPU memory
The KV cache fills GPU memory because it grows with every token, while the model's weights stay the same size. You have picked an open model and a GPU. How many requests can that GPU serve at once before it runs out of memory? The question comes up as soon as a second user arrives, or a prompt grows past a few thousand tokens.
A language model reads text in attention layers, the parts of the model that look back at earlier tokens. For each token, every layer stores a key (what later tokens match against) and a value (what the token passes on). Keeping them means the model never recomputes them for each new token, as Carol Chen's transformer inference arithmetic explains.
Reading the prompt and building its cache is called prefill. Writing the answer one token at a time is called decode. NVIDIA notes that every request in a batch needs its own cache allocation. A batch is the set of requests the GPU works on together.
How to calculate KV cache memory: the formula
KV cache memory per token is the product of five numbers, all found in the model's config file. NVIDIA's inference guide gives the same formula and points to model cards and config files for the inputs.
KV bytes per token = 2 x cached layers x KV heads x head dimension x bytes per number
KV bytes total = KV bytes per token x tokens per request x requests at onceThe 2 stands for one key and one value. An attention layer splits its work into heads, parallel lookups that each handle a slice of the numbers. KV heads are the heads that store keys and values. The head dimension is how many numbers each head stores per token. Bytes per number is 2 for 16-bit formats (BF16 or FP16) and 1 for 8-bit formats such as FP8.
The number of KV heads is where models differ most. In multi-head attention (MHA), every head stores its own keys and values. Shazeer's multi-query attention (MQA) shares one key-value head across all heads, which greatly reduces the cache. Ainslie et al. introduced grouped-query attention (GQA), where groups of heads share a key-value head. They report quality close to MHA at a speed comparable to MQA.
Every head reads the cache with a query, so a model can have 24 query heads but only 4 KV heads. The table applies all three designs to our worked example below: 16 cached layers, 24 query heads, head dimension 256, BF16.
| Attention design | KV heads | Cache per token | Cache per 32,768-token request |
|---|---|---|---|
| MHA | 24 | 384 KiB | 12 GiB |
| GQA (Qwen3.8 27B) | 4 | 64 KiB | 2 GiB |
| MQA | 1 | 16 KiB | 0.5 GiB |
Hybrid models: count only the layers that keep a KV cache
In a hybrid model, only some layers keep a KV cache, so the formula counts only those. Qwen3.8 27B, which we serve, is one. Its config lists 64 layers, of which 16 use full attention. Full attention looks back at every earlier token, so it stores a key and a value for each one.
The other 48 layers use Gated DeltaNet, a form of linear attention: instead of storing every token, each layer updates a running summary. In the Gated DeltaNet paper, each head's summary is one matrix that folds every key and value into itself, which cuts memory during inference. The matrix keeps the same size however long the request grows. Because keys and values share it, its size has no factor of 2. Qwen's model card gives the layout: three DeltaNet layers, then one full-attention layer, sixteen times over.
Worked example: KV cache for Qwen3.8 27B on one 80 GB GPU
On one 80 GB GPU with default settings of vLLM, an open-source serving engine, Qwen3.8 27B in BF16 fits 8 requests of 32,768 tokens. The inputs come from the model's config, and the arithmetic is ours.
KV bytes per token = 2 x 16 layers x 4 KV heads x 256 x 2 bytes = 65,536 bytes (64 KiB)
DeltaNet per request = 48 layers x 48 heads x 128 x 128 x 2 bytes = 75.5 MB (0.07 GiB)Counting all 64 layers in the first line would give 256 KiB per token, four times too much. In the second line, the config gives 48 value heads per DeltaNet layer. Keys and values have 128 numbers each, so each head's matrix is 128 x 128. That line assumes the engine keeps the summaries at 16 bits; at 32 bits they double. Weights and GPU memory are in GB (10^9 bytes), the cache in GiB (2^30 bytes, about 7% more).
| Step | Calculation | Memory |
|---|---|---|
| Cache per 32,768-token request | 64 KiB x 32,768 | 2 GiB |
| Memory per request, with summaries | 2 GiB + 0.07 GiB | 2.07 GiB |
| Weights in BF16 | 27 billion x 2 bytes | about 54 GB |
| vLLM memory budget | 0.92 x 80 GB | 73.6 GB |
| Left for the cache | 73.6 GB − 54 GB | 19.6 GB (18.25 GiB) |
| Requests at 32,768 tokens | 18.25 GiB ÷ 2.07 GiB = 8.8, rounded down | 8 |
| Memory per request, 8-bit cache | 1 GiB + 0.07 GiB | 1.07 GiB |
| Same with an 8-bit cache | 18.25 GiB ÷ 1.07 GiB = 17.1, rounded down | 17 |
vLLM uses 92% of GPU memory by default and sizes the cache from that budget. An FP8 cache stores each number in 1 byte instead of 2. The vLLM docs say it reduces the cache's memory and lets you store more tokens.
Now say you expect 16 users at once, each with up to 32,768 tokens. In BF16 they need 33.1 GiB, which does not fit. In FP8 they need 16 x 1.07 = 17.1 GiB, leaving 1.1 GiB for activations and engine overhead. Capping each request at 24,576 tokens cuts its FP8 cache to 0.75 GiB, or 0.82 GiB with its summary. Sixteen such requests need 13.1 GiB and leave 5.1 GiB. We recommend that FP8 cache and 24,576-token cap first, and a second GPU only if answers lose quality at 8 bits.
Two assumptions make the result too optimistic. It ignores activations, the temporary numbers each layer computes, and the engine's own overhead, which both need memory. Two make it too cautious. It keeps the weights in BF16, though 8-bit weights would free about 27 GB. It also assumes every request fills its cap, and most do not.
How vLLM fits the KV cache into GPU memory
vLLM fits the cache into memory with three mechanisms: a fixed budget, small blocks and preemption. The budget is the `--gpu-memory-utilization` share described above. You can also set the cache size directly with `--kv-cache-memory-bytes`.
PagedAttention stores the cache in small blocks instead of one buffer reserved for the longest possible request. The vLLM team found that earlier systems wasted 60% to 80% of this memory, and PagedAttention cut waste to under 4%. Their paper reports 2 to 4 times as many requests served per second at the same latency.
Preemption is what happens when the cache still runs out. The engine pauses some requests and recomputes them later, which adds latency. The docs suggest a higher memory share, fewer requests at once, or tensor parallelism. Tensor parallelism splits the weights across GPUs and leaves each one more room for the cache. Capping context and concurrency also cuts memory.
vllm serve Qwen/Qwen3.8-27B \
--max-model-len 24576 \
--max-num-seqs 16 \
--kv-cache-dtype fp8 \
--gpu-memory-utilization 0.92Plain attention on a long prompt needs temporary memory that grows with the square of its length, and FlashAttention was designed to cut that cost. Our guide to fixing vLLM out-of-memory errors covers the error messages.
What KV cache memory means when you call an API
On a serverless API, KV cache memory decides how long your repeated prompt start stays cheap. A prefix is the opening part of a prompt that later requests repeat, such as a system prompt. Prefix caching keeps its cache so later requests skip rebuilding it. On our serverless inference, recent prompt prefixes stay in GPU memory until newer requests replace them. A repeated prompt start still in memory is billed at the model's cached input price, shown on our models page.
Cache memory is shared, so how long a prefix lasts depends on load. In our own test calls, published on 14 October 2026, quick follow-ups almost always found the cache. After a pause of one to three minutes, it was hit or miss.
Lyceum is a sovereign European AI inference provider for open-source models. Our GPUs run in European data centres. We don't keep your prompts or outputs, and we never use them for training. If you would rather not size the cache yourself, Qwen3.8 27B runs on our serverless inference. On 21 October 2026, our models page listed it at $0.40 input, $0.10 cached input and $2.40 output per 1M tokens. To serve it on your own replicas, see deploying a Hugging Face model as an inference API.
Summary: how we help you size the KV cache
The KV cache grows with every token and every request at once, so it decides how many users one GPU serves. Count only the layers that keep a cache, add the weights, and try an 8-bit cache and a context cap before adding a GPU.
Here is how we help you with it:
- Sizing before you rent: our model sizing tool reads a Hugging Face model's config and shows its memory and how many requests it serves.
- No cache to size: Qwen3.8 27B runs on our serverless inference, billed per token.
- Cheaper repeated prompts: a prompt start still in our cache is billed at the model's cached input price.
- Your own replicas: we deploy any Hugging Face model ID on dedicated endpoints behind an OpenAI-compatible API.
- European by default: our GPUs run in European data centres, and prompts and outputs are not stored or used for training.
Size your own model before you rent a GPU. Our model sizing tool reads a Hugging Face model's config file and shows the memory it needs and how many requests it serves.
Sources
- https://kipp.ly/transformer-inference-arithmetic/
- https://developer.nvidia.com/blog/mastering-llm-techniques-inference-optimization/
- arxiv.org
- https://arxiv.org/abs/2305.13245
- huggingface.co
- arxiv.org
- huggingface.co
- vLLM engine arguments
- https://docs.vllm.ai/en/latest/features/quantization/quantized_kvcache/
- https://vllm.ai/blog/2023-06-20-vllm
- https://arxiv.org/abs/2309.06180
- vLLM optimization and tuning: preemption
- vLLM conserving memory
- https://arxiv.org/abs/2205.14135