Prefix Stability Engineering Behind a 99% Cache Hit Rate

    This article explains why a roughly 99% cache hit rate in large model inference is better understood as a runtime stability signal than as a billing metric. It covers what prefix caching actually reuses (KV state), why cache invalidation cascades from the first byte-level difference, and the common sources of instability in multi-turn, tool-calling agent runs. It then lays out practical engineering constraints for keeping request prefixes stable and shows how to use the hit rate as a runtime health indicator.

    1. The Real Problem Behind Two Numbers

    In the public pricing for certain large model services, the input side shows a clear two-tier difference: cached input costs roughly 0.10 CNY per million tokens, while uncached input costs roughly 3 CNY, about a 30x gap. Meanwhile, public benchmarks for some agent-oriented runtimes show cache hit rates holding steady near 99%.

    Put side by side, the intuitive conclusion is “a lot of money saved.” The cost math matters, but stopping at the bill misses the more valuable signal: a 99% hit rate is effectively a health report on runtime stability. The hit rate is high not because the pricing rules are friendly, but because the unchanged portion of each request is preserved verbatim as the request is assembled turn after turn.

    So the topic here is better framed not as “how to save money” but as a more fundamental engineering question: in a multi-turn agent run with tool calls, can the runtime guarantee that the request prefix stays byte-for-byte stable?

    2. What “Caching” Actually Caches in the LLM Context

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

    In large model inference, caching hasn’t disappeared; only the object has changed. What gets reused is not the previous answer text but the key-value state (KV state) the model leaves behind after processing an input prefix. System prompts, tool schema descriptions, project rules, and conversation history — as long as token order stays consistent, the next request can resume computing from the old prefix instead of recomputing the entire context from scratch.

    The key difference from traditional business caching is the hit condition:

    • Business caches usually have explicitly designed keys, with reads and writes actively controlled by application code;
    • Prefix cache hits depend on whether a contiguous token prefix starting from the very beginning of the request is exactly identical.

    This leads to a counterintuitive conclusion: even if two pieces of text “look nearly the same,” any insertion, deletion, or reordering anywhere in the prefix causes everything after it to lose reuse eligibility. Invalidation is not local — it cascades backward from the first point of difference through the entire remainder.

    3. Typical Sources of Instability in Agent Scenarios

    Take a common flow: the agent first reads project rules and tool descriptions, issues a retrieval, then hands the retrieval results back to the model. In the next request, that large leading block is actually unchanged; only a tool call and its result are appended at the end. Ideally the prefix is preserved as-is and the inference side reuses the existing state directly.

    But a runtime that isn’t careful will break this premise. High-frequency sources of instability:

    1. Unstable tool ordering. The tool set is emitted from a hash structure or concurrent traversal, so serialization order differs each turn.
    2. Dynamic timestamp injection. Inserting “current time” into the system prompt makes the prefix change every turn.
    3. Old messages rewritten in place. Context compaction, summary backfill, or correction rewrites modify the history rather than only appending.
    4. Inconsistent serialization. The same data uses different field order, whitespace, or escaping across turns.
    5. Model or parameter boundary drift. Switching model versions, sampling parameters, or inference configuration mid-request breaks the correspondence between prefix semantics and state.

    Schematically, the difference between stable and unstable looks like this:

    1. # Stable: prefix unchanged, append at the tail only
    2. [system+tools+rules] + [turn1] + [tool_call_1] + [tool_result_1] + [turn2]
    3. # Unstable: the prefix interior is rewritten
    4. [system+tools+rules+current_time] + [turn1(compacted and rewritten)] + ...

    4. Governing the Request Assembly Chain as a Whole

    Prefix stability cannot be achieved through single-point optimization; it requires bringing every component on the request assembly chain under a unified constraint. This typically includes system prompt rendering, tool definitions and ordering, session state management, context compaction strategy, runtime internal state, and boundary handling with the service provider.

    Actionable engineering constraints:

    • Layered context: strictly separate the stable layer (system prompt, tool schema, project rules) from the volatile layer (current-turn input, tool results), and always append volatile content at the tail.
    • Deterministic serialization: use fixed ordering and fixed encoding for tool definitions and historical messages; never depend on traversal order.
    • Time and environment info placed last: when dynamic information is needed, put it at the request tail or inside the user turn, never in the stable prefix.
    • Compaction appends, never rewrites: prefer adding a new summary entry over rewriting existing history.
    • Boundary consistency: keep the model and key parameters unchanged across the whole chain, avoiding mid-run drift.

    5. Treating the Hit Rate as a Runtime Health Metric

    Once the request assembly chain is constrained, the cache hit rate turns from a financial metric into a technical one. Without intruding on business logic, it reflects whether the runtime is stable:

    • A sudden drop in hit rate often corresponds to a release that introduced a dynamic prefix;
    • Hit rate fluctuating abnormally with session length may mean the compaction strategy is rewriting history;
    • A clear hit rate gap between different chains usually points to inconsistent serialization implementations.

    In practice, log the prefix fingerprint and hit status for each request in your logging and alerting system, and set trend alerts. Combined with distributed tracing, you can quickly pinpoint which assembly step introduced the change. This approach needs no extra business instrumentation yet covers a substantial number of stability regressions.

    6. Conclusion

    The 30x price gap shows cost sensitivity, and the 99% hit rate shows engineering feasibility, but the bridge between them is the harness layer’s ability to guard the prefix. For developers, the real takeaway is this: prefix stability is not an optimization trick but a foundational contract of the agent runtime. Make it solid, and lower cost follows naturally — while improved runtime determinism is the long-term payoff.