What is a KV cache in LLM inference?
A KV cache is the memory where a decoder-only language model keeps the key and value vectors of every token it has already processed, so each new token only needs its own keys and values computed. During generation the model produces one token per forward pass, and each new token's query has to attend to all earlier tokens. Without the cache, every pass would recompute keys and values for the whole prefix.
Every production LLM server uses a KV cache, from Hugging Face Transformers to vLLM, TensorRT-LLM, and llama.cpp. It turns generation from quadratic recomputation into one short pass per token, but it costs memory that grows with context length and batch size. That memory, often larger than the model weights, limits how many users a GPU can serve at once and how long a context it can hold, which is why grouped-query attention, cache quantization, and paged memory exist.
Key ideas
- A KV cache stores each token's keys and values so they are computed once, not at every generation step.
- The causal mask guarantees that cached keys and values never change when new tokens are appended.
- Prefill processes the prompt in parallel and is compute-bound, while decode emits one token per step and is memory-bound.
- Cache size equals 2 × layers × kv_heads × head_dim × bytes × tokens × batch.
- For a 7B model at fp16 the cache costs 0.5 MiB per token, so long contexts and large batches can outgrow the weights.
- Grouped-query attention, multi-query attention, cache quantization, and sliding windows shrink the cache, and paged allocation stops memory from being wasted.
Common questions
Does a KV cache change the model's output?
No. With exact arithmetic the cached and uncached computations produce the same logits, since the cache only stores values that would otherwise be recomputed. Small differences can appear from floating-point ordering in different kernels. Quantizing the cache to int8 or lower is an approximation and can shift outputs slightly.
What is prompt caching?
Prompt caching, also called prefix caching, keeps the KV cache of a shared prefix, such as a long system prompt or document, and reuses it across requests. A new request that starts with the same tokens skips their prefill. API providers often bill cached input tokens at a lower price for this reason.
Why is the first token slower than the rest?
The first token waits for prefill, which runs the whole prompt through the model before any output appears. This delay is called time to first token, and it grows with prompt length. Later tokens each take one decode step, so their pace depends mainly on memory bandwidth and batch size.
What is PagedAttention?
PagedAttention, introduced with the vLLM serving system by Kwon et al. (2023), stores the KV cache in fixed-size blocks instead of one contiguous buffer per sequence. Blocks are allocated as a sequence grows and can be shared between sequences with a common prefix. This removes most of the memory lost to reserving space for the maximum length, which lets a server batch more requests.
Related explainers
- Decoder-Only Transformers — The architecture behind GPT and modern LLMs
- How Self-Attention Works — Query, key, value — the core of self-attention
- Multi-Head Attention — Parallel attention heads learning different patterns
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
- Quantization — Shrinking model weights to 8 and 4 bits
- Mixture of Experts — Routing each token to a few experts in a much larger model
References
- Pope et al. (2022), "Efficiently Scaling Transformer Inference"
- Shazeer (2019), "Fast Transformer Decoding: One Write-Head is All You Need"
- Ainslie et al. (2023), "GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints"
- Kwon et al. (2023), "Efficient Memory Management for Large Language Model Serving with PagedAttention"
Last updated