LLM API Cache Hit Pricing Discount Explained: How Prompt Caching Cuts Input Costs
This article breaks down a recent MaaS pricing change that discounts only cached input tokens, explains how prompt caching (prefix/KV reuse) works during prefill and decode, and provides a cost formula plus a Python estimation script to calculate real savings. It also covers six engineering practices for improving cache hit rates, the metrics to monitor, an A/B validation approach, and common misconceptions, helping developers decide whether restructuring prompts for cache friendliness is worth the effort.
1. Background: A Price Cut That Only Touches Cached Input
A cloud provider’s model-as-a-service (MaaS) platform recently announced that, starting at midnight on a specified date, it would lower the billing price of a general-purpose large language model service in one region: the input price for the cache-hit portion would be adjusted to 20% of the original rate, with all other billing items unchanged. The announcement itself is short, but the information is concentrated in three qualifiers: region, cached input, and everything else unchanged.
These three qualifiers determine the engineering implications:
- Only calls in a specific region are affected; cross-region traffic does not automatically benefit.
- Only the input side is affected, and only when the cache is hit; the cost structure for output and for cache-missed input is unchanged.
- This is therefore not a “across-the-board price cut” but a structural price adjustment, and the benefit depends entirely on whether your requests can reliably hit the cache.
Treating it as a promotion misses the point. A more accurate framing: the cloud provider is using price as a lever to encourage developers to hand high-frequency repeated prefixes (system prompts, long documents, few-shot examples, tool definitions) to the cache for reuse, thereby reducing overall inference compute overhead. For developers, this is an arithmetic problem of “retrofit cost vs. long-term bill.”
2. How It Works: What Prompt Caching Actually Caches
LLM inference has two phases: prefill (a one-time forward pass over the input that produces key-value caches for all positions) and decode (token-by-token autoregressive generation). With long inputs, prefill is often the main source of first-token latency and the most compute-intensive stage.
The core idea of prompt caching (also commonly called prefix caching or KV reuse) is that if the prefix of the current request is byte-for-byte identical to a previous request, the key-value state for those tokens can be reused directly without recomputation. This yields two benefits: lower first-token latency and lower input-side billing.
Hit determination typically relies on the following points (common industry implementation ideas, not proprietary to any single platform):
- Strict prefix matching: comparison starts from the first token of the sequence; once any position differs, nothing after it can be reused.
- Token- or block-level granularity: common implementations cache in fixed-length blocks, and content within a block must match exactly.
- A lifecycle: subject to time-to-live and capacity eviction policies (such as least recently used), cold data gets evicted.
- Scope isolation: caches are not shared across different model versions, different parameter configurations, or different tenants.
This explains a common confusion: why requests with “roughly the same content” fail to hit. The cache does not recognize semantic similarity; it only recognizes bytes and order.
3. Cost Model: Turning the Discount into a Computable Formula
Let the original input unit price be P_in, the cache-hit discount be d (0.2 in this case), the number of input tokens be T_in, the share of cacheable prefix within it be r, the hit rate be h, and the output unit price P_out and output token count T_out remain unchanged. The cost of a single call can be written as:
Cost = P_in * T_in * (1 - r*h*d) + P_out * T_out
Relative to the original price, the savings ratio on the input side is r * h * (1 - d). When d = 0.2, the theoretical ceiling is 80% savings, but real savings must be multiplied by two discount factors:
r: the share of the request that is genuinely reusable prefix. When the system prompt is only 200 tokens but the user input is 8000 tokens,ris small.h: the hit rate. With low concurrency, long gaps, or prefixes polluted by dynamic content,hdecays quickly.
An illustrative Python estimation script:
def estimate(p_in, p_out, t_in, t_out, r, h, d=0.2):"""Estimate per-call cost and input-side savings ratio"""cost = p_in * t_in * (1 - r * h * d) + p_out * t_outbase = p_in * t_in + p_out * t_outreturn cost, 1 - cost / base# Long-document QA: high cacheable prefix share, stable hitsprint(estimate(1.0, 2.0, 12000, 400, r=0.9, h=0.95))# Short prompts, multi-turn chat: low prefix shareprint(estimate(1.0, 2.0, 600, 300, r=0.2, h=0.6))
The conclusion is intuitive: this discount is friendliest to workloads with long context, fixed prefixes, and repeated calls (document QA, code-base QA, batch extraction, fixed-persona customer service), and is nearly imperceptible for short-input, highly personalized scenarios.
4. Engineering Practice: Six Ways to Make Hit Rate Controllable
- Make the prefix static: order system prompts, tool/function definitions, examples, and knowledge snippets from “stable” to “volatile,” and always place dynamic variables (timestamps, user IDs, random numbers, retrieval results) at the end. Even one changing character inserted into the prefix invalidates everything after it.
- Separate templates from rendering: use a template engine to keep serialization stable, avoiding non-deterministic differences from dict iteration order, float formatting, or whitespace.
- Control concurrency rhythm: caches have capacity and a TTL, so a long silence after a traffic burst followed by a new request may find the entry already evicted. Batch jobs are best scheduled in concentrated windows.
- Region affinity: this price adjustment covers a single region only, so the access layer should support pinning routes by business dimension and avoid multi-region round-robin that dilutes the hit rate.
- Explicit version management: a model version or prompt template upgrade invalidates the cache wholesale, so expect a short-term cost increase during canary rollout.
- Observability first: without hit-rate metrics, optimization is blind tuning.
Core metrics worth collecting:
cache_hit_ratio cache hit rate (hit tokens / cacheable tokens)cached_input_tokens number of input tokens served from cacheprefill_latency_p95 p95 first-token latencycost_per_request cost per requestcache_eviction_rate cache eviction rate (indirect signal of capacity pressure)
5. Validation: Small-Traffic Testing Instead of a Full Switch
Take a slice of real traffic before and after the change for an A/B comparison with a fixed input distribution, and compare four data points: hit rate, first-token latency, per-request cost, and output quality consistency. Note in particular that cache reuse should not change generation results. If you observe drift in the output distribution, it usually means the prefix concatenation order or parameter configuration changed unexpectedly, which should be investigated first rather than attributed to the model.
Pseudocode for organizing requests:
def build_request(system_prompt, knowledge, user_query):# Stable prefix first, dynamic content last, to maximize reusable lengthprefix = f"{system_prompt}\n{knowledge}"return {"messages": [{"role": "system", "content": prefix},{"role": "user", "content": user_query},]}
6. Common Misconceptions
- Assuming all input is discounted: only the hit portion gets the discount; missed input is still billed at the original rate.
- Assuming a price cut means a lower bill: if
rorhis very low, the bill barely moves. - Putting dynamic content first: this is the most common cause of a collapsing hit rate.
- Ignoring regional differences: regions not covered see no change at all.
- Overlooking the output side: for long-output workloads the main cost is still in the generation phase, so input-side optimization has limited benefit.
7. Conclusion
The real value of this kind of structural price adjustment is turning “cache friendliness” into a measurable, optimizable engineering metric. For developers, the worthwhile order of actions is: first quantify r and h, then restructure the prefix, and finally use regional routing and scheduling policies to keep the hit rate stable. If you are using a related model service from Baidu AI Cloud, it is likewise worth doing a local estimate against the general billing model before deciding whether to invest in the retrofit—data-driven conclusions are always more reliable than marketing claims.
