BasicsOpen
What is a KV cache, and why long chats fill GPU memory
A model's weights can fit on a GPU while its conversations do not. The KV cache explains why context length, simultaneous users and memory capacity belong in the same calculation.
A KV cache, short for key-value cache, stores attention calculations from tokens a language model has already processed. When the model generates the next token, it reuses these stored keys and values instead of calculating them again. That saves computation, but the stored state takes memory.1

What the cache remembers
In attention, a new token's query is compared with earlier keys; the resulting weights determine how earlier values contribute to its representation. For a causal Transformer, later tokens cannot change those earlier keys and values, so they can be saved. Each attention layer keeps its own cache.1
This differs from the model's parameters. Weights are learned numbers used across requests. The cache is state produced for a particular sequence during inference. Extending a conversation adds cached states without training new weights.12
How much memory does a KV cache need?
For a conventional full-attention model storing separate keys and values, raw cache size in bytes is:
2 × layers × KV heads × head dimension × cached tokens × bytes per value × independent sequences.
The two counts keys and values. This tensor-storage calculation follows the documented cache dimensions; it excludes allocation overhead, workspaces and other inference memory.1
Take Qwen2.5-7B-Instruct as a reproducible example. Its official configuration specifies 28 layers, 4 KV heads, 28 query heads and a hidden size of 3,584. The head dimension is therefore 3,584 ÷ 28 = 128. Sliding-window attention is disabled.3
At 16 bits, or two bytes per value, each cached token needs 2 × 28 × 4 × 128 × 2 = 57,344 bytes, exactly 56 KiB. One sequence at 32,768 tokens needs 1.75 GiB; four independent sequences need 7 GiB. These are PulpBrief calculations, not GPU measurements or total-memory requirements.3
- One sequence
- Four sequences
The chart counts processed tokens, including generated tokens retained in the cache. The last point uses the configuration's 32,768-position limit: filling it entirely with a prompt leaves no room for an answer within that limit.3
Why parameter count is not enough
The formula uses KV heads, which need not equal query heads. Grouped-query attention, or GQA, lets several query heads share keys and values. It sits between ordinary multi-head attention and multi-query attention, which uses just one KV head.4
In the Qwen example, substituting 28 query heads for the actual 4 KV heads would overstate cache size sevenfold. Two similarly sized models can therefore have different cache requirements: check the architecture before converting a context window into a memory budget.34
The linear formula also needs qualification. Sliding-window layers stop retaining additional states after their window fills. Some runtimes preallocate a static cache, so reserved memory can be large before the conversation grows.5
Four ways to ease the pressure
| Approach | What it changes | What to check |
|---|---|---|
| Shorter retained context | Fewer cached token states | Whether the application still has the information it needs1 |
| Paged allocation | Packs cache into blocks allocated as needed | Reduces allocation waste; does not shrink each stored value2 |
| Cache quantization | Stores cache values in fewer bits | Supported models, quality and latency5 |
| CPU offloading | Moves much of the cache out of GPU memory | Transfer overhead can reduce throughput5 |
Choosing 4-bit model weights does not by itself select a 4-bit cache. Cache precision is a separate setting. Hugging Face also warns that cache quantization can worsen latency on short contexts when ordinary caching already fits.5
Does prefix caching make long chats free?
Prefix caching reuses cached computation when a request begins with an already processed prefix. Repeated questions about the same document can reuse its initial processing. vLLM distinguishes that prefill saving from decoding: prefix reuse does not remove the work of generating the new answer.6
Requests can also share stored prefix blocks, so the chart's independent-sequence multiplication is not a universal server-memory forecast.2 The practical questions are how much context is retained, how much is shared and how many requests are active together.
For a local setup, use the LLM VRAM Calculator to explore the memory budget, then check its architecture and cache assumptions against the model and runtime. A weights-only estimate cannot answer how many long conversations will fit.
Frequently asked questions
What is KV cache in large language models?
A KV cache stores keys and values from processed tokens so a model can reuse them while generating subsequent tokens.1
What is the difference between model weights and KV cache?
Weights are learned parameters used across requests. A KV cache holds attention state for a sequence during inference.2
Does KV cache reduce memory usage?
Caching retains additional state to save computation. Quantization, offloading and better allocation can reduce the GPU memory required for that state.52
Does 4-bit weight quantization also quantize the KV cache?
Cache quantization is a separate choice. A model with 4-bit weights can still use a 16-bit cache, so check both settings.5
Does prefix caching speed up every part of a response?
Prefix caching saves processing of a matching prefix during prefill. It does not eliminate decoding work for new answer tokens.6
References
- Hugging Face, Transformers documentation, "How caching works", attention matrices and cache storage dimensions, accessed 24 November 2026. https://huggingface.co/docs/transformers/en/cache_explanation
- Kwon, W. et al., vLLM, "vLLM: Easy, Fast, and Cheap LLM Serving with PagedAttention", 20 June 2023. https://vllm.ai/blog/2023-06-20-vllm
- Qwen, Qwen2.5-7B-Instruct official config.json: hidden_size 3584, num_attention_heads 28, num_hidden_layers 28, num_key_value_heads 4, max_position_embeddings 32768, use_sliding_window false; accessed 24 November 2026. https://huggingface.co/Qwen/Qwen2.5-7B-Instruct/blob/main/config.json
- Ainslie, J. et al., "GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints", EMNLP 2023, arXiv:2305.13245. https://arxiv.org/abs/2305.13245
- Hugging Face, Transformers documentation, "Cache strategies", static cache, quantized cache and offloading, accessed 24 November 2026. https://huggingface.co/docs/transformers/en/kv_cache
- vLLM documentation, "Automatic Prefix Caching", example workloads and limits, accessed 24 November 2026. https://docs.vllm.ai/en/latest/features/automatic_prefix_caching/


