← Tokens, Context & Inference
03 · Tokens, Context & Inference · Topic 05

What is KV Cache?

KV cache stores attention information from earlier tokens so generation can continue more efficiently.

What is it?

KV cache is temporary memory used during generation to reuse attention information from earlier tokens.

In a Transformer, attention uses key and value states. Keeping those states avoids recomputing the same earlier context for every new token. The cache uses memory and grows as the sequence grows.

Think of it like...

Think of KV cache like keeping notes while reading a long document.

When you continue reading, you use the notes instead of rereading every earlier page. The analogy explains reuse during one generation, not permanent memory across separate requests.

How it works

1 · ReadReceive the contextThe model processes the prompt and earlier tokens.
2 · BuildCreate key and value statesAttention information is computed for the sequence.
3 · StoreKeep the statesThe runtime stores them in the KV cache.
4 · GenerateAdd the next tokenThe new token uses cached earlier states.
5 · UpdateExtend the cacheThe new key and value states join the cache.

Real-world examples

Everyday example · Long chat

Input: a conversation that keeps growing

System: reuses earlier attention states during generation

Output: less repeated computation for each new token

AI product example · Streaming answer

Input: a prompt that needs a long response

System: updates the cache as tokens are generated

Output: continued generation with lower repeated work

What it is NOT

KV Cache ≠ Context Window

KV cache: runtime data used to speed up attention during generation.

Context window: the amount of information the model can work with.

KV Cache ≠ Long-term Memory

KV cache: usually belongs to one active generation.

Long-term memory: information stored for later use across interactions.

KV Cache ≠ Model Parameters

KV cache: temporary request-specific states.

Model parameters: learned values that are part of the model.

Remember this

KV cache reuses earlier attention states to make ongoing generation more efficient.

Independent explainer

Video

visual explainer
↑