The Real Cost of LLM APIs: Cache Hit Rate and Input Discounts
Standard input pricing is only one part of LLM API cost. This article explains how cache hit rate and cache pricing affect the effective input price, why hit rates vary by workload, and how to model, monitor, and optimize real input spend using formulas, logs, and sensitivity analysis.
The Real Cost of LLM APIs: Cache Hit Rate and Input Discounts
When evaluating LLM API costs, looking only at the “standard input unit price” is a common mistake. Standard pricing usually describes the input token price when the cache is not hit, while the actual bill is often composed of both cache-hit and cache-miss portions. For model services that support context caching or prefix caching, cache hit rate directly changes the effective input-side unit price. When the hit rate rises from 0% to 90%, the actual input cost can differ by several times. Cost evaluation should therefore shift from “unit price comparison” to a combined assessment of “billing logic + workload characteristics.”
1. The Core Formula for Input Cost
Let:
P_std: standard input unit price, in CNY per million tokens;P_cache: input unit price for the cache-hit portion;H: cache hit rate, ranging from 0 to 1;C_input: actual cost per million input tokens.
The actual input cost can be expressed as:
C_input = H × P_cache + (1 - H) × P_std
If the cache unit price is a proportion α of the standard price, that is, P_cache = α × P_std, then:
C_input = P_std × [1 - (1 - α) × H]Effective discount = C_input / P_std = 1 - (1 - α) × H
This formula shows two key variables: the discount of the cache unit price relative to the standard price, α, and the cache hit rate, H. The former is determined by the service’s billing rules, while the latter is determined by request structure, prefix stability, cache lifecycle, and traffic distribution.
For example, if the cache unit price is about 17.5% of the standard input price and the hit rate is 90%, the actual input cost is approximately:
C_input = 0.9 × 0.175 × P_std + 0.1 × P_std= 0.2575 × P_std
That is about 25.75% of the standard price. If the hit rate drops to 50%, the actual cost becomes 0.5875 × P_std; if the hit rate is 0%, the cost returns to the standard price. The hit rate is therefore not an “add-on” in the bill but a core variable that determines the effective unit price. The figures above are for demonstrating the billing model only and do not represent any vendor’s fixed quote or commitment.
2. Why Cache Hit Rate Fluctuates
Cache hit rate is not a fixed constant. It fluctuates with workload changes. Common influencing factors include:
- Prefix stability: Tokens located at the front of a request, such as system prompts, tool definitions, few-shot examples, and fixed-format instructions, reduce prefix reuse probability if they change frequently. If timestamps, random IDs, or dynamic user information are placed in the system prompt, the cache key may become unstable.
- Request distribution: Batch tasks under the same template, multi-turn conversations, and repeated Q&A are more likely to form a high hit rate; open-ended, long-tail requests with large prefix differences usually have lower hit rates.
- Cache granularity and lifecycle: Different services may differ in the minimum cacheable token length, cache TTL, eviction policy, and billing definitions. Refer to the specific service documentation. Cache is not permanent storage, and requests after a long interval may return to the cache-miss state.
- Growth of multi-turn conversations: In multi-turn dialogues, historical context continues to grow. If a stable prefix is retained in each turn, the hit rate may be high; if the system prompt is rewritten or dynamic content is inserted in each turn, the hit rate will decrease.
- Multimodal and structured input: Content such as images, tables, and tool call results changes the input sequence. Their serialization method, order, and version changes all affect prefix matching.
- Concurrency and traffic peaks: Under high concurrency, cache write, read, and eviction behavior may change, and averages may hide low hit rates for long-tail requests.
Therefore, evaluation should be grouped by business scenario rather than relying only on a global average. For example, customer service Q&A, document summarization, code completion, and batch review have very different prefix reuse characteristics and should have separate cost models.
3. How to Evaluate Real Input Cost
A recommended approach is “formula + logs + sensitivity analysis.”
3.1 Build a request-level cost model
Collect input tokens, cache-hit tokens, cache-miss tokens, cache-write tokens, output tokens, and request template ID from request logs. If the service’s billing definition includes cache write fees, count them separately to avoid underestimating total cost by calculating only the read discount.
3.2 Calculate the effective input unit price
def effective_input_cost(std_price, cache_price, hit_rate):"""std_price: standard input price, CNY per million tokenscache_price: cache-hit input price, CNY per million tokenshit_rate: cache hit rate, 0~1"""if not 0 <= hit_rate <= 1:raise ValueError("hit_rate must be in [0, 1]")return hit_rate * cache_price + (1 - hit_rate) * std_pricedef discount_ratio(std_price, cache_price, hit_rate):return effective_input_cost(std_price, cache_price, hit_rate) / std_pricefor h in [0, 0.3, 0.5, 0.7, 0.9, 0.95]:cost = effective_input_cost(4.0, 0.7, h)print(f"hit_rate={h:.0%}, effective_cost={cost:.4f}, discount={discount_ratio(4.0, 0.7, h):.2%}")
This code is for illustrating the general calculation logic only. In production, price parameters should be made configurable to avoid hardcoding, and aggregation should be performed by template, tenant, model version, and date.
3.3 Perform sensitivity analysis
Observe at least three sets of curves: cost changes as the hit rate moves from 0% to 95%, cost changes as the cache unit price moves from 10% to 30% of the standard price, and budget changes when request volume grows 2x and 10x. This converts “unit price discounts” into a “monthly budget range.”
3.4 Establish monitoring metrics
Recommended metrics include: cache_hit_rate, input_tokens_total, cache_read_tokens, cache_write_tokens, effective_input_price, cost_per_request, prefix_reuse_ratio, and hit rate percentiles grouped by template. Averages alone are not enough; differences between P50 and P90 may reveal low hit rates for long-tail requests. When selecting model services, cache hit rate can also be included in the TCO model, and logging and monitoring/alerting capabilities can be combined for continuous observation.
4. Optimization Practices
First, stabilize the prefix. Place system prompts, tool definitions, output format constraints, and few-shot examples at the front of the request and manage them with version control; move dynamic content later to avoid random IDs, timestamps, and meaningless decorative text breaking prefix reuse.
Second, template requests. Use unified templates and fixed field order for batch tasks to reduce serialization differences. In multi-turn conversations, retain a stable historical prefix and avoid rewriting the system prompt in every turn.
Third, route by scenario. For high-reuse scenarios, prioritize invocation methods that support caching; for low-reuse, highly dynamic scenarios, focus on standard pricing, output cost, and retry cost rather than over-optimizing for hit rate.
Fourth, set budgets and alerts. Set token budgets, effective unit price thresholds, and hit-rate decline alerts by template and tenant to prevent cache policy changes or prompt revisions from causing sudden cost increases.
Fifth, run regular regression tests. After prompt version upgrades, remeasure hit rate and actual cost instead of relying on old assumptions.
5. Common Pitfalls
Looking only at standard pricing ignores the actual discount brought by hit rate; looking only at hit rate ignores cache write fees, output fees, and retry fees; looking only at global averages hides low hit rates for long-tail requests; treating cache as permanent storage underestimates the impact of TTL and eviction policies. A more rigorous approach is to use the formula as the core, logs as the basis, and scenario grouping and sensitivity analysis as the method, continuously calibrating the actual input unit price. This enables explainable and reproducible cost judgments in model selection, capacity planning, and budget approval.
