Prompts Forms Resume Reports ★ Saved ★ Saved★ + Submit a tool
Models and algorithms

KV Cache

About 3422 characters Updated Nov 30, 2026 28 views AI wiki

KV Cache is the mechanism in autoregressive generation that caches the key and value vectors of historical positions, avoiding recomputation of the entire context each time a new token is generated. It is the precondition that makes an inference engine practical, and also the main source of memory pressure in long-context serving.

Core Principles

In the decoding stage the model adds only one token at a time, but attention requires that token to interact with the key and value vectors of all historical positions. Without caching, every generated token would require running the whole context through all layers again, so the computation accumulates linearly with the generated length and the total cost approaches the quadratic order. Caching means that each layer keeps the keys and values it computed for each position, so the next step only needs to compute the new keys and values for the new position, append them to the cache and then attend over the full cache. The query vectors need not be stored, because each step uses only the one at the newest position.

This change lowers the per-step computation from proportional to the total context length to roughly proportional to the model dimension plus the part interacting with the cache, and the generation speed improves markedly. The price is memory: the cache must store keys and values for every layer and every attention head, its size is proportional to the number of layers, heads, head dimension and sequence length, and it keeps growing as generation proceeds. In long-context and high-concurrency scenarios the cache easily exceeds the memory taken by the model parameters themselves and becomes the real ceiling on serving capacity. Hence a variety of optimizations: paged cache management to reduce fragmentation waste, reuse of the same cache by requests sharing a prefix, dropping keys and values that are too distant on a fixed window, quantizing the cache for compression,

From a system perspective, the cache is essentially trading memory for time: the repeated computation saved at each step is exchanged for a continuously growing state. The serving side therefore has to weigh concurrency, context length and per-request latency against each other repeatedly, and for an inference framework the quality of cache management often separates cost levels more than the model itself does.

Key Points

Memory grows linearly with sequence length is the core contradiction: doubling the length doubles the cache, so long-context inference is often forced to reduce concurrency.

Cooperation with grouped-query attention: letting several query heads share one set of key-value heads directly compresses the cache several times over, and it is the standard design of current mainstream models.

Paging and reuse: allocate the cache in pages and let it grow on demand, avoiding a reserved contiguous block; prefix reuse means the system prompt and the history of a multi-turn dialogue are stored only once.

Trade-offs in deployment: quantizing the cache saves memory but may lose a little accuracy; discarding old cache with a sliding window controls capacity,

Competition with batching: the same device must hold both the weights and the caches of all requests, and the larger the cache the smaller the batch it can hold, so throughput and latency trade off against each other.

Common Misconceptions

A common misunderstanding is that KV Cache is only an implementation detail that does not affect model capability, when on the serving side it directly determines the concurrency ceiling and the cost structure. Another is to ignore the difference in the training stage: training can process a whole sequence in parallel and needs no stepwise caching, so it is wrong to conflate the two.

Application Scenarios

Online inference of large models, long-document question answering, multi-turn dialogue, streaming generation, code completion, high-concurrency API services, and the question-answering pipelines of retrieval-augmented generation that repeatedly concatenate long contexts.