Understanding KV Cache: Why Chatbots Can Reply So Fast

When you chat with an AI like ChatGPT or Claude, the reply shows up one word at a time. Have you ever wondered how it keeps up that speed, even after you've pasted in a long document?
A big part of the answer is a simple trick called the KV Cache. Almost every modern chatbot uses it. It's also one of the main reasons AI needs so much GPU memory.
This article explains what the KV Cache is, why it exists, and why engineers spend so much time working on it. You won't need any maths.
First, How Does a Chatbot Write?
A Large Language Model (LLM) doesn't write a whole sentence at once. It writes one small piece at a time, and that piece is called a token. A token is usually a word or part of a word.
Here is what happens when you ask, "What is the capital of France?"
| Step | What the Model Sees | What It Writes Next |
|---|---|---|
| 1 | What is the capital of France? | The |
| 2 | What is the capital of France? The | capital |
| 3 | What is the capital of France? The capital | is |
| 4 | What is the capital of France? The capital is | Paris |
Notice the pattern. At each step the model looks at everything so far and then adds one new token. This is called generating "one token at a time."
The Hidden Problem: Doing the Same Work Again and Again
To choose the next word, the model has to understand how all the earlier words relate to each other. This step is called Attention. It's how the model knows that "it" in a sentence refers to "the cat" and not "the table."
For attention, the model makes three small notes for every token:
| Note | Simple Meaning | Everyday Example |
|---|---|---|
| Query (Q) | "What am I looking for?" | A question you ask in a library |
| Key (K) | "What do I contain?" | The label on each book's spine |
| Value (V) | "Here is my actual information." | The content inside the book |
The new token uses its Query to check every earlier token's Key, finds the ones that matter, and then reads their Values.
Here is the problem. Without any trick, the model recalculates the Keys and Values for ALL earlier tokens at every single step, even though those tokens never change.
Imagine reading a book. Every time you turn a page, you go back and reread the whole book from page 1 before continuing. That is extremely slow, and it gets worse the longer the book is.
The Fix: Just Save Your Notes
The KV Cache fixes this with a very simple idea.
KV Cache Once the model works out the Key and Value for a token, it saves them in memory. On the next step it reuses the saved notes and only calculates the Key and Value for the one new token.
Back to the book example: instead of rereading the whole book, you keep sticky notes on what each page said. When you turn a page, you read only the new page and glance at your sticky notes for the rest.
| Without KV Cache | With KV Cache |
|---|---|
| Recalculates notes for every earlier token, every step | Calculates notes only for the newest token |
| Work grows bigger and bigger with each new word | Work per new word stays small |
| Slow for long replies | Fast for long replies |
| Uses less memory | Uses more memory to store saved notes |
That last row is the catch. The KV Cache trades memory for speed.
Why Only K and V? Why Not Q?
Beginners often ask this, and it's a good question.
The Query belongs to the token that is asking the question, which is always the newest token. Once that token has done its job, its Query is never needed again.
Keys and Values are different. Every future token will need to check them, so they are worth saving.
| Note | Needed Again Later? | Saved in Cache? |
|---|---|---|
| Query | No, only the newest token uses it | No |
| Key | Yes, every future token checks it | Yes |
| Value | Yes, every future token reads it | Yes |
The Two Phases of a Chat Reply
When you send a message, the model works in two phases.
Phase 1: Prefill The model reads your whole prompt at once and builds the KV Cache for all of it. This is why there's sometimes a short pause before the first word appears, especially with long prompts.
Phase 2: Decode The model writes the reply one token at a time. Each new token adds its own Key and Value to the cache, so the cache grows with every word.
| Phase | What Happens | Speed Feeling |
|---|---|---|
| Prefill | Reads the full prompt and fills the cache | The short wait before the first word |
| Decode | Writes one token at a time and adds to the cache | Words streaming onto the screen |
The Price: KV Cache Gets Big, Fast
Saving notes is great for speed, but those notes take up space on the GPU. The cache gets bigger when there are:
| What Grows | Why the Cache Grows |
|---|---|
| Longer conversations | Every token needs its own saved notes |
| Bigger models | More layers means more notes per token |
| More users at once | Each user's chat has its own cache |
Here is a rough example for a 7-billion-parameter model like Llama 2 7B, using standard 16-bit numbers:
| Conversation Length | Approximate KV Cache Size |
|---|---|
| 1 token | About 0.5 MB |
| 1,000 tokens | About 0.5 GB |
| 4,000 tokens | About 2 GB |
| 32,000 tokens | About 16 GB |
The model weights themselves are about 13 to 14 GB. With a long chat, the KV Cache can end up bigger than the model. Serve many users at once and memory runs out quickly.
This is why "long context" is expensive, and why so many engineers focus on making the KV Cache smaller and smarter.
How Engineers Shrink the KV Cache
Here are the most common ideas, explained simply.
Grouped-Query Attention (GQA) Instead of every attention "head" keeping its own set of notes, a group of heads shares one set. It's like a study group sharing one notebook instead of each person writing their own. Llama 3 8B uses this and needs about 4 times less KV memory per token than Llama 2 7B.
Multi-Query Attention (MQA) This is the extreme version of GQA: all heads share just one set of notes. It saves the most memory, though quality can drop a little.
KV Cache Quantization The saved notes are stored with fewer digits, for example 8-bit or 4-bit numbers instead of 16-bit. It's like writing "approx. 3.14" instead of "3.14159265." It takes less space and is usually close enough.
PagedAttention This idea comes from the vLLM project. Instead of reserving one big block of memory for each chat, the cache is stored in small pages, like an operating system manages computer memory. Less space is wasted, so more users fit on one GPU.
Prefix Caching Many chats start with the same long instructions, such as a system prompt. The model computes the cache for that shared part once and reuses it for everyone, like photocopying a common first page instead of handwriting it each time.
Sliding Window The model keeps saved notes for only the most recent tokens and drops the oldest ones. This saves memory, but the model may "forget" very early parts of a long chat.
| Technique | Main Idea | Saves Memory By | Trade-off |
|---|---|---|---|
| GQA | Groups of heads share notes | Fewer sets of K and V | Very small quality impact |
| MQA | All heads share one set | Far fewer K and V | Some quality loss possible |
| Quantization | Store notes with fewer digits | Smaller numbers | Tiny accuracy loss |
| PagedAttention | Store cache in small pages | Less wasted space | More complex system |
| Prefix Caching | Reuse cache for shared starts | No repeated work | Only helps with shared prompts |
| Sliding Window | Keep only recent notes | Dropping old tokens | May forget early content |
Why This Matters to You
Even if you never build a model, the KV Cache explains many things you see every day:
| What You Notice | KV Cache Reason |
|---|---|
| A pause before the first word with long prompts | Prefill is building the cache |
| Words stream out quickly after that | Decode reuses saved notes |
| Long-context models cost more | A bigger cache needs more GPU memory |
| "Cached input" is cheaper on some AI APIs | Prefix caching reuses saved work |
| Running big models locally runs out of memory | The cache grows with chat length |
If you run models on your own machine with tools like llama.cpp or Ollama, you'll see settings such as context size and KV cache type. Now you know what they control: how many notes are kept, and how tightly they are packed.
Key Takeaways
- LLMs write one token at a time, and each new token looks back at all earlier ones.
- Attention uses three notes per token: Query, Key and Value.
- The KV Cache saves Keys and Values so they aren't recalculated at every step.
- This makes replies much faster but uses a lot of GPU memory.
- Techniques like GQA, quantization and PagedAttention keep the cache under control.
The KV Cache is a perfect example of a common idea in computing: remember what you've already worked out, so you don't have to do it again. It's a small trick, but it's one of the reasons modern AI chat feels instant.