Scroll down
AtomGradient Academy

KV Cache

Why are tokens cheaper in long conversations?

Attention Mechanism Exact Reuse Principle Memory vs Compute Hit Rate
About 10 min read
Chapter 01

How are K/V computed?

When each token enters the Attention layer, it is multiplied by two fixed weight matrices to produce a Key and a Value vector — these two vectors are what gets cached.

INPUT
token
embedding
Fixed at inference
×
WEIGHT
WK
Model weight fixed
WEIGHT
WV
Model weight fixed
OUTPUT
K vector
OUTPUT
V vector
CONCLUSION
Both inputs are fixed
→ Output is always identical
✓ Exact reuse, zero error

sin(30°) = 0.5 — once known, there's no need to recompute the trig function every time. KV Cache stores these "known results" in a lookup table for reuse.

Chapter 02

Cache Hit vs Miss

Tokens with a matching prefix are read directly from cache; only new tokens need to be computed.

▶ Request 1 (first time) — compute all and write to cache
You are an expert assistant
Please answer in English
User asks:
What's the weather?
⚡ KV CACHE
You are an expert assistant · K/V
Please answer in English · K/V
User asks · K/V
What's the weather · K/V
▶ Request 2 — matching prefix hits cache, only new tokens computed
You are an expert assistant
Hit ✓
Please answer in English
Hit ✓
User asks:
Compute →
How to cook?
Compute →
First computation, written to cache
Cache hit (computation skipped)
Cache miss (recomputed)
Chapter 03

User Messages & Model Replies Are Both Cached

The model sees just a stream of tokens — it doesn't distinguish who said what. All tokens need K/V, and all are stored in cache. As conversation turns increase, the hit rate keeps climbing.

System Prompt
You are an expert assistant
Please answer in English
User Turn 1
What is KV Cache?
Assistant Turn 1
KV Cache is a
caching mechanism...
User Turn 2
You are an expert assistant
Please answer in English
What is KV Cache?
KV Cache is...
Why is it cheaper?
Assistant Turn 2
All history hit ✓
Because cached...
Overall hit rate 0%
Turn 1: 0% Turn 2: ~70% Turn 10: ~90%+
Chapter 04

Same 1000 Input Tokens, Compute Comparison

Cache hit = skip K/V matrix multiplication. Compute drops from O(N) to approximately O(1). Providers discount cached tokens accordingly, typically around 10% of the normal price.

Scenario 1: Fixed System Prompt
Save ~50%
No cache1000 units
1000 units (full)
With cache (50% hit)~500 units
~500 units
Scenario 2: Multi-turn Chat (Turn 10)
Save ~90%
No cache1000 units
1000 units (full)
With cache (90% hit)~100 units
~100 units
Scenario 3: RAG Long Document Reuse
Save ~88%
No cache1000 units
1000 units (full)
With cache (88% hit)~120 units
~120 units
Chapter 05

The True Shape of KV Cache: 4D Tensor

It's not "a single matrix" — it's multiple sets of vectors stored separately by layer and attention head.

[ L layers ] × [ H attention heads ] × [ N tokens ] × [ d head dimension ]
L
Layers
32 layers

Each layer stores K/V independently

H
Attention Heads
32 heads

Multi-head attention, each head computes independently

N
Token Count
Grows linearly with context

Longer conversations use more memory

d
Head Dimension
128 dims

Usually fixed, determined by model architecture

📐 Actual size: 2 × 32 layers × 32 heads × 1000 tokens × 128 dims × 2 bytes (fp16) ≈ 500 MB — memory usage grows linearly with context length

Summary

KV Cache at a Glance

📐
Essence

K/V are outputs of a deterministic function. Both inputs (token embedding + weights) are fixed, so the result is always the same and can be safely reused.

💰
Why It's Cheaper

Cache hit = skip matrix multiplication, saving GPU compute. Providers discount cached tokens at roughly 10% of the normal price.

🎯
Precision Guarantee

Not an approximation — it's bit-for-bit identical floating-point values. Exactly the same as recomputation, zero error, no impact on output quality.

💬
Cache Scope

Both user messages and model replies are cached. The model doesn't distinguish who said what — all tokens need K/V and are treated equally.

🗂
Storage Shape

A 4D tensor [L layers × H heads × N tokens × d dims]. Longer context means more memory — a tradeoff between memory and compute.

✍️
Best Practice

Put stable content (System Prompt / documents) at the beginning, append changing content at the end — this maximizes prefix cache hits.

KV Cache = trading memory for compute time · the higher the hit rate, the cheaper the tokens

Next: ANE Hybrid Inference →