What is the KV cache in LLMs?
The KV cache stores a language model's intermediate attention values so the model does not recompute them as it generates an answer, one token at a time.
The KV cache is temporary memory in which a language model stores intermediate results needed to generate an answer without repeating the same calculations at every step. The model processes text as tokens (small units of text) and uses attention, a calculation that relates different parts of the sequence to one another. For each token already processed, it stores two numerical representations, keys and values, used to compute attention for later tokens. Skipping recomputation costs memory: the longer the sequence actually held, the more space the cache can take up. Concurrent requests also compete for that memory. The cache is not an archive of answers and is distinct from the weights, the model's learned parameters. How it is managed depends on the architecture and the software running the model: some configurations retain or share part of the data across requests, while others release it when generation ends.
See also: Prompt cachingReusing the fixed part of a prompt across requests: the model does not reprocess it from scratch and costs plummet.
How much memory is needed
In the PagedAttention paper, the authors calculate that the 13-billion-parameter OPT model needs 800 KB of cache per token and up to 1.6 GB for a sequence of 2,048 tokens, with values stored at 16-bit precision. These figures are specific to that model and precision, not a universal per-token rate. The weights, other working buffers, and software-reserved space take up additional memory.
For a given architecture and precision, retaining more tokens and serving more independent sequences raise the memory requirement. Alongside the context window, actual sequence lengths, request duration, and the inference engine's allocation method matter. In some architectures, layers that use sliding-window attention stop growing their cache once it reaches the window size, as the Transformers documentation explains.
An enterprise example
A company hosts an assistant that reviews long contracts. Tested by a single user, it responds acceptably. As a deadline approaches, though, several staff members submit different documents at once. The team measures memory used by active requests, input and output text included, alongside response times. It repeats the test with short and long documents and different levels of concurrency. If memory runs out, the engine may have to pause requests, move data, or recompute it, which can delay responses: the test aims to find, before the deadline, the load at which memory runs out.
Why it matters for decision makers
When comparing vendors, keep weight size, cache memory, and actual capacity under load separate. Quantization, which represents values with fewer bits, can reduce cache memory, but answer quality and response times must be verified. The KIVI paper documents this tradeoff for one specific method and offers no guarantee for every model.
Prompt caching, by contrast, reuses work done on a prefix shared across requests: it can draw on the KV cache but has its own reuse conditions. When buying a service, ask how data is isolated, when memory is released, and what performance is guaranteed under the expected load. For an API service, these details often remain the provider's responsibility, but they still count in the evaluation.
Frequently asked questions
Related terms
- Prompt cachingReusing the fixed part of a prompt across requests: the model does not reprocess it from scratch and costs plummet.
- Prefill, decode, TTFT and ITLPrefill and decode are the phases that process the prompt and generate the answer; TTFT and ITL measure the initial wait and the gaps between streamed outputs.
- Continuous batchingContinuous batching changes which LLM requests are processed together at each iteration, freeing capacity when a response ends so others can be served at once.
- Quantization (quantizzazione)Quantization represents model values with fewer bits, reducing the memory footprint but introducing a quality trade-off that must be verified case by case.