Prefix Cache Hit Rate as a First-Class Design Goal for LLM Agent Harnesses

    Agent inference cost often hinges less on the model than on how the harness structures requests for prefix caching. This article explains prefix KV reuse, the request-layout mistakes that break cache continuity, harness practices that keep prefixes stable, and why hit rate should be a primary observability metric.

    1. An underrated cost lever: start with the numbers

    In a public measurement, a team running multi-turn LLM tasks on a self-built agent execution framework (harness) recorded about 947 million prefix-cache hit tokens, with a hit rate of 99.93%. Under the common cloud billing model that prices cache hits and misses separately, those tokens would have cost about $2.65 at the hit price versus about $132.62 if billed entirely as misses—a 50× difference.

    The implication is easy to miss: in agent applications, inference cost is often driven less by the model itself than by how the calling framework designs request structure. Same model, same workload: hit-rate differences can move the bill by an order of magnitude. A good harness should therefore get prefix-cache hits right first.

    2. How prefix cache hits work: KV reuse

    Mainstream LLM inference services implement prefix caching:

    1. During Transformer inference, each input token produces corresponding KV (key-value) cache tensors used in later attention.
    2. If two requests share an identical prefix, the KV cache for that prefix can be reused instead of recomputed.
    3. Servers typically bill hit-prefix tokens at a much lower rate (often a fraction of the miss price) because reuse avoids a large amount of prefill compute.

    The hard constraint is “identical prefix”: a single differing token invalidates the cache from that position onward. Cache optimization is therefore a request-orchestration problem, not a model-tuning problem.

    3. What actually breaks hit rate

    A typical agent request is: system prompt → tool definitions → history → current input. These factors break prefix continuity:

    1. Dynamic content in the system prompt

    A common anti-pattern is injecting timestamps, random IDs, or live state into the system prompt. Example:

    1. # Anti-pattern: the time changes every request; the prefix fails from this line onward
    2. You are an ops assistant. Current time: 2025-06-01 10:23:45.123

    One character change in the timestamp invalidates every subsequent token. Put time-like data later in the messages, or let the model fetch it on demand via a tool call.

    2. Unstable tool-definition order

    If the framework lexicographically sorts tools, appends them by registration time, or trims them based on routing, the tool-definition sequence drifts. Tool schemas often span thousands of tokens; drift is expensive. Keep tool list content and order fully fixed within a session.

    3. Inserting or rewriting history

    Agent loops often insert system summaries into the middle of history, reformat old messages, or interleave messages from parallel branches. Any mid-history rewrite invalidates cache after that point. Follow append-only: add new content only at the tail; keep history byte-identical.

    4. Sampling parameters and serialization differences

    Some servers include sampling parameters in the cache key or are sensitive to message serialization. Unify serialization so the same content is not treated as a different prefix because of whitespace, escaping, or field order.

    4. Harness-level engineering practices

    A cache-friendly harness should implement:

    1. Layered request assembly

    Split the request into stable vs. volatile layers:

    1. [Stable] static system prompt + tool definitions (fixed order)
    2. [Semi-stable] history (append-only)
    3. [Volatile] current user input, timestamps, retrieval results, dynamic state

    Always put volatile content last so the longest common prefix is as long as possible.

    2. Shared prefixes across sessions

    In batch workloads, reuse the same system prompt and tool definitions across concurrent sessions so that high-frequency prefix stays hot in cache and amortizes cost.

    3. Session state outside the prompt

    Move session IDs, counters, and temporaries out of the message body into harness memory or a database; write only the minimum needed context at the tail of the request.

    4. Placement of long static context

    For large static blobs (long documents, codebases), pin them early (the earlier the better) and put volatile instructions and questions after them so the static block can be reused across requests.

    5. Treat hit rate as a first-class metric

    Optimization needs measurement. The harness should monitor:

    • Prefix cache hit rate: hit tokens / total input tokens
    • Per-request prefix failure: which request types keep breaking the cache
    • Cost accounting: hit vs. miss prices separately, so optimization ROI is visible

    When integrating LLM services—self-hosted clusters or cloud inference, including capabilities such as those from Baidu AI Cloud—hit data can usually be read from usage fields in the response or from logs. Build a cache-health dashboard and alert on sudden hit-rate drops so regressions are easy to find.

    6. Closing

    Raising prefix-cache hit rate does not require changing the model or adding hardware. It is request-structure engineering: keep prefixes stable, put dynamic content last, append history only, and measure continuously. For token-heavy agent apps, moving hit rate from tens of percent to 99%+ can mean tens of times lower cost and better latency. That is the point: a good harness gets cache hits right first.