What is a key-value (KV) cache?
A key-value (KV) cache is the memory in which a Transformer language model keeps the attention keys and values computed for earlier tokens, so it does not recompute them at every generation step.
It sits in GPU memory next to the model weights and grows with the context. Unlike prompt caching, which reuses this data across requests that share a prompt prefix, the KV cache lasts for a single request.
- Topic
- Inference and performance
- Also known as
- KV cache, key/value cache
How a key-value (KV) cache works
During prefill the keys and values of every prompt token are computed at each layer and written to the cache. During decoding the model computes vectors only for each new token and reads the rest from the cache.
Cache size grows in proportion to layer count, key-value head count, context length and the number of concurrent requests. PagedAttention stores the cache in blocks to limit memory fragmentation.
In more detail
The KV cache capacity formula and a worked example
Capacity planning starts from the per-token memory: 2 × layers × KV heads × head size × bytes per element. For example, a 70B-class GQA model with 80 layers, 8 KV heads and head size 128 needs about 320 KB per token in FP16, so a single 32,000-token request needs about 10 GB of VRAM for its KV cache alone and 10 concurrent users about 100 GB.
Prefill versus decode bottlenecks and disaggregated serving
Prefill is compute-bound (processing all prompt tokens in parallel) whereas decode is memory-bandwidth-bound (reading the growing KV cache from HBM for every single generated token). To avoid prefill spikes stalling ongoing decode requests, current enterprise serving stacks increasingly use disaggregated prefill-decode pools and offload cold KV blocks to CPU RAM or NVMe storage.
Architectural shrinking from MHA to GQA and MLA
Modern architectures shrink the KV bottleneck at model level: Grouped-Query Attention (GQA) shares K/V projections across groups of query heads, while Multi-head Latent Attention (MLA) compresses keys and values into a low-rank latent representation, cutting KV footprint by over an order of magnitude compared to classic Multi-Head Attention (MHA).
Commonly confused terms
| Term | How it differs |
|---|---|
| Model weights | Model weights are fixed parameters loaded once into GPU memory regardless of traffic, whereas the KV cache is dynamic per-token activation state that grows linearly with sequence length and concurrent batch size. |
| PagedAttention | PagedAttention is a virtual-memory-inspired block allocation algorithm for managing the KV cache with minimal fragmentation; it is a memory-management technique for the KV cache, not a separate cache type. |
Why a key-value (KV) cache matters for on-premises AI
On an on-premise server GPU memory is fixed and the KV cache gets whatever the model weights leave free. It therefore largely decides how many users can be served at once and with how long a context. A deployment that looks fine in a small pilot can start queuing requests in production, so capacity tests combine the longest context with the heaviest concurrent load. Because the cache holds user context, memory handling between sessions is also reviewed for security.
An insurer's policy summarisation system runs smoothly in a small pilot. As concurrent requests rise in production, the insurer recalculates GPU capacity and maximum context length from the KV cache requirement.
Common questions
Why does memory run out when the model weights fit on the GPU?
The weights take up a fixed amount of space. The KV cache grows with context length and the number of concurrent requests until it fills the remaining memory.
How can the KV cache be made smaller?
The usual ways are choosing a model that uses grouped-query attention, serving it with an inference engine that supports PagedAttention, quantising the cache and capping the maximum context length.
Related terms
- GPU memory
- PagedAttention
- Context window
- Inference
- Continuous batching
- Prompt caching
- KV cache quantisation
- Prefix caching
- Semantic caching
- Prefill
- Grouped-query attention
- KV cache offloading
Sources
- Kwon et al., Efficient Memory Management for Large Language Model Serving with PagedAttention, 2023
- Pope et al., Efficiently Scaling Transformer Inference, 2022
Related page on this site: Supermicro page · In the glossary: Glossary, D to L