How Prompt Caching Cuts LLM Input Costs by Up to 90%

    This article explains why long-context LLM applications often pay repeatedly for the same input, and how prompt caching turns that repeated billing into a one-time cost. It covers prefix matching, cache lifetime, the trade-off between write premiums and read discounts, the break-even point for cache hits, the scenarios that benefit most, a layered prompt design for stable prefixes, observability metrics, and fallback strategies. Follow the checklist to reduce input-side costs to roughly one tenth of the original in high-repetition workloads.

    1. Where the Cost Anxiety Really Comes From: Repeated Billing of Long Contexts

    In enterprise LLM applications, a ballooning bill rarely comes from “asking too much.” It comes from asking for the same thing over and over. System prompts, role definitions, tool and function descriptions, business glossaries, retrieved long document chunks, and few-shot examples barely change within a single session, or even across a whole day, yet they are fully re-billed with every request. Once a single request grows from a few hundred tokens to tens of thousands, the repeated portion becomes the dominant line item.

    Recognizing this shifts the optimization goal from “switch to a cheaper model” to “eliminate repeated billing.” The latter usually delivers a larger payoff without sacrificing output quality.

    2. How Prompt Caching Works

    Most mainstream LLM services offer a context caching capability. The core idea: the stable portion of a request prefix is retained on the server side for a period of time, and subsequent requests that hit that prefix are only fully computed and billed for the newly added content.

    Two implications follow.

    First, caching is typically prefix-based. Stable content must sit at the very front of the prompt, because any change further back invalidates reuse of everything before it. Putting frequently changing fields such as timestamps, random IDs, or user nicknames at the beginning is the most common “cache killer” in practice.

    Second, caches have a lifetime. Most implementations use a short survival window (often on the order of minutes) and may evict entries early under capacity pressure. A cache is therefore not persistent storage, but reuse within a time window.

    3. Billing Structure: Write Premium vs. Read Discount

    Cache billing generally has two components, and understanding their relationship is the only reliable way to decide whether caching is worth enabling:

    • Write cost: when a cache is first created, those tokens are usually billed above the standard rate (commonly around 1.25x the original price), a premium paid for retaining state.
    • Read cost: cached portions are billed at a significant discount, in some schemes as low as 10% of the original price, saving roughly 90%.

    This yields a simple but highly practical break-even rule. With a 25% write premium and a 10% read rate, the extra investment in creating the cache must be amortized by later hits. As a rough estimate, once there are about three or more hits within the same cache lifetime, caching starts producing a net gain; the more hits, the closer the marginal benefit approaches the theoretical ceiling.

    Conversely, if a long context is used once or twice and never appears again, enabling the cache is a net loss. That is the mathematical root of “caching suits high-frequency repetition, not one-off long documents.”

    4. Where the Gains Are Largest

    • Agent services with a fixed system prompt plus long tool descriptions; the higher the QPS, the more obvious the benefit.
    • Multi-turn Q&A over long documents: the document is parsed once, and follow-up turns share the same prefix.
    • Coding assistant tools: repository conventions, interface contracts, and sample code stay unchanged for long periods.
    • Batch structured extraction: fixed templates with only the trailing data changing.
    • Multi-label classification and moderation: the taxonomy and decision criteria form a stable prefix.

    The unsuitable cases are equally clear: sessions where the prefix changes every time, very long documents that appear only once, and short requests that are cost-insensitive but extremely latency-sensitive.

    5. Engineering Practice: A Layered Design for Stable Prefixes

    Split the prompt explicitly into three layers, ordered from front to back:

    1. [Layer 0] Globally invariant: safety policy, output format constraints, role definition
    2. [Layer 1] Slowly changing business content: glossary, tool/function schema, retrieved knowledge chunks
    3. [Layer 2] Per-request variable: session history delta, user input, timestamps, random identifiers

    Coding conventions matter just as much:

    1. def build_prompt(knowledge: str, user_input: str, session_delta: str) -> list[dict]:
    2. # The earlier, the more stable; always place variable content at the end
    3. return [
    4. {"role": "system", "content": SYSTEM_PROMPT}, # Layer 0
    5. {"role": "system", "content": knowledge}, # Layer 1
    6. {"role": "user", "content": session_delta}, # Layer 2
    7. {"role": "user", "content": user_input},
    8. ]

    Three engineering tasks remain. First, hash the prefix and monitor the hit rate, treating the “prefix fingerprint” as a core metric. Second, switch knowledge chunk updates to scheduled batch refreshes rather than real-time changes on every request. Third, control total prefix length: longer is not always more cost-effective, so the optimum must be calculated against hit frequency.

    6. Observability: Making the Savings Measurable

    Optimization must be verifiable. Mature solutions return cache-related usage details in the response, such as the number of tokens written to the cache this time and the number read from it. These two fields support a cost dashboard:

    • Cache hit rate = read tokens / (read tokens + write tokens + uncached input tokens)
    • Average cost per request, and the effective discount delivered by caching
    • Alerts correlating prefix change events with sudden drops in hit rate

    Without this instrumentation, cache optimization easily becomes “the config is on, but nobody knows whether it works.” On platforms such as Baidu AI Cloud Qianfan, it is likewise advisable to combine logging and monitoring/alerting capabilities so that usage details accumulate into continuously observable metrics, rather than being reverse-engineered from the monthly bill.

    7. Invalidation, Fallback, and Risk Control

    A cache is an optimization, not a correctness dependency. Assume it may fail at any time:

    • Fallback path: on a miss, automatically fall back to a full request; functionality is unaffected and only cost rises.
    • Consistency strategy: version knowledge updates, and allow the prefix to change only when the version changes, avoiding churn.
    • Isolation and compliance: in multi-tenant scenarios, confirm the cache scope to prevent cross-user context leakage; assess whether sensitive data is appropriate for caching under compliance requirements.
    • Capacity and rate limiting: under high concurrency, cache creation and eviction amplify volatility, so pair them with queues and retry backoff.

    8. Common Pitfalls

    1. Placing variable fields first, driving the hit rate to zero over the long run.
    2. Looking only at the discount ratio while ignoring the number of hits and the write premium.
    3. Assuming caching can replace retrieval augmentation; it only lowers the cost of repeated computation and does not improve knowledge freshness.
    4. Letting the prefix grow without bound, which raises the per-write cost instead.
    5. Missing usage instrumentation, making it impossible to tell whether the optimization actually took effect.

    9. Implementation Checklist

    • Is the prompt layered by stability, with all variable content placed at the end?
    • Have you measured the cache hit rate and average hits, and passed the break-even point?
    • Have knowledge refreshes been converted to versioned batch updates?
    • Is there automatic fallback and alerting for cache misses?
    • Are usage details wired into monitoring and a cost dashboard?

    Following this approach, in long-context, high-repetition call patterns, compressing input-side cost to around one tenth of the original is an achievable engineering goal. The essence of cost optimization is not finding a cheaper channel, but making the same information billed only once.