Why Coding Scenarios See Cache Hit Rates Above 90%
This article explains why coding assistants often show KV cache hit rates above 95%, why that range is normal for incremental coding sessions, and what low hit rates actually indicate. It covers how hit rate is calculated, the physical limits imposed by cache size and GPU memory, and engineering practices such as tiered caching, consistent-hash routing, and monitoring that help sustain high hit rates.
In everyday use of AI coding assistants, many people notice something that looks suspicious: during continuous coding conversations, the cache hit rate often stays above 95%, which leads them to assume the metric is being measured incorrectly. The opposite is true. That range is reasonable for coding scenarios, and a persistently low hit rate is what actually deserves attention. This article explains the topic from three angles: principles, architecture, and engineering practice.
What the Cache Hit Rate Actually Measures
When a large language model processes input, it converts each segment of text into intermediate results for attention computation, commonly referred to as the KV cache, which is stored in the GPU memory of the inference node. When the prefix of a new request exactly matches a previous request, those intermediate results do not need to be recomputed and can be reused directly.
The hit rate can be approximated as:
hit rate ≈ reusable prefix tokens / total input tokens
Note that the denominator is the total input, not the number of words you newly typed in the current turn. This is the key to understanding a value above 90%.
Why Coding Scenarios Naturally Produce High Hit Rates
Interactions between developers and an assistant have three stable characteristics:
- The same problem is advanced within the same session, so the historical context is continuous;
- Every request carries the full conversation history and the code generated so far;
- What is genuinely new is often only a modification instruction of a few dozen to a few hundred words.
Consider a simple model: 12,000 tokens of prior context and 200 new tokens in the current turn yield a theoretical hit rate of about 12000 / 12200 ≈ 98.4%. High hit rates therefore come from the shape of the interaction, not from statistical tricks. Conversely, if you frequently start new sessions and paste large blocks of new code each time, the hit rate naturally drops below 70%, which is a difference in usage patterns.
Two Physical Bottlenecks That Limit the Hit Rate
1. Cache Size and GPU Memory Capacity
With standard attention mechanisms, the KV cache grows linearly with sequence length and consumes a considerable amount of space under long contexts. GPU memory capacity is limited, so once the cache grows a little too large it gets evicted by new requests, producing a “built and immediately invalidated” effect.
A common mitigation in the industry is to compress the KV representation, for example by compressing along the sequence dimension or using shared or grouped attention, reducing the cache footprint for the same context to a small fraction. A smaller footprint brings two chained benefits: the same GPU memory can hold more session prefixes, and the cache has a chance to be offloaded to cheaper storage tiers for long-term retention.
2. A Single Cache Layer Is Just Temporary Storage
If the cache exists only in GPU memory, then higher concurrency, a few minutes of session inactivity, or switching conversations will clear the old prefix, leaving the next turn to recompute everything from scratch. This explains the common experience of “coming back half an hour later and finding it slower.”
Tiering and Persistence: The Engineering Prerequisite for Stable Hits
A more mature approach uses a three-tier cache:
| Tier | Location | Role |
|---|---|---|
| L1 | GPU memory | Current hot session, fastest reads |
| L2 | Host memory | Inactive sessions, can be brought back quickly |
| L3 | Disk/object storage | Persistent stable prefixes, reused across sessions |
This is paired with consistent-hash routing, which pins the same session to the same node and prevents prefixes from being scattered at random:
route_key = hash(session_id) -> node_id
On the container platform and logging service side, hit rate, eviction rate, and origin-fetch latency also need to be monitored, so that “whether the cache is working” becomes an observable metric.
A Self-Check List for Developers
- If the hit rate is persistently below 80%, first check whether you are starting new sessions too often;
- If the first turn after an idle period is noticeably slower, it is usually a cold-start recomputation, which is normal behavior;
- Pay attention to the billing model: cache reads are typically charged at a lower unit price, so the hit rate directly determines cost;
- Actively reuse stable prefixes by keeping the system prompt and project conventions fixed at the start of the session.
Conclusion
A cache hit rate above 90% is not an anomaly but the normal result of incremental coding interactions combined with a tiered cache architecture. Understanding the denominator, distinguishing cold starts from invalidation, and relying on monitoring data are more valuable than doubting the number. The general-purpose caching and monitoring capabilities offered in Baidu AI Cloud’s related cloud products can also serve as a reference framework for building your own evaluation.
