If you remember one thing from this track, make it this: in LLM serving, GPU memory after the weights is KV cache, and KV cache is concurrency. This lesson explains what the cache stores, gives you the formula to size it, works through how many requests a GPU can hold at different context lengths, and covers the techniques engines and model designers use to shrink it: grouped-query attention, paging, prefix sharing, quantization and offloading.
- Explain what the KV cache stores and why decoding needs it
- Compute KV-cache bytes per token from a model's architecture
- Estimate how many concurrent requests fit at a given context length
- Explain how GQA, paging, prefix caching, FP8 cache and offload reduce memory pressure
- Recognise the symptoms of a full cache in a running service
What the cache holds
In every attention layer, each token produces a key and a value vector. To generate the next token, the model compares the new token's query with the keys of all previous tokens and mixes their values. Recomputing those keys and values for the whole history at every step would make generation quadratically slower, so the server stores them: that store is the KV cache. Prefill writes it for the prompt; each decode step reads all of it and appends one more token's worth.
The cache is per request and lives as long as the request. It grows linearly with the number of tokens so far, and it is read in full on every decode step, so a long context costs both memory and bandwidth.
Sizing the cache
KV bytes per token = 2 x layers x kv_heads x head_dim x bytes_per_value
32 layers, 8 KV heads, head_dim 128, FP16 (2 bytes):
2 x 32 x 8 x 128 x 2 = 131,072 bytes = 128 KiB per token
Same model with 32 KV heads (no GQA): 512 KiB per tokenNow fit it on a GPU. Take an 80 GB card, let the engine use 90% of it (72 GB), and subtract 16 GB of FP16 weights: about 56 GB is left for cache. At 128 KiB per token that is roughly 427,000 tokens of cache in total, shared by every active request.
| Average tokens per request | Concurrent requests that fit | With FP8 KV cache |
|---|---|---|
| 4,000 | about 106 | about 213 |
| 32,000 | about 13 | about 26 |
| 128,000 | about 3 | about 6 |
This is why long-context features are expensive: the same GPU serving 128k-token conversations holds a few dozen times fewer users than one serving 4k-token chats. When the cache fills, the engine must queue new requests or preempt running ones, evicting their cache and recomputing it later, which shows up as latency spikes.
Making the cache smaller
- Grouped-query attention (GQA) and multi-query attention let many query heads share fewer key-value heads. Most modern models use it; it is why
kv_headsis often far smaller than the number of attention heads. It is a property of the model, not a setting. - Latent and sliding-window attention: some architectures compress keys and values into a smaller latent vector, or only keep a recent window of tokens in some layers, shrinking the cache further.
- Paged allocation (vLLM's PagedAttention and equivalents) stores the cache in small blocks allocated on demand, so memory is not reserved for the maximum length up front.
- Prefix caching shares the cache blocks for identical prompt prefixes, such as a long system prompt, across requests.
- KV-cache quantization stores keys and values in FP8, halving memory with usually small quality impact; validate on your evals.
- Offloading moves inactive blocks to CPU memory or SSD and brings them back instead of recomputing, useful for long idle conversations.
The cheapest win is often a lower maximum context length in the engine. If 99% of your requests are under 8k tokens, configuring a 128k maximum mostly wastes headroom; route the rare long requests to a separate deployment.
Spotting a full cache in production
- The engine's cache-usage gauge (for vLLM,
vllm:kv_cache_usage_perc) sits near 1.0. - Requests waiting rises while requests running stays flat at a ceiling.
- A preemption counter climbs and tail latency jumps.
- Time to first token degrades first, because new requests cannot be admitted.
The fixes come in two groups: more cache (another GPU or replica, tensor parallelism to spread weights, FP8 cache, higher memory-utilisation setting) or less demand per request (lower max context, shorter prompts, prefix caching, smaller max_tokens).