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.

Field: AI Hardware & Software Stack

Japanese name: KVキャッシュ

Why LLM inference keeps keys and values

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.

How big the KV cache gets

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.

Prefill, decode and memory bandwidth

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.

Ways to shrink or manage it

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.

Try it: questions about KV cache

What is the role of the KV cache in LLM inference?

  1. AIt stores frequent questions and answers to reply instantly to repeats
  2. BIt places all model weights in the CPU cache
  3. CIt stores the attention keys and values of already processed tokens so they need not be recomputed for each new token
Show answer and explanation

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.

FAQ

What is the KV cache in large language models?

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.

Why does the KV cache use so much memory?

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.

Does the KV cache affect output quality?

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.

Questions that mention KV cache (4)

KV cache appears in 5 practice questions across 1 topic, including AI Hardware & Software Stack.

  1. If a 7-billion-parameter (7B) model's weights are held in FP16 (2 bytes), roughly how much memory do the weights take?
  2. What is a drawback of making the inference batch size too large?
  3. LLM inference splits into a stage that processes the input prompt together (prefill) and a stage that generates output one token at a time (decode). With small batches, what mainly limits each stage?
  4. When LLM inference handles long contexts or large batches, what tends to become a problem?

Books on AI Hardware & Software Stack Ad

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.

Back to the glossary