Is a 96%–99% Code Generation Cache Hit Rate Normal?

    This article explains how to interpret a 96%–99% cache hit rate in AI coding tools, code completion, and code generation systems. It covers the three common ways to define a cache hit rate, the conditions that make such high values plausible, warning signs of inflated metrics, how to verify results through logging and controlled comparisons, and engineering practices for stable prompt prefixes and cache keys.

    1. Confirm the Metric Definition First

    A cache hit rate can be defined in at least three common ways:

    1. Token-level hit rate: cache-hit input tokens divided by total input tokens. For example, when stable prefixes such as system prompts, project conventions, file trees, and dependency summaries are reused, while the code near the current cursor changes, the token-level hit rate can be high.

    2. Request-level hit rate: requests that hit the cache at least once divided by total requests. This value is usually lower than the token-level rate, because a request that contains dynamic content can still partially hit the cache.

    3. Full-hit request ratio: requests whose entire prefix hits the cache divided by total requests. This is the strictest definition. Reaching 96%–99% under this definition usually requires highly templated requests or highly continuous sessions.

    A “96% hit rate” therefore means something different under each definition. If the metric is token-level and the denominator includes only cacheable tokens, a 96%–99% value can be explained. If the claim is a request-level full-hit rate of 96%–99%, the claim needs a stricter audit.

    An illustrative calculation looks like this:

    1. cache_hit_tokens = number of input tokens served from cache
    2. cache_miss_tokens = number of input tokens not served from cache
    3. prompt_tokens = cache_hit_tokens + cache_miss_tokens
    4. token_hit_ratio = cache_hit_tokens / prompt_tokens
    5. request_hit_ratio = requests with at least one cache hit / total requests
    6. full_hit_ratio = fully cached requests / total requests

    If the metric combines local caching, tool-side deduplication, session reuse, and inference-side prefix caching, the reported percentage can rise further. The first step is not to judge whether the number is high or low. The first step is to confirm the numerator, the denominator, and the sampling scope.

    2. When Is 96%–99% Reasonable?

    In code generation scenarios, a high cache hit rate usually depends on several conditions:

    • Long stable prefixes: System prompts, coding standards, project structure, and interface conventions stay unchanged, and dynamic content is placed at the end of the request.
    • High session stickiness: The same developer, project, or session sends consecutive requests, so earlier context prefixes can be reused.
    • Stable context organization: Each request does not reorder file snippets arbitrarily or insert random IDs, timestamps, or unordered retrieval results.
    • Sound cache key design: Cache keys isolate entries by tenant, project, model version, and prompt template version, which avoids frequent invalidation.
    • Local changes in multi-turn interactions: Code completion and continuous editing are highly local, so little new content is added and most of the earlier prefix is reused.
    • Prefix caching or context reuse on the inference side: Repeated prefixes are cached, which reduces repeated computation.

    Under these conditions, a token-level hit rate above 90% is not unusual. If stable prefixes make up most of the input and the dynamic tail is short, a 96%–99% rate can occur. A high hit rate does not automatically mean high quality or low cost, though. It only shows that repeated prefixes are being reused often.

    3. Signs That the Metric May Be Inflated

    When you see a 96%–99% rate, check for these warning signs:

    • The denominator counts only “cacheable tokens,” which excludes many uncached requests.
    • Local session caching, tool-side deduplication, and model-side caching are counted more than once.
    • Only successful requests are counted, and failed or timed-out requests are excluded.
    • The sample is concentrated in a few long sessions or templated requests and does not represent overall traffic.
    • The hit rate shows no clear correlation with time-to-first-token latency or input cost.
    • Cache keys are too coarse, which creates a risk of data leaking across projects or tenants.
    • Old cache entries are not invalidated promptly after code changes, which leads to stale results.

    If the hit rate is high but latency, cost, and quality do not improve, or if the rate varies widely across scenarios, review the raw logs.

    4. How to Verify and Observe the Metric

    Log at least the following fields for each request: prompt_tokens, cache_hit_tokens, cache_miss_tokens, cacheable_tokens, request_id, session_id, project_id, model_version, prompt_template_version, cache_ttl, time-to-first-token latency, and total latency. Then calculate several metrics grouped by scenario, project, and session length. Look at the distribution rather than only the average.

    You can also run a small controlled comparison. Send the same batch of requests with caching enabled and disabled, and compare the hit rate, P95 latency, input cost, and output quality. Sample cache keys to confirm they include tenant and version information, which prevents reuse across users. If the token-level hit rate is high but the request-level hit rate is low, the workload relies on long prefix reuse. If the full-hit request ratio is also very high, check whether the requests are overly templated.

    5. Engineering Best Practices

    1. Separate stable prefixes from dynamic tails: Place system prompts, standards, and file trees at the start. Place the current editing content at the end.
    2. Version prompt templates: When a template changes, invalidate old cache entries to prevent contamination.
    3. Avoid random noise: Do not insert timestamps, random IDs, or unordered retrieval snippets into stable prefixes.
    4. Isolate cache keys: Include tenant, project, model version, template version, and permission scope in each key.
    5. Set reasonable TTLs and invalidation rules: Invalidate entries promptly after code or permission changes.
    6. Observe metrics together: Review hit rate, latency, cost, error rate, and output quality together.
    7. Segment statistics by scenario: Measure completion, generation, question answering, and refactoring separately so averages do not hide long-tail behavior.

    Conclusion

    A 96%–99% cache hit rate in code generation is not inherently abnormal. If the metric is token-level, stable prefixes make up a large share of input, session stickiness is strong, and the inference side uses prefix caching and context reuse, the value can be explained. If the claim is a request-level full-hit rate, or if the hit rate does not match latency, cost, or quality, audit the denominator, the sampling method, and the cache key design. Judging whether the number is normal depends less on the percentage itself than on confirming the definition, the conditions, and the verification process behind it.