Understanding LLM API Input Cache Hit Billing: How Prompt Caching Works and How to Optimize Costs

    This article explains how input cache hit billing works in large language model APIs and why it has become a central cost factor. It covers the general mechanics of prefix-based prompt caching, a typical timeline of pricing and rule changes, the technical conditions that determine cache hits, cost optimization under peak and off-peak tiered pricing, and engineering practices for cost governance. Readers will learn how to measure hit rates, attribute costs, set budgets and alerts, and validate changes so that reusable prefixes and off-peak scheduling keep spending predictable.

    1. Why Input Cache Hits Have Become a Core Cost Factor

    In large-scale language model API usage, billing is typically split into two dimensions: input and output. The input side is further divided into cache misses and cache hits. An input cache hit means the prompt prefix of the current request closely matches context that was processed recently, so the server can reuse the already computed intermediate state instead of running the full attention computation again. Because most of the compute cost is avoided, the per-token price for the hit portion is usually significantly lower than for the miss portion.

    This mechanism directly changes the cost structure. In the past, cost was almost linearly related to total token volume. Once prefix reuse is introduced, cost depends more on the reusable ratio. For scenarios such as long system prompts, fixed tool descriptions, batch document question answering, and multi-turn conversations, the reusable ratio can often reach a high level, so the cost reduction can far exceed intuitive expectations.

    It is worth emphasizing that cache hits are not a proprietary feature of any single vendor. They are a class of inference optimization techniques widely adopted across the industry, and each provider defines its own billing basis and discount level. Understanding the general principle is more valuable in the long run than memorizing a specific set of numbers.

    2. Timeline: A Typical Evolution of Pricing and Rules

    Take a cloud provider as an example. Its adjustments in 2026 are highly representative and can be abstracted into five stages:

    1. Initial pricing (late April): The API price for the flagship model was announced first, followed by a limited-time promotion that used lower prices to attract developers to migrate and run load tests.
    2. Series-wide reduction (April 26): A reduction in the input cache hit price across the entire API series was announced, along with an extension of the flagship model promotion period. The signal here is strong: the target of the price cut was not output but the cache hit segment specifically, indicating that the provider was actively guiding developers to optimize context reuse.
    3. Promotion extension and confirmation (late April to late May): The deadline shown on the official website moved back, and official announcements together with researchers on social platforms confirmed that some changes were permanent. The promotion end date was pushed from early May to the end of May.
    4. Permanent discounts (after May 31): After the promotion ended, the flagship model pricing was formally adjusted, with the final price at roughly one quarter of the original launch price.
    5. Fine-grained billing (June to August): At the end of June, peak and off-peak tiered pricing was introduced, with unit prices differentiated by load period. In late August, the billing rules were adjusted further so that weekends were billed at the off-peak rate all day.

    This path reveals a common pattern: limited-time promotion, extension and observation, permanent adoption, then fine-grained tiering. For developers, the real risk is not any single price cut but writing a temporary promotional price into a long-term cost budget.

    3. How Cache Hits Are Determined Technically

    To take advantage of the hit discount, you first need to understand the conditions for a hit. The prevailing industry practice is to match based on the prompt prefix, so a stable prefix is the prerequisite for a hit. Illustrative pseudocode:

    1. def build_prompt(system, history, user_input):
    2. # The prefix must be byte-for-byte stable; any change causes all subsequent content to miss
    3. stable_prefix = system + render_tools(TOOLS) + render_history(history)
    4. return stable_prefix + user_input
    5. # Bad example: putting a timestamp and random ID at the very front
    6. bad = f"Current time {now()} session {uid}\n" + system + user_input

    Common patterns that break cache hits include: prepending the current time, a request ID, or randomly ordered retrieved snippets to the very front of the system prompt; using multiple templates with similar but different wording for the same business; and enabling preprocessing options that rewrite the text. All of these change the prefix hash and degrade what should have been a hit into full billing.

    In addition, caches usually have a lifecycle and capacity limits. Long idle periods, sudden concurrency spikes, or overly long context can all cause reuse to fail. The hit rate is therefore a metric that needs to be continuously observed in monitoring, not a one-time configuration.

    4. Cost Optimization Under Peak and Off-Peak Tiered Pricing

    The essence of peak and off-peak pricing is to convert the time distribution of load into price differences: unit prices are higher during business peak hours and lower during off-peak hours, and some providers treat the entire weekend as off-peak. This extends cost optimization from writing code to scheduling tasks.

    Practical approaches include: scheduling batch summarization, offline evaluation, log analysis, and data cleaning tasks into off-peak windows; keeping the online path latency-first and the offline path cost-first; and setting different model tiers and maximum output length limits for the two paths.

    1. # Illustrative: routing by task type
    2. routing:
    3. online:
    4. tier: standard
    5. timeout_ms: 800
    6. offline:
    7. tier: economy
    8. schedule: "weekend_or_offpeak"
    9. max_output_tokens: 2048

    5. Engineering Practices for Cost Governance

    It is advisable to treat cost as an observable object and govern it across at least four areas:

    • Metering: Record hit and miss tokens separately, input and output tokens, call counts, and failed retry counts, rather than looking only at the total.
    • Attribution: Tag by business line, endpoint, and template version to identify which type of call is driving up the miss ratio.
    • Budgets and alerts: Set daily and monthly budgets and period-over-period spike alerts, and rate-limit abnormal call sources.
    • Regression validation: After template or prompt changes, compare hit rate and cost per task against a fixed sample set to prevent “optimizing the prompt” from actually increasing cost.

    A practical estimation form is as follows:

    1. Cost per call ≈ hit tokens × hit unit price
    2. + miss tokens × miss unit price
    3. + output tokens × output unit price

    Once the hit token ratio is treated as a first-class metric, teams naturally tend to stabilize prefixes, streamline tool descriptions, and merge duplicate context. These changes often improve latency and stability at the same time.

    6. Conclusion

    Input cache hits are not just a discount item but a key lever at the intersection of prompt engineering and cost engineering. In the face of rule changes such as promotion extensions, permanent discounts, peak and off-peak tiering, and flat weekend off-peak billing, the prudent strategy is to build your own estimation sheet on top of the general billing model, use hit rate and cost per task as core metrics, lock down reusable prefixes, move deferrable tasks into off-peak windows, and keep conservative assumptions about temporary discounts in your budget. That way, the cost structure stays controllable no matter how prices change.