Prompt Cache Hit Rate as a Runtime Stability Signal

    This article explains why prompt caching hit rate is best understood as a byproduct of runtime stability rather than a standalone cost metric. It covers how prefix KV state reuse differs from traditional caching, why agent tool calls can break a shared prefix, six common points in request assembly that introduce non-determinism, and how to structure context into stable, semi-stable, and volatile layers. It also shows how to track hit rate, reusable prefix length, and assembly drift as observability signals.

    1. Reading Cache Economics Through the Price Gap: Who Pays for Recomputed Context

    In the billing structure of some large model services, the input side often has two price tiers: a very low price per million tokens for cache hits, and a much higher price for cache misses. The two can differ by an order of magnitude or more. At the same time, public evaluations of agent-oriented scenarios show that cache hit rates can stay consistently high. Put these two sets of numbers together, and the most intuitive conclusion is that a lot of money is being saved.

    The bill is worth calculating, of course, but stopping at the billing layer misses the more important question: at each step an agent takes, can the runtime preserve the unchanged portion of the request exactly as it was? Hit rate is not a number that can be optimized in isolation. It is a byproduct of whether the entire request assembly pipeline is stable.

    Engineers who work on architecture are familiar with caching: in-process local caches, consistent hashing and in-memory databases 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 transferred can be done one fewer time. In large model inference, caching has not disappeared; only the object being reused has changed.

    2. What Gets Reused Is Not the Answer, but the Prefix KV State

    A traditional business cache reuses a complete result, with a key explicitly designed by the business. Prompt caching instead reuses the key-value state (KV State) left behind after the model processes an input prefix. System prompts, tool structure descriptions, project rules, and conversation history all qualify: as long as the token sequence counted from the beginning of the request stays identical, the next turn has a chance to continue computing from the old prefix rather than starting over.

    This produces two characteristics that differ sharply from traditional caching:

    • Whether a hit occurs is not determined by a business key, but by the longest common token prefix measured from the start.
    • The prefix is position-sensitive. Anything inserted near the front causes everything after it to lose its reuse opportunity, even if that content itself has not changed by a single character.

    As a result, a timestamp concatenated into the system prompt, a reordered tool definition list, or a re-serialized history message can turn two requests that look nearly identical to the human eye into two different prefixes from the cache’s point of view.

    3. Agent Scenarios: How One Tool Call Can Destroy an Entire Prefix

    Consider a typical loop: the runtime first injects project rules and tool descriptions, the model decides to make a retrieval call, the tool result is filled back in, and the next round of inference begins.

    Ideally, the front portion of the next request is identical to the previous one, with only the tool call record and result appended at the end:

    1. Turn N : [system prompt][tool definitions][history messages ...][current user input]
    2. Turn N+1 : [system prompt][tool definitions][history messages ...][current user input][tool call][tool result]

    As long as [system prompt][tool definitions][history messages ...][current user input] is preserved token for token, the prefix can be reused. Conversely, if the runtime regenerates the tool list every turn, writes the current time into the system prompt, or casually trims and rewrites old messages, the prefix breaks in the middle and everything after it is recomputed.

    This also explains why the cost of long-context agents is often far higher than expected: it is not that a single request became more expensive, but that every round pays again for the same context.

    4. Breaking Down the Request Assembly Pipeline: Six Common Prefix-Breaking Points

    Tracing the request path from top to bottom reveals six common sources of breakage.

    1. System prompt. Placing dynamic variables (current time, random identifiers, user nickname, debug information) at the front of the prompt is the most common and most hidden problem. The principle is to push dynamic content as far back as possible and keep static constraints at the front.
    2. Tool and structure definitions. If tool ordering, description text, and field order come from hash traversal or concurrent assembly, the order will drift. Sort explicitly and fix the serialization format.
    3. Session and message serialization. Re-encoding the same history with different whitespace, line breaks, escaping, or multimodal placeholders is equivalent to rewriting the prefix.
    4. Context compression and summarization. Compression itself is necessary, but it should be done as “compress once, then freeze the result,” rather than re-summarizing old content every turn, which keeps the prefix in constant flux.
    5. Runtime state. If retries, concurrent branches, or multi-path sampling each maintain their own prompt assembly logic, prefixes that should be shared will fork.
    6. Provider boundaries. Caches are not shared across different models, different versions, or different service endpoints; switching means a cold start.

    A practical self-check is to hash the “complete token sequence about to be sent” before the request goes out, compare it with the previous turn, and output the longest common prefix length. If that length is noticeably shorter than expected, there is a non-deterministic factor in the assembly pipeline.

    1. def prefix_len(prev_tokens, cur_tokens):
    2. n = min(len(prev_tokens), len(cur_tokens))
    3. i = 0
    4. while i < n and prev_tokens[i] == cur_tokens[i]:
    5. i += 1
    6. return i

    5. Context Layering: Keep the Stable Prefix Fixed, Append New Content at the End

    The more robust engineering approach is to divide context explicitly into three layers:

    • Stable layer: system constraints, output specifications, tool contracts, which barely change across sessions.
    • Semi-stable layer: project rules, long-term memory, retrieved reference material, which change infrequently and should be replaced as a whole when they change rather than edited locally.
    • Volatile layer: current user input, tool calls and results, temporary variables, which may only be appended at the end.

    Three accompanying rules: do not insert dynamic values at the front; do not perform in-place edits, only appends; and treat a newly produced summary from compression as a whole replacement for the semi-stable layer, re-establishing the prefix baseline after the replacement.

    6. Treating Hit Rate as an Observability Metric for Runtime Stability

    The value of hit rate goes beyond cost. It also imposes a requirement on the determinism of the runtime: the same input state should produce a request that is token-for-token identical. It is therefore worth including it in the observability system, alongside latency and error rate:

    • Metrics: prefix hit rate, average reusable prefix length, and extra input tokens caused by assembly drift.
    • Logs: record the prompt version number, tool list hash, and compression batch number for each turn’s request.
    • Alerts: a sustained decline in hit rate under scenarios where no change was expected usually means some change introduced non-deterministic assembly.

    7. Conclusion

    The price gap between cache hits and misses ultimately points to a plain engineering requirement: make the parts that do not change actually stay unchanged. When the system prompt is clearly layered, tool order is fixed, history is only appended and never rewritten, compression results are frozen, and runtime state is isolated, a high hit rate appears naturally. Conversely, the hit rate curve is the most direct evidence of whether these constraints are being honored. Writing prefix stability into design specifications and release checklists turns cost optimization and runtime governance into the same task.