Large language models generate text one token at a time. Without an optimization, every new token would force the model to repeat attention calculations for tokens it has already processed.
KV caching avoids much of that repeated work. During inference, the model stores the key and value representations produced by attention layers for previous tokens. When generating the next token, it can reuse those stored representations instead of recomputing them from the beginning.
This makes autoregressive generation much faster, but the cache consumes memory and grows with the active context. Understanding that trade-off is useful when choosing context lengths, batch sizes, model sizes, and serving strategies.
Why autoregressive generation repeats work
A decoder-style language model predicts the next token from all tokens that came before it. Consider a short sequence:
The model generates textTo predict the next token, attention needs information derived from each existing token. After a new token is generated, the model predicts again using the longer sequence.
A naive implementation could process the full prefix on every generation step:
step 1: [The model] -> generates
step 2: [The model generates] -> text
step 3: [The model generates text] -> ...Most of the prefix is unchanged between steps. Recomputing its attention keys and values repeatedly wastes computation.
KV caching stores those reusable results.
What keys and values represent
In a transformer attention layer, token representations are projected into queries, keys, and values. Conceptually, attention compares a query with keys to determine how strongly to use the corresponding values.
For causal generation, the newest token produces a new query, key, and value. The keys and values from earlier tokens do not need to change merely because another token was appended.
The model can therefore keep them:
previous tokens
|
v
[keys, values] ----+
|
new token ----------+--> attention --> next-token computationThis stored collection is the KV cache.
The cache exists separately for attention layers, so its total size depends on the model architecture rather than only on the number of input tokens.
Prefill and decode are different phases
LLM inference is often easier to understand as two phases.
During prefill, the model processes the input prompt. It performs substantial parallel computation across the prompt tokens and builds the initial KV cache.
During decode, the model generates tokens sequentially. Each generation step adds new key and value entries to the cache and reuses entries from earlier tokens.
prompt
|
v
prefill --------> initial KV cache
|
v
decode token 1 -> larger cache
|
decode token 2 -> larger cache
|
decode token 3 -> larger cacheThis distinction explains why time to the first generated token and the speed of later tokens can behave differently. A long prompt increases prefill work, while long generation repeatedly exercises the decode path.
The cache trades memory for computation
KV caching does not make attention free. The model still needs to compare the current query against cached keys and combine relevant values. The optimization mainly prevents repeated calculation of keys and values for the unchanged prefix.
The cost is memory.
A useful conceptual relationship is:
KV cache size
~ active tokens
x attention layers
x stored key/value dimensions
x bytes per stored elementExact memory use depends on architecture. Models may use standard multi-head attention, multi-query attention, grouped-query attention, different head dimensions, and different numerical formats. These choices can significantly change cache requirements.
The important operational point is that a longer active context usually means a larger KV cache.
Long contexts can reduce serving capacity
Suppose a serving system has enough accelerator memory for many short requests. If several users suddenly send very long prompts and request long outputs, their KV caches can occupy a much larger fraction of available memory.
That can reduce the number of requests that fit concurrently.
For this reason, a model supporting a very large maximum context window does not mean every request should use that maximum. Context length is also a resource-management decision.
A practical service may enforce limits such as:
maximum input tokens
maximum output tokens
maximum total active tokens per requestThese controls can improve capacity planning and prevent a few large requests from consuming disproportionate memory.
Batch size and context length interact
Batching improves accelerator utilization by processing work from multiple requests together. KV caches complicate the picture because every active sequence needs cache storage.
A rough mental model is:
more concurrent sequences + longer sequences = more KV-cache memoryA configuration that handles a large batch of short prompts may not support the same batch size for long conversations.
This is one reason production LLM serving often schedules work according to tokens rather than treating every request as equally expensive.
Shared prefixes can create another optimization opportunity
Many requests may begin with identical content, such as a long system instruction, tool specification, or common document prefix. If the serving stack supports prefix caching, it may be able to reuse computation associated with that shared prefix instead of performing the same prefill work for every request.
Prefix caching and ordinary per-request KV caching are related but not identical ideas. Ordinary KV caching reuses a request’s previous attention state during generation. Prefix caching attempts to reuse compatible prefix state across repeated inputs or requests.
Whether this is available depends on the inference system, and reuse is only valid when the cached model state corresponds to the same effective prefix and compatible model configuration.
Cache precision introduces another trade-off
KV-cache entries consume memory according to their numerical representation. Some inference systems can store cache data at reduced precision to lower memory use and potentially increase serving capacity.
Lower precision is not automatically free. It may affect model output quality, and the impact can depend on the model, workload, context length, and chosen format.
Treat cache quantization as an empirical optimization:
- Define representative prompts and expected outputs.
- Measure memory and throughput with the baseline cache format.
- Enable the lower-precision cache format.
- Repeat quality and performance evaluation.
- Keep the optimization only if the quality trade-off is acceptable.
A memory saving is useful only when the resulting system still meets application requirements.
KV caching does not remove context-window limits
It is easy to confuse caching with extra model capacity. A KV cache stores intermediate representations for tokens the model is already allowed to attend to. It does not increase the model’s supported context window.
If a model or serving configuration allows a certain number of active tokens, caching those tokens more efficiently does not permit an unlimited conversation.
Applications still need context-management strategies such as removing irrelevant history, summarizing older material when appropriate, retrieving only useful evidence, and reserving enough room for the expected output.
Measure latency in more than one way
A single average response time can hide important inference behavior. For interactive LLM applications, useful measurements include:
- Time to first token: how long the user waits before generation begins.
- Inter-token latency: how quickly subsequent tokens arrive.
- Input throughput: how efficiently prompts are processed during prefill.
- Output throughput: how many generated tokens the system produces over time.
- Concurrent capacity: how many active requests the available memory can sustain.
KV caching primarily helps the repeated computation in autoregressive decoding, but its memory footprint also affects concurrency. Performance tuning should therefore consider latency, throughput, and memory together.
Practical guidance
When operating an LLM service, start with the workload rather than the maximum capabilities printed for the model.
Measure realistic input and output lengths. Observe KV-cache memory under concurrent load. Set token limits that reflect actual product needs. Test batching with both short and long contexts. If the inference stack supports cache quantization or prefix reuse, benchmark those features independently instead of assuming they always improve the system.
Also distinguish prompt-processing bottlenecks from generation bottlenecks. If users wait mostly for long prompts to be processed, optimizing decode alone may not solve the problem. If generation is slow under heavy concurrency, cache memory and scheduling may be central constraints.
Conclusion
KV caching is a fundamental optimization for autoregressive LLM inference. By storing attention keys and values from previous tokens, it avoids recomputing unchanged prefix representations at every generation step.
The benefit comes with a clear trade-off: cached state consumes memory and grows with active sequence length and concurrency. That makes KV caching more than an implementation detail. It connects transformer attention directly to practical decisions about context windows, batching, latency, throughput, and serving capacity.
For production systems, the right question is not simply whether KV caching is enabled. It is whether the cache configuration, context limits, and scheduling strategy fit the workload the application actually serves.