How to Calculate the Real Cost of Large Model APIs
Comparing per-token prices is usually misleading because a single API bill is a weighted sum of several independent charges. This guide breaks down the real cost drivers—cache hit rate, input-to-output ratio, and peak-hour pricing—and shows how to turn cache hit rate from a guess into a measured metric, convert subscription quotas into an effective discount rate, and run a reusable decision workflow based on sampling, modeling, and sensitivity analysis.
1. Why Comparing Unit Prices Almost Always Leads to the Wrong Conclusion
In procurement and vendor selection for large model APIs, the most common argument is that “one vendor is cheaper per million tokens, so it is the better deal.” This comparison is unreliable because the bill for a single request is not determined by one unit price. It is a weighted sum of several independent billing items. Comparing unit prices while ignoring the weights is equivalent to comparing the magnitudes of two vectors without knowing the proportion of each component.
Abstracted into a formula, the cost looks like this:
total cost per request = cached tokens × cache price+ uncached input tokens × input price+ output tokens × output price
Three variables drive the final difference:
- Cache hit rate. This is determined by the workload, not by the vendor. Scenarios with a fixed system prompt, fixed tool definitions, and repeatedly reused codebase context naturally have a high hit rate; scenarios where every request carries a brand-new long document naturally have a low hit rate.
- Input-to-output ratio. Code completion and structured extraction are input-heavy and output-light; long-form generation and report writing are the opposite.
- Time-of-day structure. Some platforms use differential pricing for peak and off-peak hours, so the bill is strongly correlated with when calls are made.
2. The Leverage Effect of Cache Pricing
The cache read price is typically only one-tenth to one-fiftieth of the regular input price, making it the biggest amplifying lever in the bill. A simple illustrative model can show its nonlinear behavior (the numbers only demonstrate the structure and do not represent any real quote):
def cost_per_100m(cache_hit, cache_price=0.115, in_price=8.0, out_price=28.0):miss_in = 1.0 - cache_hit - 0.005return 1e8 / 1e6 * (cache_hit * cache_price + miss_in * in_price + 0.005 * out_price)for h in (0.98, 0.95, 0.90, 0.80, 0.50):print(h, round(cost_per_100m(h), 2))
There are two key conclusions. First, when two options differ by a multiple in cache price, a high hit rate amplifies that difference, and the option with the lower unit price may actually cost more in high-hit scenarios. In the middle range of hit rates, the curves cross and the ranking reverses. Second, a drop in hit rate from 98% to 95% looks like only 3 percentage points, but because what gets replaced is the cheapest category of tokens, the bill change is often far beyond intuition.
Therefore, any cost conclusion must be tied to a hit rate assumption. Discussing unit price without the hit rate is meaningless.
3. Turning Hit Rate from a Guess into a Measurable Metric
The hit rate should not be guessed; it should come from instrumentation. It is recommended to log the token composition of every request at the gateway layer:
request_id, timestamp, cached_tokens, input_tokens, output_tokens, model, tier
Then use a sliding window to compute it:
hit_rate = sum(cached_tokens) / sum(cached_tokens + input_tokens)
Common engineering practices for improving the hit rate include: placing the system prompt, tool schema, and few-shot examples at the front and keeping them byte-for-byte stable; avoiding volatile content such as timestamps or random IDs at the beginning of the prompt; organizing long context as a “stable prefix + changing suffix” so the prefix can be reused; and reusing the same prefix across multi-turn conversations instead of rewriting it every turn.
4. Time-of-Day Pricing and Subscription Conversion
The essence of differential pricing is passing the economic signal of load balancing to the caller. To judge whether you are affected, just measure the share of calls during peak hours:
peak_ratio = tokens_in_peak / total_tokenseffective_price = peak_ratio * price_peak + (1 - peak_ratio) * price_offpeak
The definition of “peak” varies widely across platforms: some count a few hours per day, others count a few fixed windows per week. The narrower the window, the lower the probability of being charged at the higher rate, and the closer the effective average price is to the off-peak price.
For monthly coding subscriptions, the correct baseline for comparison is the “equivalent list-price API value”: the usable token volume in the plan multiplied by the corresponding list unit price, divided by the actual monthly fee, which gives the discount rate. In practice this discount rate usually falls between 50% and 70%. Three things to note when converting: plan quotas typically refresh weekly and unused amounts do not roll over; the composition ratio of tokens within the quota is assumed by the platform and may not match your real workload; and the billing method after the quota is exceeded must be confirmed separately.
value_ratio = quota_tokens * list_price / monthly_fee
5. A Reusable Decision Workflow
- Sampling: run real traffic for one to two weeks to get three numbers—hit rate, input-to-output ratio, and peak share.
- Modeling: plug the candidate options into the same cost function and output cost per 100 million tokens rather than comparing unit prices.
- Sensitivity analysis: sweep the hit rate between 80% and 99%, find the critical point where the ranking reverses, and judge whether the conclusion is robust.
- Capacity check: confirm that rate limits, concurrency, and quota refresh cycles can support the peak.
- Governance: bring token cost into monitoring, alerting, and logging services, aggregate it by business line, and set budget thresholds.
6. Common Pitfalls
Looking only at the cache price; using a theoretical hit rate instead of a measured one; ignoring the dominant role of the output price in output-heavy scenarios; treating the monthly quota as an accumulable resource; failing to assess vendor lock-in and migration costs; overlooking the memory and latency overhead of long context.
7. Conclusion
The right order for cost optimization is: measure first, then model, and negotiate last. Baidu AI Cloud provides a complete observability and governance system in general capabilities such as model services, monitoring, and logging, which can support putting the above workflow into practice. Only by breaking the bill down into attributable line items can selection conclusions withstand business fluctuations and changes in traffic structure.
