What is KV cache?
Memory in LLM inference holding the attention keys and values of already processed tokens. It avoids recomputation but grows with context length and batch size.
Japanese name: KVキャッシュ
Memory in LLM inference holding the attention keys and values of already processed tokens. It avoids recomputation but grows with context length and batch size.
Japanese name: KVキャッシュ
A transformer language model generates text one token at a time. In each attention layer the new token's query is compared with the keys of all earlier tokens, and the result weights their values. Because attention is causal, the keys and values of earlier tokens never change once computed. The KV cache stores them per layer, so each decoding step only computes the new token's query, key and value and appends the latter two, instead of reprocessing the entire sequence. This turns repeated full recomputation into a modest per-token cost.
Cache size is 2 (keys and values) × number of layers × key/value dimension per layer × number of tokens × batch size × bytes per element. As an illustration, a model with 32 layers, a hidden size of 4096 and standard multi-head attention needs about 0.5 MB per token in FP16. A 4096-token context then takes about 2 GB per sequence, and a batch of eight such sequences needs about 16 GB, more than the roughly 14 GB that the weights of a 7-billion-parameter FP16 model occupy.
Inference has two phases. Prefill processes the whole prompt in parallel and writes its keys and values into the cache; it is mostly limited by compute. Decode then produces one token per step, and each step must read the model weights plus the entire KV cache for that sequence, so it is usually limited by memory bandwidth. Larger batches share the weight reads among more requests and raise throughput, but they enlarge the KV cache and lengthen each step, increasing the latency of individual requests.
Several techniques reduce KV cache pressure. Grouped-query and multi-query attention let several query heads share one set of key and value heads, cutting cache size proportionally. Storing the cache in 8-bit or lower precision halves or further reduces memory relative to FP16. Allocating the cache in small fixed-size blocks, rather than one contiguous region per sequence sized for the maximum length, avoids fragmentation and lets more requests fit; it also allows sequences with a common prompt prefix to share blocks. Some models also limit attention to a sliding window of recent tokens.
Answer: C. It stores the attention keys and values of already processed tokens so they need not be recomputed for each new token
In attention, each new token refers to the keys and values of all earlier tokens. In text-generating models attention is causal, so no token refers to later tokens. Earlier keys and values therefore do not change, and storing them instead of recomputing means only the new token's share must be computed. That store is the KV cache. It cuts computation dramatically, but the amount of stored keys and values grows in proportion to sequence length and batch size.
It is memory holding the attention keys and values of tokens already processed, so each newly generated token only needs its own keys and values computed instead of recomputing the whole sequence.
Its size grows linearly with the number of layers, the attention width, the context length and the batch size, so long contexts or many concurrent requests can make it larger than the model weights.
Caching exact keys and values is mathematically equivalent to recomputing them; only approximations such as low-precision cache storage or dropping old tokens can change outputs.
KV cache appears in 5 practice questions across 1 topic, including AI Hardware & Software Stack.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.