KV Cache and Cache Hit Rates: The Technology Behind Lower LLM API Prices
KV caching reduces redundant computation during large language model inference, while cross-request prefix reuse can further lower processing costs. This article explains how KV caches work, how to calculate their GPU memory requirements, why some workloads achieve very high cache hit rates, and how developers can structure prompts to improve cache reuse.
1. The Technical Economics Behind API Price Cuts
A Chinese large language model provider recently announced a permanent reduction in its flagship model’s API pricing to one-quarter of the previous rate. While such cuts reflect competitive pressure, they can also be supported by real reductions in inference costs. One important factor is more effective caching.
Some developers have reported cache hit rates approaching 99% in workloads such as multi-turn conversations and long-document processing. When most of an input matches a reusable cached prefix, the service can avoid much of the computation needed to process that input.
Calling this “almost free” is an exaggeration: generating new output still requires computation. Nevertheless, high cache reuse can substantially reduce the cost of processing repeated context.
To understand why, we need to start with how language models generate text.
2. Autoregressive Generation and Redundant Computation
Most mainstream large language models use Transformer architectures and generate text autoregressively. They produce one token at a time, append it to the sequence, and then predict the next token.
For a simplified illustration:
- Step 1: Given “The weather,” the model generates “is.”
- Step 2: Given “The weather is,” it generates “nice.”
- Step 3: Given “The weather is nice,” it predicts the next token.
Actual token boundaries depend on the tokenizer, but the generation process follows this pattern.
Within each attention layer, token representations produce three types of vectors:
- Q — Query
- K — Key
- V — Value
Without caching, processing the growing sequence repeatedly would recompute the keys and values for earlier tokens. In a causal Transformer, those earlier tokens’ keys and values do not change when a new token is appended.
As the sequence grows, this redundant work becomes increasingly expensive.
3. KV Cache: Reusing Computation During Inference
A KV cache, or key-value cache, avoids this repeated work by storing previously computed keys and values in GPU memory.
The process is straightforward:
- Compute the keys and values for the input tokens and store them.
- During subsequent generation steps, read the historical keys and values from the cache instead of recomputing them.
- Process the newest token to produce its query, key, and value vectors. Use its query with the available keys to calculate attention scores, then use those scores to form a weighted combination of the values.
This means the model no longer needs to run the entire historical sequence through every layer at each generation step. It processes the newest token while reusing the cached representations of earlier tokens.
However, the new token still attends to the preceding context. KV caching eliminates redundant computation; it does not eliminate the work of reading and attending to the cached sequence.
The benefit is especially important for long-context workloads.
4. Calculating the GPU Memory Cost of KV Caching
KV caching trades GPU memory for faster inference. For a conventional key-value cache, its size can be calculated as:
KV cache size =2 × number of layers × number of KV heads × head dimension× sequence length × bytes per element
The factor of two accounts for storing both keys and values. For multiple independent sequences, multiply by the batch size.
Consider a hypothetical model with:
- 32 layers
- 32 KV heads
- A head dimension of 128
- A sequence length of 4,096 tokens
- FP16 storage, requiring 2 bytes per element
The memory requirements are:
KV storage per token per layer:2 × 32 × 128 × 2 = 16,384 bytes = 16 KiBKV storage for the full sequence:16 KiB × 32 × 4,096 = 2 GiBKV storage for a batch of 10:2 GiB × 10 = 20 GiB
For long sequences or large batches, KV cache memory can become a deployment bottleneck and may even exceed the memory occupied by model parameters.
This pressure helps explain the importance of approaches such as:
- Multi-Query Attention (MQA)
- Grouped-Query Attention (GQA)
- Multi-head Latent Attention (MLA)
- PagedAttention
MQA, GQA, and MLA reduce the amount of state needed for attention caching through architectural changes. PagedAttention addresses how cache memory is organized and managed. These approaches tackle different aspects of the same problem: making KV storage more practical and efficient.
5. Why Cache Hit Rates Can Approach 99%
KV caching within a generation request and cache reuse across requests are closely related, but distinct.
Within a request, the cache prevents repeated computation as tokens are generated. Across requests, a serving system can reuse cached KV state when requests share the same prefix—for example, a system prompt, a fixed document, or an existing conversation history.
The matching portion of the prefix does not need to be processed again.
Several common workloads naturally create substantial opportunities for reuse:
- Multi-turn conversations: Each new request preserves the earlier conversation and adds new content.
- Agent workflows: System instructions and tool definitions recur across multiple calls.
- Long-document question answering: Repeated questions about the same document can reuse its cached prefix.
- Batch processing: Many requests share a template or instruction prefix.
When the reused prefix accounts for nearly all the input, a token-based input cache hit rate can approach 99%. That can sharply reduce repeated input-processing work.
It does not mean that 99% of all inference computation disappears: new input and generated output still require processing. Even so, extensive prefix reuse can lower a provider’s marginal serving costs and help support sustained price reductions rather than temporary subsidies.
6. Practical Ways to Improve Cache Reuse
Developers can design applications to take better advantage of caching.
Keep prefixes stable
Place fixed content—such as system instructions and few-shot examples—at the beginning of the prompt. Avoid inserting changing content into that prefix, because it can prevent later content from matching the cached sequence.
Preserve conversation history
Pass previous messages through unchanged whenever possible. Even small edits to earlier content can break the prefix match and reduce reuse.
Monitor token usage and cache hits
Record token consumption and cache usage for each call through your logging system. Combine those records with monitoring and alerts to track costs and identify changes in cache behavior.
Consider cache requirements when choosing a model
For self-hosted deployments, consider architectures that reduce KV storage requirements, such as GQA or MLA, and plan GPU memory capacity accordingly.
The following simplified example keeps the system prompt fixed and preserves the supplied history:
SYSTEM_PREFIX = "You are a professional technical documentation assistant. Follow these guidelines strictly: ..." # Keep unchangeddef build_messages(history, user_input):# Preserve history unchanged to keep the prefix stable and support cache reuse.return [{"role": "system", "content": SYSTEM_PREFIX}] + history + [{"role": "user", "content": user_input}]
A stable message structure supports prefix reuse when the serving system provides cross-request caching; the application code itself does not implement the cache.
7. Conclusion
Lower LLM API prices are not only a matter of commercial competition. Inference optimizations—particularly KV caching and effective prefix reuse—can change the underlying cost structure of serving models.
KV caching avoids recomputing historical token representations, while cross-request caching extends that benefit to repeated prompts and context. The trade-off is GPU memory, which makes cache-efficient architectures and memory management important parts of deployment.
For developers, understanding these mechanisms has practical value: stable prefixes, unchanged conversation history, and careful usage monitoring can improve cache reuse and help deliver more efficient AI applications at lower cost.
