When Cache Hit Rate Reaches 99%: How Agent Runtimes Preserve Request Prefixes

    This article explains why a high cache hit rate is better treated as a runtime health indicator than a cost metric in LLM applications. It covers how prefix caching reuses intermediate model state rather than previous answers, why tool reordering, dynamic system prompt injection, history rewriting, and inconsistent serialization break prefixes, and how teams can enforce deterministic request assembly, layered observability, and regression checks to keep unchanged content byte-for-byte stable across turns.

    1. Reading the Problem Through Price Gaps: High Hit Rates Test Runtime Stability

    In large model invocation scenarios, input-side pricing is often clearly tiered: the unit price of cache-hit input and the unit price of cache-miss input can differ by an order of magnitude, and public tests have even shown ratios approaching full hits. Put those two numbers side by side and the most immediate association is cost savings.

    But cost is only the outcome. Follow the request assembly path downward and the variable that truly determines the hit rate is whether the runtime can preserve the unchanged parts of each step byte for byte. If every round reorders tools, rewrites history, or inserts dynamic content, even the best-designed context tiering cannot hold the prefix.

    For this reason, a high hit rate is better treated as a runtime health indicator: it tests in reverse whether system prompt management, tool ordering strategy, session storage, context compaction, and runtime state management are stable.

    2. The Cached Object Has Changed: From “Computed Answers” to “Computed State”

    Developers are familiar with traditional cache tiering: in-process local caches, distributed caches between services, and content delivery networks in front of static assets. They all solve the same class of problem — whether content that has already been computed, fetched, or transmitted can be done one fewer time.

    In large model inference, caching has not disappeared; only the reuse target has changed. What the server reuses is not the previous answer but the intermediate state left after the model processes an input prefix (commonly understood as key-value state). As long as the system prompt, tool structure descriptions, project rules, and conversation history keep the same token order, the next request has a chance to continue computing from the old prefix.

    The key difference from business caching lies in how it is addressed:

    • Business caches usually have explicitly designed keys agreed upon by both readers and writers;
    • Whether a prefix cache hits depends on whether a token prefix counted from the beginning of the request is exactly identical.

    This means a hit is not decided by any single field but by the overall serialized bytes and token sequence. Move one tool, insert a current timestamp at the front, or re-serialize old messages, and content that looks unchanged afterward may lose its reuse opportunity as well.

    3. A Typical Scenario: How One Tool Call Affects the Prefix

    Picture a common agent execution fragment: the runtime first loads project rules and tool descriptions, the model invokes a retrieval capability once, and the result is handed back to the model to continue reasoning.

    In the next request, that large block at the front is actually unchanged: the model identifier, system prompt, tool definitions, and existing conversation are all still there, with only a tool call and its result appended at the end. As long as this prefix is preserved as is, the server has a chance to directly reuse the already computed intermediate state.

    Conversely, if the runtime does any of the following at this step, the prefix may break:

    1. Reordering or re-serializing tool definitions;
    2. Injecting the current time, a random identifier, or a request ID into the system prompt;
    3. Going back to rewrite earlier messages, or applying “seemingly equivalent” format normalization to them;
    4. Using different field ordering and escaping strategies when restoring a session.

    Two requests that look nearly identical to the human eye may already be two different prefixes from the cache’s perspective.

    4. A Checklist of Invalidation Triggers: Finding the “Invisible Rewrites”

    In engineering practice, prefix invalidation often comes from “well-intentioned normalization.” It is worth focusing on the following places:

    • System prompt assembly: whether dynamic timestamps, environment information, or canary flags are placed at the very front;
    • Tool description generation: whether an unordered collection is traversed, causing order to drift across processes;
    • Session persistence: whether serialization format, field order, and null handling stay consistent from round to round;
    • Context compaction: whether summaries are inserted in the middle of history rather than appended at the end;
    • Multi-turn retries: whether the entire context is rebuilt after a failure instead of continuing from the original prefix;
    • Mixing models or parameters: switching models or sampling parameters within the same session, making the prefix unreusable.

    Illustrative pseudocode showing the assembly principle of “stable prefix plus tail append”:

    1. request = stable_prefix # model identifier + system rules + tool definitions, fixed order
    2. + compacted_history # append only, never rewrite existing entries
    3. + current_turn_tool_result # new content for this turn goes at the end
    4. + dynamic_context # volatile information such as time should be placed later or removed

    The principle is not complicated: the earlier the position, the more stable it must be; the more volatile the content, the later it should go.

    5. Engineering Governance: Making the Prefix an Observable Object

    Turning the hit rate from a “billing number” into an operable metric requires three things.

    First, build deterministic assembly. Fix field order, fix the serialization implementation, and sort collections before output; remove dynamic information from the head or move it later. Consistency checks can be added: compute a digest fingerprint of the stable prefix and compare it across rounds to see whether it has changed.

    Second, establish layered observability. Record each round’s prefix length, fingerprint, whether reordering occurred, and where compaction happened, then correlate that with hit results. When the hit rate drops, locate the round where the fingerprint changed first, rather than looking at the bill.

    Third, bring the hit rate into regression validation. Use a fixed script to run a set of representative sessions and assert that the stable prefix fingerprint stays unchanged and that appends happen only at the tail. This way cache problems surface before release instead of appearing in production as a cost.

    6. Conclusion: Cost Is the Surface, Determinism Is the Capability

    An order-of-magnitude price gap on the input side is certainly worth careful accounting; but what deserves more investment is runtime determinism. Prefix design belongs to context tiering, cost optimization belongs to resource governance, and whether the runtime can preserve unchanged parts as is every round is the key link that connects the two.

    When a team can prove with fingerprints, observability, and regression tests that “the unchanged parts were never rewritten,” a high cache hit rate is no longer occasional luck but a natural result of architectural stability.