How to Calculate the Real Cost of Large Model APIs
Comparing per-million-token prices is misleading because actual API cost depends on cache hit rate, input/output ratio, and time-of-day pricing. This article breaks the bill into three components, shows where two pricing schemes cross over, and explains how to optimize hit rate, weight peak-hour usage, and convert subscription plans into an equivalent pay-as-you-go cost for a fair comparison. It also provides reusable Python snippets for cost estimation and hit-rate monitoring.
1. Why Comparing Unit Prices Often Gives the Wrong Answer
When evaluating the cost of a large model API, the most common approach is to put the “price per million tokens” of two options side by side and declare one more expensive. The problem is that this collapses a multi-dimensional cost function into a single scalar. The actual bill is determined by at least four variables: the price of the cache-hit portion, the price of the uncached input portion, the price of the output portion, and whether requests fall into peak-hour windows.
The debate in the original discussion stems from exactly this: one platform charges about CNY 0.115 per million tokens for cached input, while another charges about CNY 0.05 for cached input during off-peak hours, making the former look more than twice as expensive. But once you plug in hit rate and the input/output ratio, the conclusion reverses completely. Unit price is a parameter, not a conclusion.
A more rigorous framing is to write unit cost as a function of hit rate and ratio, then discuss how that function behaves across different ranges, instead of arguing about which is higher at a single point.
2. The Cost Model: Splitting the Bill into Three Parts
Mainstream token billing usually splits a request into three parts: cache-hit input, uncached input, and model output. Let:
- Total tokens be
T - Cache hit rate be
h - Input share be
p_in, output share bep_out, withp_in + p_out = 1 - Cache-hit unit price
c_cache, uncached input unit pricec_in, output unit pricec_out
Then unit cost can be written as:
Cost_per_Token = h * p_in * c_cache+ (1 - h) * p_in * c_in+ p_out * c_out
This formula reveals three easily overlooked facts.
First, the cache unit price only matters at a high hit rate. When h approaches 1, cost is almost entirely driven by c_cache; as h drops, the (1-h) * c_in term grows quickly and cancels out the advantage of cheap caching.
Second, the output share is a rigid cost. Output tokens usually carry the highest unit price and cannot be optimized through caching, so a rising p_out pushes total cost up linearly.
Third, hit rate is an architecture metric, not luck. It depends on whether the prefix is stable, whether the system prompt is fixed, and whether multi-turn conversations reuse the same context.
3. Recomputing Two Scenarios: Where Is the Crossover
Reusing the two sets of assumptions from the original material makes the crossover phenomenon clear.
Scenario A: hit rate 98%, input share 1.5%, output share 0.5% (the rest is the cached portion). Here the high-hit-rate option costs about CNY 12.57 per 100 million tokens, and the off-peak option about CNY 9.40, so the latter is cheaper.
Scenario B: hit rate drops to 95%, with the same ratios. Now the former costs about CNY 14.92 and the latter about CNY 18.25, so the former is the better deal.
Looking at both sets together, the key conclusion is not “which is cheaper” but that there is a critical hit rate: above it, the option with the lower cache price wins; below it, the option with the lower uncached input price wins. In engineering, the right move is to first measure your own business’s real hit rate and then set the budget, rather than planning around the best-case values published by vendors.
Note that the numbers above come from hypothetical examples in public discussion and only demonstrate the model structure; they should not be used as a price reference for any specific product. Actual prices, off-peak windows, and quota rules should follow the official documentation.
4. Hit Rate: The Only Variable Worth Optimizing
Since hit rate is the most sensitive variable in the cost function, the engineering practices around it are worth spelling out:
- Stable prefix. Put the system prompt, tool descriptions, and few-shot examples at the very front of the request, and do not concatenate volatile content such as timestamps or random IDs.
- Session reuse. Keep the history prefix unchanged across multi-turn conversations and only append new content at the end, so the prefix can be reused.
- Structured output constraints. Place volatile user input at the end to avoid polluting the cacheable region.
- Hit-rate observability. Instrument the application side to track the share of cached tokens and produce reports broken down by business line.
Illustrative local statistics logic:
def summarize(usage: dict) -> dict:total_in = usage["prompt_tokens"]cached = usage.get("cached_tokens", 0)return {"hit_rate": cached / total_in if total_in else 0.0,"billable_in": total_in - cached,"out": usage["completion_tokens"],}
Treating hit_rate as a first-class monitoring metric, alongside latency and error rate, is one of the most effective ways to control cost.
5. Time-of-Day Differences: Folding “Peak Hours” into the Unit Price
Beyond caching, the second commonly missed variable is time of day. Some platforms divide the day into peak and off-peak windows, with higher prices or tighter quotas during peak. The original material mentions a platform with a wide off-peak window and peak compressed to four hours per weekday; such rules directly change the effective unit price.
The correct handling is to introduce a time weight:
Effective_Cost = Σ ( Cost_i * w_i )
where w_i is the share of requests in time window i. If the business is an interactive coding assistant with peak concentrated in working hours, the effective cost will lean clearly toward the peak price; if it is offline batch processing, scheduling jobs into the off-peak window can reduce cost without changing any code.
6. Converting Subscription Plans: Compare Using “Equivalent Original Price”
Subscription plans (a fixed monthly fee with a token or request allowance) cannot be compared directly with pay-as-you-go billing; they need to be converted into an “equivalent original-price API amount”:
Equivalent multiple = amount of in-plan usage priced at original rates / monthly feeDiscount rate = monthly fee / the original-price amount above
The example in the original material: three tiers correspond to usage worth about CNY 160, 955, and 2,229 at original prices, with monthly fees of CNY 118, 538, and 1,078, giving discount rates in the range of roughly 50% to 70%. The value of this conversion is that it turns “the plan looks cheap” into a verifiable ratio, and it can further be translated into actual cost per million tokens, placed on the same axis as the pay-as-you-go option.
Three things to watch when converting: whether the allowance resets weekly or daily, whether unused amounts roll over, and whether peak hours are throttled separately. These three determine the gap between the nominal and usable allowance of a plan.
7. A Reusable Estimation Script
Turning the above logic into a script lets you generate a table in one shot during a selection review:
def cost_per_100m(h, p_in, c_cache, c_in, c_out, peak_ratio=0.0, peak_mult=1.0):p_out = 1 - p_inbase = (h * p_in * c_cache+ (1 - h) * p_in * c_in+ p_out * c_out)return base * 1e8 * ((1 - peak_ratio) + peak_ratio * peak_mult)for h in (0.90, 0.95, 0.98, 0.99):print(h, round(cost_per_100m(h, 0.015, 0.115, 0.5, 2.0), 2))
8. Conclusions and Practical Recommendations
First, do not decide based on unit price; decide based on the effective cost under the triple of hit rate, ratio, and time window. Second, treat hit rate as an optimizable engineering metric; the gains from prefix stability usually outweigh commercial negotiation. Third, putting predictable workloads into a subscription plan and leaving burst and peak workloads to pay-as-you-go is a common optimal cost structure. Fourth, all prices and quota rules change with the official documentation, so the estimation script should make unit prices configuration items for easy recomputation. If an organization already uses model services on Baidu AI Cloud, it is likewise advisable to build a token-level usage dashboard with monitoring, alerting, and logging services, so that cost optimization rests on observable data rather than estimates.
