InferenceAI

Enter at least two characters to search.

Start free
INTERACTIVE LESSON LIBRARY

KV caching

Decode tokens while keeping keys and values aligned in a bounded window.

INTERACTIVE ENGINEERING · TEACHING MODEL

KV caching

Decode tokens while keeping keys and values aligned in a bounded window.

Retained token positions

Token 1K1V1
EmptyKV
EmptyKV
EmptyKV
Retained pairs1
Evicted pairs0

Dense example: 1 request × 1 tokens × 32 layers × 8 KV heads × 128 dimensions × 2 bytes × 2 (K and V) = 0.125 MiB.

Change the experiment

Core calculation
keys = (keys + [new_key])[-capacity:]
values = (values + [new_value])[-capacity:]
Try it yourself

Set a capacity of two and decode five tokens. Which pairs remain, and how many were evicted?

1 tokens decoded. 1 cached pairs; 0 evicted.

Cached keys and values avoid recomputing their projections for earlier tokens. Attention still reads the retained cache.

Playback pauses in hidden tabs and offscreen. With reduced motion enabled, use the timeline or Next step.

Turn the idea into an implementation

Set a capacity of two and decode five tokens. Which pairs remain, and how many were evicted?

Predict a result, change one input, then explain what happened. Use the timeline to revisit a state and compare it with the algorithm sketch.

Explain it in an interview

Cached keys and values avoid recomputing their projections for earlier tokens. Attention still reads the retained cache.

Describe the inputs, the state that changes, the computational cost and a limitation of the model before discussing a production implementation.