How to Calculate the Real Cost of Large Model APIs: A Practical Guide
Comparing API prices per million tokens often leads to the wrong conclusion. This guide breaks down the three variables that actually drive your bill—cache hit rate, input-to-output token ratio, and peak-hour pricing—and shows how to model them with a reusable formula and script. You will also learn how to evaluate subscription plans against pay-as-you-go pricing and build a cost dashboard that attributes spending to specific business lines.
1. Why “Price-Per-Token Comparisons” Almost Always Lead to the Wrong Conclusion
In discussions about choosing a large model API, the most common argument is: “Provider X is cheaper per million tokens, so it saves more money.” This comparison is unreliable because your bill is never a linear function of unit price. It is the result of three interacting variables: cache hit rate, the ratio of input to output tokens, and whether requests occur during peak or off-peak hours. A change in any one of them can flip the ranking.
Writing cost as a formula makes this clearer. Let the cache hit rate be h, the input share be a, the output share be b, the cached-input unit price be p_c, the cache-miss input unit price be p_i, and the output unit price be p_o. The expected cost per million tokens is approximately:
cost_per_1M = a * (h * p_c + (1 - h) * p_i) + b * p_o
This equation shows that the cached-input price only dominates the bill when h is high enough. Once h drops, the cache-miss input term grows quickly, offsetting—or even reversing—the advantage of cheaper cached input.
2. Variable One: Cache Hit Rate Determines the Ranking
Consider two common cached-input price tiers: one around 0.1 per million tokens and another around 0.05 per million tokens. The first looks more than twice as expensive. But plugging the numbers into the formula does not yield a single answer.
- When
h = 98%, input is 1.5%, and output is 0.5%, the cost per 100 million tokens is roughly 12.6 and 9.4 for the two tiers respectively. The cheaper-cache option wins. - When
hdrops to 95% with the other ratios unchanged, the result flips to roughly 14.9 and 18.3. The higher-priced cache option is now more economical.
The reason for the reversal is straightforward: the difference in cached-input price is amplified by h, while the difference in cache-miss price is amplified by (1 - h). When h falls from 98% to 95%, the miss share rises from 2% to 5%—a 2.5x increase—and the cache-miss price becomes the dominant term in the bill. The engineering takeaway: do not treat the cached-input price on a marketing page as a decision criterion. First measure the distribution of h under your own real traffic.
3. Variable Two: Input/Output Ratio and Business Type
The values of a and b are determined by the type of workload, and the differences can span an order of magnitude:
- Long-context Q&A and code-base retrieval augmentation: input far exceeds output, so caching yields significant savings.
- Batch summarization and structured extraction: input and output are relatively balanced.
- Content generation and conversational writing: output dominates, so the output unit price carries the most weight and caching barely affects the bill.
The same price sheet applied to different workloads can therefore produce completely opposite optimal choices. During load testing, define three to five typical request profiles and calculate costs for each, rather than smoothing everything over with a single “average.”
4. Variable Three: Peak Hours and Capacity Constraints
Many platforms apply different pricing or rate-limiting policies during peak hours. The definition of the peak window varies widely: some count four hours per weekday (for example, a fixed afternoon block), while others cover a longer weekday range. The shorter the window, the larger the off-peak token pool, and the more usage a given monthly fee can support. When budgeting, convert “peak window duration × number of weekdays” into monthly available capacity; otherwise you will underestimate spike costs.
5. How to Evaluate Subscription Plans
Take a subscription tier for an AI coding tool as an example. The monthly fee falls into three tiers (roughly 100, 500, and 1,000 in local currency), with official off-peak weekly quotas of approximately 300 million, 1.9 billion, and 4.4 billion tokens. The conversion is:
equivalent API value = weekly quota * 4.33 * price per 1M tokens / 1e6plan discount rate = monthly fee / equivalent API value
Assuming a 98% cache hit rate, the equivalent API values for the three tiers are about 160, 955, and 2,229, with discount rates landing in the 50%–70% range. The real benefit of a subscription is “50% to 70% off,” not the vague claim of “unlimited.” If your weekly usage is below the lowest tier’s quota, a subscription is not worth it. If you consistently exceed the highest tier, evaluate overage charges and rate-limiting policies.
6. A Reusable Calculation Script
def cost_per_1m(h, a, b, p_cache, p_miss, p_out):return a * (h * p_cache + (1 - h) * p_miss) + b * p_outdef plan_value(weekly_tokens, price_per_1m, weeks=4.33):return weekly_tokens * weeks * price_per_1m / 1_000_000# Example: comparing two price sheets at a 95% hit rateprint(cost_per_1m(0.95, 0.015, 0.005, 0.115, 3.0, 8.0))
7. Practical Recommendations
- Instrument your system to measure the real
h,a,b, and the share of peak-hour requests before discussing vendor selection. - Run the same script on candidate options to perform sensitivity analysis and identify the range of
hwhere the ranking flips. - Treat cache hit rate as an optimizable engineering metric: stable prefixes, fixed system prompts, and disciplined session reuse all improve
h. - Keep both pay-as-you-go and subscription paths in your budget, and assign usage based on actual monthly volume.
- Build a cost dashboard on top of general-purpose platform capabilities such as monitoring and alerting, logging, and object storage—for example, on Baidu AI Cloud—so that bills are attributable to business lines rather than reconciled once at month-end.
