← All briefings

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.

3 min read

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

Engraved illustration of a model chip wired to parallel rows of cache sockets, with a new red pair of key and value tiles.
Illustration © 2026 PulpBrief. All rights reserved.

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

Same model, different conversation memoryQwen2.5-7B-Instruct raw 16-bit KV cache, GiB; independent sequences, no prefix sharing
  • One sequence
  • Four sequences
0 GiB2 GiB4 GiB6 GiB8 GiB8,19216,38432,768
Source: PulpBrief calculations from Qwen's official configuration3 and cache dimensions1. Derived data and chart: CC BY 4.0.Chart: PulpBrief · CC BY 4.0
View data
Cached tokens per sequenceOne sequenceFour sequences
8,1920.44 GiB1.75 GiB
16,3840.88 GiB3.50 GiB
32,7681.75 GiB7.00 GiB

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

ApproachWhat it changesWhat to check
Shorter retained contextFewer cached token statesWhether the application still has the information it needs1
Paged allocationPacks cache into blocks allocated as neededReduces allocation waste; does not shrink each stored value2
Cache quantizationStores cache values in fewer bitsSupported models, quality and latency5
CPU offloadingMoves much of the cache out of GPU memoryTransfer 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. Hugging Face, Transformers documentation, "Cache strategies", static cache, quantized cache and offloading, accessed 24 November 2026. https://huggingface.co/docs/transformers/en/kv_cache
  6. vLLM documentation, "Automatic Prefix Caching", example workloads and limits, accessed 24 November 2026. https://docs.vllm.ai/en/latest/features/automatic_prefix_caching/

Cite this briefing

PulpBrief. (2026, November 24). What is a KV cache, and why long chats fill GPU memory. https://pulpbrief.com/brief/what-is-kv-cache/

BibTeX
@misc{pulpbrief2026what,
  author       = {{PulpBrief}},
  title        = {What is a KV cache, and why long chats fill GPU memory},
  year         = {2026},
  month        = nov,
  howpublished = {\url{https://pulpbrief.com/brief/what-is-kv-cache/}},
}

Charts, maps and derived figures by PulpBrief may be reused under CC BY 4.0 with credit. Source data stays with the sources named under each chart. Citing and reuse