Understanding Input Caching and Billing for Large Model APIs
This article explains how input cache hits are billed in large model APIs, why pricing and time-of-day tiers keep changing, and how to model the total cost. It also covers four engineering practices for raising the cache hit rate, architectural and process responses to price changes, and common misconceptions to avoid.
1. What Is an Input Cache Hit
In the billing structure of large model APIs, the input side is usually split into two parts: input tokens that miss the cache, billed at the standard input price, and input tokens that hit the cache, billed at a significantly lower price. An “input cache hit” means the prompt prefix of the current request (or a reusable segment defined by the platform) matches content already cached on the platform side. The server reuses the intermediate computation results directly instead of repeating the full preprocessing, so those tokens receive a price discount.
Two points are widely agreed on in the industry. First, a cache hit is not “free”; it is “cheaper.” Second, whether a hit occurs is generally determined by exact prefix matching and the cache lifetime. Once the prefix changes, the content that follows is unlikely to hit. Understanding these two points is the foundation for all later cost optimization.
2. Why Billing Items Change Often: From Promotions to Peak and Off-Peak Tiers
The source material shows a typical pricing evolution path. A cloud vendor first published an initial price, then ran a limited-time promotion and extended the deadline several times, eventually making the discounted price permanent at roughly one quarter of the initial launch price. Later it introduced peak and off-peak tiered pricing, dividing the day into peak and off-peak periods (peak covering a few hours in the morning and a few hours in the afternoon), with a higher unit price during peak hours. Finally, it changed again so that weekends are billed at the off-peak price all day.
The logic behind this path is not mysterious. Compute supply for inference services fluctuates noticeably within a day, and using price signals to shift demand away from peak hours is the most direct way to improve resource utilization. For developers, the real risk is not a price increase itself but a billing structure that becomes multi-dimensional: unit price, time period, and cache hit rate multiply together to determine the final spend.
3. Turning the Bill into a Computable Model
For a given billing period, let the missed input tokens be (T{miss}), the hit input tokens be (T{hit}), and the output tokens be (T{out}), with unit prices (P{miss}), (P{hit}), and (P{out}). The total cost can be approximated as:
Cost = T_miss * P_miss + T_hit * P_hit + T_out * P_out
If a time-period factor is added, each period is accumulated separately. The cache hit rate is defined as:
HitRate = T_hit / (T_hit + T_miss)
This formula shows a key conclusion: in workloads where output length is relatively fixed, the bill is determined almost entirely by the hit rate and the time-of-day distribution. Raising the hit rate from 0.5 to 0.8 often saves more than switching models.
4. Raising the Hit Rate: Four Engineering Practices
Stabilize the prefix. Put invariant content such as the system prompt, tool descriptions, few-shot examples, and long documents at the very front of the prompt in a fixed order, and put user variables at the end. Any timestamp, random ID, or list with nondeterministic ordering in the leading content will break the cache.
Govern the cache key. If the platform uses a content hash or an explicit identifier as the cache key, make sure the same business semantics always produce the same key, avoiding key drift caused by field order or whitespace differences. Example:
import json, hashlibdef cache_key(payload: dict) -> str:# Serialize consistently so identical semantics produce an identical keyblob = json.dumps(payload, sort_keys=True, ensure_ascii=False, separators=(",", ":"))return hashlib.sha256(blob.encode("utf-8")).hexdigest()
Batch and merge. Aggregating short requests that can be merged into a single long-prefix request significantly increases the chance of reuse, but latency and timeouts must be weighed, so pair this with a queue and timeout control.
Observability. At minimum, log the input tokens, hit tokens, time period, and unit price for every request, then build a hit-rate dashboard and alerts:
metrics: hit_rate, miss_tokens, peak_ratio, cost_per_1k_requestsalert: hit_rate < 0.4 for 10 minutes -> check whether the prefix is polluted
5. Handling Price and Time-Period Changes: Architecture and Process
- Abstract the billing layer. Make unit prices, the time-period table, and the cache discount configuration rather than scattering them through business code. When prices change, edit only the configuration and keep historical versions for bill traceability.
- Schedule around peaks. Move offline batch jobs, batch summarization, and log analysis to off-peak or low-rate periods; keep interactive online paths in peak hours and offset the peak unit price with a high cache hit rate.
- Change notification. The common industry practice is to give advance notice before a price change; users who do not accept the new price can stop the service and request a refund of their balance. On the engineering side, subscribe to announcement channels and keep the ability to switch to a backup model or backup region with one action in the configuration center.
- Budget guardrails. Set a daily cost cap with tiered degradation: first reduce the maximum output length, then fall back to a smaller model, and finally queue or reject low-priority requests.
Degradation order: shorter output -> drop non-essential examples -> switch to a smaller model -> queue/throttle
6. Common Misconceptions
The first is treating a “hit” as “no cost” and ignoring that hit tokens still carry a unit price. The second is changing the system prompt frequently, which keeps the hit rate near zero over time. The third is looking only at unit price and not at time periods, so the bill is amplified during peak hours. The fourth is lacking bill attribution, making it impossible to identify which business line is driving up cost. All of these are avoided the same way: make hit rate, time-of-day distribution, and output length three metrics that are observable, alertable, and regression-testable.
7. Summary
An input cache hit is essentially a billing item that trades engineering discipline for a price discount. As pricing and time-period rules keep evolving, the certainty developers can control comes from three places: a stable prefix and cache key, a schedule that avoids peak hours, and a cost observability system with attribution. Get these three solid, and the bill stays predictable and optimizable regardless of how the billing rules change.
