Understanding LLM API Input Cache Hit Billing: Cost Drivers and Engineering Practices
This article breaks down how input cache hit pricing works for large language model APIs and why API cost is a combination of cache hit rate, time-of-day distribution, and prompt structure rather than a single unit price. It explains the technical basis of prefix caching, the factors that affect hit rates, a billing formula with cost decomposition, and a practical checklist covering prompt layering, off-peak scheduling, context governance, canary regression, and budget guardrails. It also corrects common misconceptions and shows how to build a cost dashboard around general billing dimensions.
1. What “Input Cache Hit” Means, Seen Through a Price Timeline
In long-context, multi-turn, and AI coding applications, input-side spending often exceeds output-side spending and becomes the main source of the bill. One cloud provider went through a representative billing evolution in 2026: in late April it first published the initial API price for a flagship model and launched a limited-time promotion; it then announced a reduction in the input cache hit price across the entire API lineup and pushed the promotion end date from early May to the end of May; in late May it signaled a price adjustment after the promotion ended; by the end of May the promotional price was made permanent, with the final price at roughly one quarter of the original launch price; at the end of June it introduced peak/off-peak tiered pricing; and in late August it adjusted the billing rules further so that weekends are billed at the off-peak rate all day.
The takeaway for developers is that the cost of a model API is not a single “unit price” problem. It is a combined problem determined by cache hit rate, time-of-day distribution, and context structure.
2. How Input Cache Hits Work
An input cache hit means that when the prompt prefix of the current request is identical or highly similar to that of recent requests, the server can directly reuse the already computed intermediate state (such as the attention key-value cache) without re-running the forward computation for that portion. The tokens in the hit portion are therefore billed at a significantly lower price, while the tokens in the missed portion are billed at the regular input price.
Key factors that affect the hit rate:
- Prefix stability: whether the system prompt, tool definitions, and few-shot examples stay word-for-word unchanged;
- Order consistency: whether variable content is placed at the end of the prompt;
- Time window: whether the interval between two requests falls within the cache lifetime;
- Model and parameter consistency: switching models or changing decoding parameters may invalidate the cache.
Illustrative pseudocode (keep the prefix unchanged and only append the variable tail):
SYSTEM = "You are a senior coding assistant. Follow these rules: ..." # unchanged over the long termTOOLS = [...] # fixed orderdef build_prompt(user_msg, history_tail):# Put the variable part last to maximize prefix reusereturn f"{SYSTEM}\n{TOOLS}\n{history_tail}\nUser: {user_msg}"
3. Billing Model and Bill Breakdown
The cost of a single request can be abstracted as:
cost = hit_tokens * p_hit + miss_tokens * p_miss + out_tokens * p_out
Here p_hit is the cache hit unit price and p_miss is the regular input unit price. The benefit of a higher hit rate is non-linear: when the hit portion accounts for a very high share, input-side cost quickly approaches a lower bound. Combined with peak/off-peak tiers and the weekend off-peak rule, the actual cost should also be multiplied by a time-of-day factor. It is advisable to log six dimensions for every call: model, ts, prompt_tokens, hit_tokens, out_tokens, and tier, and to aggregate them with a logging service and monitoring alerts. Only then can you tell whether a cost anomaly comes from a drop in hit rate or from a time-of-day mismatch.
4. Engineering Checklist
- Prompt layering: stable system layer → stable tool layer → variable session layer; never insert timestamps or random IDs at the top.
- Batching and off-peak scheduling: schedule offline jobs into off-peak hours and weekends to take full advantage of tiered and off-peak pricing.
- Context governance: use rolling summaries instead of full history, so a long prefix is not invalidated as a whole by a tiny change.
- Canary and regression: evaluate the hit rate change for any prompt change, and make the hit rate part of release metrics.
- Budget guardrails: set quotas and alert thresholds per business line to prevent abnormal retries from inflating the bill.
5. Common Misconceptions
- Treating a “hit” as free: a hit only means a lower unit price; the tokens are still billed.
- Assuming the cache lasts forever: there is a lifetime window, and after a long idle period the cache needs to be warmed up again.
- Optimizing only the unit price: ignoring time-of-day and structure leaves limited room for cost reduction.
- Starting with variable content: a single change invalidates the entire prefix.
6. Conclusion
Cost governance around input cache hits is essentially the merger of “prompt engineering” and “cost engineering”: a stable prefix raises the hit rate, off-peak scheduling fits tiered and off-peak rules, and an observability system makes the optimization verifiable. If you use the relevant model services from Baidu AI Cloud, it is likewise advisable to build your own cost accounting dashboard around general billing dimensions and let data drive continuous optimization.
