KV Cache

LLM foundationsContext and memoryPublished By Simon Budziak

A KV cache stores the key and value attention states a transformer calculates for earlier tokens, so the model can reuse them while generating the next token instead of recalculating the entire conversation. It makes token generation much faster, but its memory use grows with context length and concurrent requests.

During generation, a model must attend to the tokens that came before. Without a cache, it would repeat the same attention calculations after every new token. The cache retains those calculations layer by layer, then adds one new set of states for each generated token.

Why is the KV cache fast but expensive?

It avoids repeated work, so decoding can move forward one token at a time. The trade is memory, not free speed. Cache size grows with sequence length, model layers, attention heads, and the number of live requests. A busy server can run out of KV-cache capacity before it runs out of compute.

How is it different from prompt caching?

Prompt caching avoids reprocessing an identical prefix across separate requests, often through a provider-managed mechanism. The KV cache is the immediate working state of a running sequence. They both reward stable context, but they solve different layers of the same cost problem. A KV cache accelerates later decoding, not the initial reading of fresh input. A large context window is only useful if the serving system can hold the required state, and time to first token still includes reading new input before cached generation begins.

Frequently asked questions

Is a KV cache the same as prompt caching?

No. A KV cache is internal transformer state for processed tokens. Prompt caching reuses a stable prompt prefix across API requests and may use KV cache data underneath.

Why does a KV cache matter in production?

It consumes accelerator memory for every active sequence, so long contexts and high concurrency can limit throughput even when the model weights fit.

Summarize this page with

Train your team to build this