A KV cache is a store of intermediate calculations a large language model keeps while generating text, so it does not redo the same work for every new word. KV stands for key and value, the two pieces of information the model’s attention mechanism computes for each token it has already processed. When a chatbot replies one word at a time, the KV cache lets it remember the earlier words cheaply instead of re-reading the whole conversation for each new word.
The idea matters because it sits at the centre of how modern AI chatbots, coding assistants, and search tools actually run. Every time a model answers, it performs an enormous amount of arithmetic, and the KV cache trades memory for speed: keep the keys and values in fast memory, and each new token costs far less compute. That trade-off shapes the cost of running AI services, the length of conversation a model can handle, and how many users one server can serve.
This explainer covers what a KV cache is, how it works, the main types and variants, the benefits and risks, and where you will meet it in the real world. It is written for readers who use AI tools every day and want to understand what is happening behind the reply, without needing a degree in machine learning.
Key takeaways
- A KV cache stores the key and value vectors a language model has already computed, so each new token reuses past work instead of recomputing it.
- The cache trades memory for speed: longer conversations need more memory, which is why context length is often limited by hardware.
- KV cache size grows with every token, so long chats and many simultaneous users compete for the same memory.
- Techniques such as grouped-query attention, quantisation, and paged attention reduce the memory cost without changing what the model can do.
- A KV cache holds the content of a conversation in memory, which raises real questions about privacy and data retention.
What a KV cache actually is
To understand a KV cache, start with how a language model reads text. It breaks a prompt into tokens, which are short chunks of words, and turns each token into a set of numbers called a vector. During generation, the model uses attention to decide which earlier tokens matter most for the next word. For each token, attention computes three vectors: query, key, and value. The query is compared with every key, and the matching values are combined to produce the next word.
The important detail is that the keys and values for earlier tokens do not change as new tokens arrive. Once the model has processed the first sentence of your prompt, its keys and values are fixed, and recomputing them would be pure waste. The KV cache is simply where those computed keys and values are stored, ready to be reused. Think of it like taking notes during a long meeting: you write a point down once, then check your notes instead of asking for the argument again. The way models handle the size limit is closely related to edge inference, where the same calculations run on smaller devices with far less memory to spare.
How the KV cache works step by step
The process has two phases. The first is called prefill, and it happens when you send a prompt. The model reads all the tokens in your prompt at once, computes the query, key, and value for each, and stores the keys and values in the cache. This phase happens only once per prompt, and it is where most of the waiting time comes from with a long document.

The second phase is called decoding, and it happens once for each word the model generates. The model takes the latest token, computes its query, key, and value, adds them to the cache, and compares the query against every key to choose the next word. Because the cache holds all the earlier keys and values, this step is fast, though the cost grows as the conversation gets longer, which is why providers charge more for long inputs.
Types and variants of KV cache
The simplest form is the standard full KV cache used by early transformer models, where every attention head stores its own complete set of keys and values for every token. It is straightforward but uses the most memory, and it is why long-context models were so expensive to run when they first appeared. Most production systems in 2027 use one of several leaner variants instead.
Multi-query attention is the first major variant: instead of each attention head keeping its own keys and values, all the heads share a single set, cutting the memory cost sharply at a small cost to quality. Grouped-query attention is a middle ground: the heads are divided into groups, and each group shares one set of keys and values. It keeps most of the quality of the full cache while saving most of the memory.
Beyond the attention design, there are techniques that change how the cache is stored and managed. Quantisation stores the keys and values in fewer bits, for example eight instead of sixteen, which halves the memory at a small cost to precision. Paged attention stores the cache in small blocks rather than one long stretch of memory, which reduces waste and lets many conversations share a server more efficiently. Offloading moves part of the cache to slower memory when fast memory fills up, and sliding window attention keeps only the most recent tokens.
Benefits of using a KV cache
The main benefit is speed. Without a cache, generating a reply of 500 words would mean reprocessing the entire conversation 500 times, which would make interactive chat unusable. With a cache, each word costs roughly the same amount of work, and the model can stream its reply as it goes. That streaming is what makes a chatbot feel responsive.
The second benefit is cost. Compute is the most expensive part of running a language model, and the KV cache removes the largest source of repeated work. A provider serving thousands of users saves an enormous amount of processing by caching. The same logic applies to local models: the question of how much unified memory local AI models need is largely a question about how large a KV cache the machine can hold.
The third benefit is that the cache makes long conversations possible at all. A model that could only remember the last few sentences would be useless for editing a document or debugging code. The KV cache keeps the whole conversation in play, so a question in the tenth message can still refer to a detail from the first. That continuity is one of the main reasons modern AI assistants feel useful rather than forgetful.
Risks, limits, and trade-offs
The biggest limit is memory. The cache grows with every token, and faster for larger models, which have more attention layers and wider vectors. A long document can produce a cache of several gigabytes for one conversation, and a server handling hundreds at once needs an enormous amount of memory. When the cache does not fit, the system must shorten the conversation, slow it down, or refuse new users until memory frees up.
There is also a privacy angle that is easy to overlook. The KV cache is not the original text, but it is a mathematical record of it, and in principle it can be used to recover parts of the conversation. If a cache is kept after a chat ends, it carries the same sensitivity as the text itself. Responsible providers clear caches when a session ends, but anyone handling sensitive conversations should ask how long caches are kept.
Real-world uses of the KV cache
The most visible use is in chatbots, where the KV cache is what makes a long conversation feel instant. When you paste a long document into a chat and ask questions about it, the prefill phase processes the document once, and every follow-up question reuses that cache. That is why the first question about a long document takes a while and later ones feel fast. Coding assistants work the same way: the cache holds your file, so each suggestion builds on the code already seen.
Behind the scenes, the KV cache shapes how AI services are built and priced. Providers design servers around cache memory and charge for long contexts because they consume the most memory. Our guide to VPS hosting for AI apps covers the memory and hardware trade-offs that matter when an application holds large caches for many users. The cache also matters on devices, where phones and laptops have far less memory.
Checklist: understanding a KV cache
Use these checks to test whether you have a working grasp of the idea.
- You can explain why a model stores keys and values instead of recomputing them for every new word.
- You can name the two phases, prefill and decoding, and say which one happens once per prompt and which once per word.
- You know that cache size grows with conversation length, model size, and the number of simultaneous users.
- You can name two ways to shrink the cache, such as grouped-query attention or quantisation.
- You understand that the cache holds a record of the conversation, so it needs the same care as the text itself.
- You can tell a KV cache apart from prompt caching and from ordinary processor cache.
Frequently asked questions
What does KV stand for in KV cache?
KV stands for key and value, the two vectors that the attention mechanism computes for each token. The query is computed fresh for each new token, while earlier keys and values are stored in the cache and reused.
Why does a KV cache use so much memory?
Because it stores a key and a value vector for every token, in every layer of the model. A long conversation with a large model therefore produces a very large cache, and memory grows with the number of tokens.
Does the KV cache affect the quality of the model’s answers?
The full KV cache does not change the answers; it only makes them faster to produce. Leaner variants can change the answers slightly, but most users will not notice the difference.
Can a KV cache be shared between users?
It can be shared when users send the same prompt, but a cache holding one user’s conversation must never be reused for another user. Sharing a conversation cache across users would leak private information.
What happens when the KV cache runs out of memory?
The system must stop the conversation, drop the oldest tokens, move part of the cache to slower memory, or compress it. Which option is chosen depends on the provider.
Is the KV cache the same as the model’s memory of its training data?
No. Training knowledge lives in the model’s weights, which are fixed after training. The KV cache is short-term working memory for the current conversation only.
The bottom line
A KV cache is a simple idea with large consequences: store the work a language model has already done, and reuse it instead of redoing it for every new word. That single trade, memory for speed, is what makes interactive AI chat possible, what sets the cost of running a model, and what limits how long a conversation can be. The variants that shrink the cache are all ways of getting more conversation out of the same amount of memory.
For anyone who uses AI tools, the practical lesson is straightforward. Long inputs cost more because they build bigger caches, long conversations slow down because every new word looks back at everything before it, and memory matters as much as the processor. Understand the KV cache, and the behaviour of the chatbots you use every day starts to make sense.

