KV Cache Hit Rate Above 95% in LLM Coding Sessions: Is It Normal?
This article explains what KV cache hit rate measures and why LLM coding sessions often reach 96% to 99%. It covers the factors that lower hit rates, including memory limits and unstable prompt prefixes, the architecture and tiered-storage approaches used to improve caching, and practical steps developers can take to keep hit rates high.
Developers who use large language models for coding often notice a metric that is easy to overlook: the cache hit rate. Some find that a single session reaches 96% or even 99%, and their first reaction is to wonder whether something is wrong. In fact, this is normal. For typical programming workflows, a high hit rate reflects a healthy state. The sections below explain why, starting from first principles.
1. What KV Cache and Cache Hit Rate Mean
When a model processes input text, it does not simply read the text once. Each piece of input goes through intermediate computations in the attention mechanism. These computations produce context data that later inference steps can reuse. This data is called the KV cache (Key-Value Cache), and it is stored in GPU memory on the server.
If a new request shares a large part of its input with a previous request, meaning the prefixes match, the server does not need to recompute that shared part. It can reuse the stored KV data directly. The cache hit rate is the share of a request’s input tokens that can be served directly from the cache without recomputation.
The mechanism affects both cost and latency:
- Cost: Cached input tokens are usually billed at a much lower rate than regular input tokens. A higher hit rate lowers the bill.
- Latency: Skipping prefix computation shortens the time to first token, which makes interaction feel more responsive.
2. Why Coding Is a Natural High-Hit-Rate Scenario
Once you understand the mechanism, it is clear why coding sessions tend to have high hit rates. A typical development workflow looks like this:
- You work on one problem within a single session and iterate turn by turn.
- Each new request resubmits the full conversation history and all the code generated so far as context.
- The truly new content is often just the latest instruction, usually tens to a few hundred tokens.
Consider an example. Suppose a session has accumulated 20,000 tokens of context. In the next turn, you add one instruction, “change this loop to async processing,” which is about 50 tokens. In that case, about 99.75% of the request matches the previous turn exactly. That is the theoretical ceiling. As long as the cache works correctly, a hit rate above 95% is expected.
In other words, a coding session has a structure of a very long, stable prefix plus a very small incremental instruction. That structure explains the high hit rate. It is not an anomaly.
3. Why Hit Rates Often Fall Short
If the theoretical ceiling is so high, why do real-world hit rates often sit at 80% to 90%, or even lower? The common causes include the following.
1. The KV cache is too large for available GPU memory
In conventional architectures, KV cache size grows linearly with context length, so long sessions occupy a lot of GPU memory. Memory is limited. When concurrency is high, new requests can push out the cache of older sessions, so the cache is evicted before it can be reused.
2. The cache lives only in GPU memory, which is temporary
Many systems keep the cache only in GPU memory and have no more durable storage layer. Under heavy load, a session that sits idle for a few minutes, or a switch to a new conversation, can cause the old cache to be cleared. The next turn then has to recompute everything.
3. The prefix is unstable
If the system prompt includes a timestamp or a random value, or if the conversation history is truncated or reordered, the prefix changes. Everything after the change point loses its cache. This is one of the most common mistakes on the application side.
4. Industry Approaches to Improving Caching
Industry efforts to address these problems generally follow two paths.
Path 1: Compress the KV cache at the model architecture level. By improving the attention mechanism, KV data can be compressed along the sequence dimension. For long contexts, this can reduce the KV cache to a very small fraction of its size in conventional models. Smaller caches let the same GPU memory hold dozens of times more cached content, so the cache for an active session is less likely to be evicted. Small caches are also practical to offload to host memory or even disk for longer-term storage.
Path 2: Build a tiered caching system. Borrowing from the classic storage hierarchy, a three-tier design works well:
- L1 (GPU memory): Holds hot caches for sessions that are actively in use, for the fastest access.
- L2 (host memory): Holds caches for inactive sessions. These do not occupy GPU memory and can be loaded back when needed.
- L3 (persistent storage): Writes stable prefix caches to disk, where they can remain valid for hours or even days.
In a coding workflow, this means that if you stop to look up documentation, attend a meeting, or change requirements, then return half an hour later to send the next instruction, the earlier code context is still cached. The cache is not cleared and rebuilt from scratch.
Major model service platforms in China are investing in both directions. For example, some use prefix caching and tiered storage to raise hit rates in long sessions. Developers who use AI coding tools or call APIs directly can review the context caching documentation provided by their platform.
5. Practical Recommendations for Developers
Platform capabilities matter, but application code can also help keep hit rates high:
- Keep the prefix stable. Keep the system prompt fixed where possible. Do not embed timestamps, random IDs, or other values that change between requests.
- Avoid unnecessary history rewrites. Do not summarize, truncate, or reorder existing conversation content. Any change invalidates the cache for everything after it.
- Prefer long sessions. Complete related tasks in the same session when possible, and avoid opening new conversations too often.
- Place dynamic content at the end. Fields that must change, such as the current time, should go at the end of the prompt, not the beginning.
- Monitor billing metrics. Check cache hit rates and cache billing details regularly, and use them as one indicator of service quality.
6. Conclusion
For a workflow with a long, stable prefix and small incremental edits, a cache hit rate of 96% to 99% is reasonable and should be expected. If your hit rate stays clearly below that range, and you have ruled out prefix instability in your own application, the cause is likely a limitation in the model or platform caching. Such limitations usually appear as higher inference costs and slower responses for the user. Understanding how caching works and how it can be optimized helps you read your bills and evaluate providers.
