Cache Hits First: How Prompt Caching Determines Agent Performance

    This article explains why prompt caching is the most important engineering lever for LLM agents, how KV Cache and prefix-based reuse work, the common practices that silently destroy cache hit rates, and the design principles needed to keep hit rates high. It also covers how to measure cache performance and quantify the cost savings of effective caching.

    Cache Hits First: How Prompt Caching Determines Agent Performance

    Why Cache Hit Rate Is the First Dividing Line for an Agent Harness

    As agents move from proof of concept to production, engineering details beyond raw model capability increasingly determine success. In a typical agent run, a single session sends hundreds or even thousands of requests to the model: after every tool call, the model must “re-read” the system prompt, tool definitions, project context, and conversation history. If every request recomputes all of that from scratch, both cost and latency quickly spiral out of control.

    Prompt caching solves this problem by reusing intermediate results that have already been computed, so each request only incrementally computes new content. Publicly reported data from well-designed agent frameworks shows cache hit rates above 99% on long tasks, with cached input tokens typically priced at a fraction of uncached tokens—often a few tenths or less. In other words, for the same task, the difference between good and bad caching can change the bill by one to two orders of magnitude.

    So if someone asks, “Why spend effort studying caching?” the answer is simple: it directly saves money and makes much longer contexts viable.

    Understanding KV Cache from the Model’s Inference Flow

    Processing a large language model request roughly breaks into two phases:

    1. Prefill: The model reads the system prompt, tool definitions, project documentation, conversation history, and the user’s new question, computing attention states for all input tokens in parallel.
    2. Decode: The model generates the answer token by token, based on the context it has understood.

    In the attention computation of every Transformer layer, tokens that have already been read produce corresponding Key and Value vectors. These intermediate states, once saved, form the KV Cache. When generating the next token, the model does not need to reprocess the preceding text; it only uses the new token’s Query against the cached Keys and Values.

    One common misconception needs clarification: the KV Cache is not “the previous answer text,” nor is it a document that can be opened and read. It is more like the computational notes the model leaves behind after reading the material—unreadable to humans, but highly reusable for subsequent computation. As long as the preceding text has not changed, those notes can be reused as-is, skipping the entire prefill computation.

    Prefix: The Core Concept Behind Cache Reuse

    Understanding caching starts with understanding the prefix. A request an agent sends to the model is typically assembled in the following order:

    1. [system prompt][tool definitions][project documentation][conversation history][new message this turn]
    2. <------ reusable common prefix ------> <-- new suffix -->

    The definition of a prefix is strict: a contiguous segment, starting from the very first token, that is identical. It is not about keyword overlap or semantic similarity. For example:

    • If the previous request was A+B+C and the current request is A+B+C+D, then as long as A+B+C is unchanged, the server can potentially reuse the prefix through C and only compute the new D.
    • If a tool definition in the middle of B is modified, then everything from that change point onward—B, C, and D—must be recomputed, even though C and D themselves did not change.

    That is what prompt caching really is: a game of matching tokens one by one from the very beginning. The more stable the opening, the longer the reusable prefix; the earlier a change appears, the more must be recomputed.

    Common Engineering Issues That Erode Hit Rates

    In real agent systems, hit rates are quietly eroded by the following problems:

    1. Dynamic content inserted at the front: If the system prompt starts with a timestamp, a random ID, or per-request user information, the very first token already differs, and almost the entire request must be recomputed.
    2. Frequently changing tool definitions: Unstable tool ordering or tool descriptions containing varying fields invalidates everything from the tool definitions onward.
    3. Rewriting or reordering history: In multi-turn conversations, summarizing history, truncating at shifting positions, or rearranging messages breaks prefix continuity.
    4. Unnecessary repetition of content: Re-assembling large chunks of static documentation into every turn, while a small earlier change prevents a cache hit, wastes the opportunity entirely.

    Design Principles for Improving Cache Hit Rate

    1. Strictly Layer Context by Stability

    Order context from most stable to least stable: system prompt → tool definitions → project documentation → conversation history → new message this turn. Put all unchanging content at the front and push volatile content toward the end. When dynamic information is needed, place it at the end of the message or have the model fetch it on demand through a tool, rather than embedding it in the system prompt.

    2. Keep the System Prompt Byte-Stable

    The system prompt is the starting point of the prefix; a single bit of change invalidates everything after it. To ensure stability:

    • Do not append current time, random numbers, session IDs, or other per-request values;
    • Manage it with versioning, keeping content completely fixed within a version;
    • If personalization is required, inject it via user messages or tool calls instead of rewriting the system prompt.

    3. Keep Tool Definitions Deterministic

    • Keep the tool list ordering fixed;
    • Keep tool names, descriptions, and parameter schemas unchanged throughout the session;
    • When tools are added mid-session, append new definitions after existing ones rather than interleaving them, to avoid disturbing the preceding order.

    4. Handle History Compression and Truncation Carefully

    When history grows too long and must be compressed, pay attention to when and how truncation happens. A practical approach: truncate once at a determined boundary, rather than trimming from the middle on every turn. If a summary is needed, place it as new system-level context before the history and keep it stable. Avoid the strategy of “regenerating the summary and replacing history every turn”—that is essentially resetting the prefix every turn.

    5. Maintain Session-to-Cache Affinity

    Ensure requests from the same session are routed to server-side infrastructure that supports their cache. When using a managed model service, choose models and interfaces that support prefix caching and follow the caching rules in the official documentation. When self-hosting inference, build on the KV Cache reuse capabilities of open-source inference frameworks or implement your own prefix-matching logic.

    6. Build a Measurement Loop Around Hit Rate

    Treat cache hit rate as one of the harness’s core metrics and monitor it continuously:

    • Record input tokens, cached tokens, and uncached tokens for every request;
    • Break down statistics by session, task type, and model version;
    • Run before/after comparisons whenever prompt structure changes to verify that the change has not broken the prefix.

    A simple log structure might look like:

    1. {
    2. "session_id": "...",
    3. "input_tokens": 52340,
    4. "cached_tokens": 51980,
    5. "cache_hit_rate": 0.9931,
    6. "prompt_fingerprint": "sha256(...)"
    7. }

    Here, prompt_fingerprint helps quickly locate which layer’s content changed.

    A Quantified Cost Perspective

    Suppose a model service charges cached input tokens at 1/10 of the standard input price (actual ratios vary by provider; refer to the applicable pricing). Consider an agent task that accumulates 1 billion input tokens:

    • Fully uncached: cost is 1.0× at standard price.
    • 99% hit rate: 99% of tokens are billed at 1/10, yielding a total of 0.99×0.1 + 0.01×1.0 ≈ 0.109×—less than one-ninth of the original.

    This simple model explains why cache hit rate is one of the highest-leverage factors in an agent’s cost structure. The reduced prefill computation also directly shortens time-to-first-token, which benefits the interactive experience.

    Conclusion

    A good harness does not start with flashy agent orchestration; it starts with getting the fundamentals right. The three keywords for cache hits are: a stable prefix, deterministic assembly order, and continuous measurement. First make the first few tokens of every request identical to the previous one—then talk about complex multi-agent collaboration. That is the first step, and the most cost-effective one, toward taking an agent from “able to run” to “able to run at scale.”