How Prompt Caching Reduces Token Costs for LLM Applications

    Prompt caching lets inference providers reuse computed KV states for stable prompt prefixes, dramatically lowering input token costs—often to around one-tenth of the normal price. This article explains how prefix caching works, why cached reads are cheaper, the real-world constraints around memory and scheduling, and practical techniques to make AI Agent workloads cache-friendly. Learn how to structure prompts for maximum cache hit rates and significantly reduce costs for long-context Agent applications.

    Why Don’t Agent Bills Scale Linearly with Request Count?

    A single task completed by an AI coding Agent often involves dozens of model calls. Each call carries the full context: the system prompt, tool definitions, conversation history, and the current code snippets. Intuitively, more calls and longer contexts should mean proportionally higher token costs. Yet real-world bills are often far less dramatic than expected.

    This is usually not because the model provider is offering a “discount,” but because an underlying caching mechanism in the inference system is doing the heavy lifting. When pricing usage, providers track a portion of input tokens separately and bill them at a rate far below the standard input price. These tokens are called “cache reads” or cached tokens.

    To take full advantage of this mechanism, you need more than the discount numbers—you need to understand the principles, constraints, and engineering trade-offs behind it.

    What Is a Cache Hit?

    To understand cache hits, we need to revisit the basics of Transformer autoregressive generation.

    As the model processes each token, it generates corresponding Key/Value vectors and retains them for subsequent attention computations. For a long prompt the model has never seen, it must perform forward computation token by token—a phase commonly known as prefill. Prefill is compute-intensive: the longer the context, the more resources it consumes.

    If the next request’s prompt prefix is exactly identical to the previous one, the KV vectors for those prefix tokens will also be identical. The inference service can therefore save them and reuse them on the next request instead of recomputing. This is “prefix caching,” also commonly called Prompt Caching.

    Importantly, a cache hit does not mean “copy-pasting” text. It reuses the tensor state the model has already computed internally. Its validity depends on strict prefix consistency: starting from the first token of the input, any continuous identical segment can be reused; once a single token changes at some position, everything after that point must be recomputed.

    For example: if the system prompt contains the current timestamp, even a completely unchanged 100K-token conversation history following it may be invalidated because the timestamp changed. Conversely, if you place the timestamp at the very end of the message, the system prompt, tool definitions, and conversation history can all be cached as a stable prefix.

    Why Are Cached Reads So Cheap?

    Inference costs consist of two main parts: prefill and decode.

    Prefill processes the bulk of the input tokens and is typically compute-intensive. Decode generates output tokens one at a time and is more constrained by GPU memory bandwidth. Cache hits eliminate a large amount of prefill computation, significantly reducing the provider’s compute costs—which is why providers can offer much lower pricing for cached tokens.

    On usage bills, regular input tokens and cached read tokens are usually reported separately. Field names vary across providers, but the meaning is the same. The cached read price can be around one-tenth of the regular input price, or even lower.

    Output tokens are unaffected by caching—you pay for exactly what is generated. So the savings from cache hits come from the input side, especially in scenarios with very long contexts.

    Here’s a simple example:

    • Regular input price: P;
    • Cached read price: 0.1P;
    • A single request has 120K input tokens, of which 119K hit the cache and 1K are new content.

    Without caching, the input cost is roughly 120K × P. With caching, it is roughly 119K × 0.1P + 1K × P = 12.9K × P. In other words, input-side costs drop to about 10.7% of the original—roughly an 89% saving.

    This doesn’t even account for the latency benefit. Because most of the prefill is skipped, the model can start generating sooner—a significant advantage for interactive Agents.

    Quantified Savings at the Agent Step Level

    Why are Agent workloads especially well suited to cache hits?

    Because every model call in an Agent carries the full context: system prompt, tool definitions, conversation history, file snippets, compilation errors, test output, and so on. The genuinely new content is often just the latest user message or the latest tool result.

    For a single step of an intelligent coding Agent, the typical pattern is: reading about 119K tokens of historical prefix, with only about 875 new tokens requiring full computation.

    That means roughly 99.3% of the input tokens are reusable prefix. If this portion hits the cache and is billed at one-tenth the normal input price, the input cost of a single call drops to roughly one-tenth of the original.

    Multiplied across the dozens of calls an Agent makes, the total cost difference becomes substantial. This is the fundamental reason why “request volume grows but the bill doesn’t scale proportionally.”

    Note, however, that cache hits are not “free.” Reading from the cache still consumes GPU memory bandwidth, and attention computation for new tokens must also access the cached states. So cached read tokens are not zero-cost—they’re just substantially cheaper.

    Real-World Constraints on Cache Hits

    Cache hits are not a free optimization. They are bounded by several underlying conditions.

    GPU Memory Management

    The KV Cache consumes a lot of GPU memory. To support long-prefix caching, the server must retain large volumes of KV vectors. If every request cached its long prefix, memory would fill up quickly.

    As a result, servers typically impose cache limits, evict less-used prefixes with strategies like LRU, or set a TTL so cached entries expire after a period of time. For developers, this means you cannot assume the cache will always hit—treat caching as a performance optimization, not a correctness dependency.

    Cluster Scheduling

    If a request is routed to an instance without the relevant cache, the benefit is completely lost. Cluster scheduling therefore needs “cache affinity”: routing requests with the same prefix to the same instance whenever possible.

    But scheduling purely on affinity can hurt load balancing. One instance may become overloaded because it holds a hot prefix, while others sit idle. The server has to balance cache hit rates against overall throughput.

    API Pricing and Billing

    The billing model itself is a constraint. Because cached read tokens are priced separately, cache hit rate becomes a core cost-optimization metric. If the prefix is unstable and the hit rate drops, costs quickly revert to full price.

    This pushes application developers to cooperate with the caching mechanism at the request-construction level, rather than relying solely on server-side scheduling optimizations.

    How to Make Your Agent Cache-Friendly

    Once you understand how caching works, you can derive a set of cache-friendly request-construction principles.

    1. Put the Stable Prefix First

    System prompts, tool definitions, and long-document context should be assembled in a deterministic order and placed at the front of the message sequence. Don’t generate tool lists in random order, and don’t inject runtime environment information at the beginning of the system prompt.

    The key requirement for a stable prefix is “exact token sequence consistency.” Semantically identical content with a different token sequence still breaks the cache. For example, changing “please help me fix the issue” to “please help fix the issue” alters the prefix even though the meaning is nearly the same.

    2. Put Dynamic Content at the End

    Frequently changing information—current time, directory paths, process IDs, task-unique IDs, the latest code diff—should go into the last user message or the latest tool result.

    If time information must appear in the system prompt, it will likely invalidate the cached prefix for all subsequent conversation history. This calls for a trade-off against product requirements.

    An ideal structure looks like this:

    1. [Stable system prompt]
    2. [Stable tool definitions]
    3. [Stable conversation history]
    4. [Fresh user message / latest tool result]

    3. Avoid Unnecessary Reordering and Truncation

    Reordering history messages changes the prefix and invalidates the cache. If you must truncate history, start with the oldest, least important messages. But note that truncation itself changes the full context and may still alter the cached prefix—you’ll need to weigh context completeness against cache savings.

    4. Keep Tool Schemas Stable

    Field order, description text, and default values inside tool definitions must remain consistent. If a single request adds a random comment to a tool description, all subsequent requests may fail to hit the cache due to that tiny change.

    5. Continuously Monitor Cache Hit Rates

    Using the cached-token counts available in usage metrics or the relevant API documentation, you can approximate the cache hit rate: cached tokens read divided by total input tokens.

    If the hit rate is below expectations, first check whether recent changes polluted the prefix. Common culprits include: timestamps injected into requests, tool list ordering changes, reordered history messages, or dynamic fields placed early in the message sequence.

    Summary

    Cache hits are not a model capability—they are the result of inference systems and engineering practices working together.

    For Agent developers, understanding how caching works, how it’s priced, and what constraints apply means you can keep contexts complete while dramatically reducing costs. The benefits of caching are not automatic; they require healthy cooperation between the application and the service: the server handles KV cache management and scheduling, while the developer pushes variability as far toward the end of the prompt as possible.

    Only when the system prompt, tool definitions, and conversation history all remain stable—and only the last message changes—can the cache hit rate approach its ideal. That makes prompt caching one of the highest-leverage optimizations for long-context Agent cost reduction.