System Design
19. AI System Design and LLM InfrastructureAnimated
KV Cache Management
Storing keys and values of earlier tokens.
The idea
The cache avoids recomputation but uses lots of GPU memory, so it must be managed, evicted and sometimes quantised.
Watch it work
Press play, pause, or step through. Some animations have settings you can change.
- computed
- cached
Token 1: with no cache the model recomputes keys and values for all 1 tokens again.
- token:
- 1
- work:
- 1
1/16
Without a cache the work grows with the square of the length; with it, linearly.
Step by step
- 1Compute K and V for each token once
- 2Store them
- 3Reuse in later steps
- 4Evict or compress when memory is tight
Where it is used
Every LLM server.
Watch out for
Long contexts and many users exhaust memory.