Home / Glossary / KV Cache

On this pageIn more detailWhy it mattersRelated termsRead more

KV Cache

A KV cache (key-value cache) is the stored intermediate data that lets a large language model generate each new token without reprocessing everything that came before.

In more detail

Language models built on the transformer design use a mechanism called attention, in which each token is related to all earlier tokens. For each token at each layer, the model computes two sets of values, known as keys and values.

Text is generated one token at a time. Without a cache, producing each new token would mean recomputing keys and values for the entire preceding sequence. With a cache, they are computed once, stored and reused. Each step then only processes the newest token.

The cache grows as the sequence grows. Its size depends on the number of layers, the model’s dimensions and the length of the context. For long conversations or documents it can occupy many gigabytes for a single request.

Why it matters

The KV cache trades memory for computation, and it is what makes text generation practical. It also makes memory a main constraint in AI inference. The number of requests an accelerator can serve at once is often limited by the memory available for caches, not by its arithmetic speed.

This has consequences for system design. Caches can be shared among requests that start with the same text. They can be moved to slower, cheaper memory or storage when a conversation is idle and brought back later. Managing them well across a cluster places demands on memory, storage and networking.

See also prefill and decode, the two phases that create and then use the cache.

Related terms

Read more