Learn AI Math

KV Cache

What is KV Cache?

In one sentence: The KV cache is the model's short-term memory of the keys and values it already computed for earlier tokens.

Without it, the model would redo work for every earlier token each time it writes a new one. The KV cache saves that work so each new token only needs a small amount of new math. But it grows with the conversation and can use a lot of memory.

Σ Quick math / rule of thumb: Without a KV cache, each new token would mean re-processing the entire conversation so far. With it, the model processes only the new token and reuses the stored keys and values for everything before it. The trade-off: the cache grows with every token, using more memory.

What does KV Cache look like?

In a long chat, the KV cache can take several gigabytes of GPU memory.

Why does KV Cache matter?

The KV cache makes generation fast, but it is often what limits context length and the number of users.

Last updated · Fact-checked by The AI Math

Linking to this page from a lesson or article?

Like this explanation? Get one every week.

The AI Math newsletter turns confusing AI news into plain English. No hype, no jargon walls.