LLM Inference Cost Calculation and Plan Selection: A Practical Guide
Comparing model service prices by unit rate alone often leads to wrong conclusions. This article shows how to build a weighted cost model based on cache hit rate, input/output ratio, and per-unit pricing, explains why a few percentage points of cache hit rate can swing total cost by more than 50%, and covers how to convert peak/off-peak pricing and monthly coding plans into comparable effective unit prices. It also includes a reusable Python estimation script and a six-step selection checklist.
1. Why Unit Price Comparisons Lead to Wrong Conclusions
When comparing two model services, the most common approach is to put their cached input unit prices side by side, look at the ratio, and conclude that one provider is “twice as expensive.” The problem is that this treats cost structure as a single number, when the actual bill is a weighted sum of several billing items.
A request’s input is usually split into two parts: the prefix that hits the cache, and the newly added portion that misses. Output is billed separately, and its unit price is generally much higher than input. So the real cost is approximately:
Cost per 100M tokens = hit rate × cached input price+ (1 - hit rate) × standard input price+ output ratio × output price
As long as there is a clear gap between the cached input price and the standard input price, the hit rate becomes the variable with the largest weight. A cached price that is half as low does not mean total cost is half as low; conversely, a slightly higher cached price can be amortized to near zero at a high hit rate.
2. Cache Hit Rate: The Highest-Weight Variable in the Cost Model
Plugging two typical scenarios into the formula above shows the conclusion reversing:
- High hit rate scenario (about 98% hit, input at 1.5%, output at 0.5%): the two options cost about CNY 12.6 and CNY 9.4 per 100M tokens respectively, and the one with the lower cached price wins.
- Medium hit rate scenario (about 95% hit): the two options cost about CNY 14.9 and CNY 18.3 respectively, and the one with a slightly higher cached price but cheaper standard input is actually more economical.
The engineering implication is clear: a drop in hit rate from 98% to 95%, just 3 percentage points, is enough to make total cost differ by more than 50%. The reason is that the missed portion is billed at the standard input price, which is typically several times, or even more than ten times, the cached price.
So the first priority in cost optimization is not picking a unit price, but improving prefix stability: keep the order of system prompts and tool declarations fixed, place dynamic content at the end, avoid inserting timestamps or random identifiers into the request prefix, and use a stable chunking strategy for long documents. These are general engineering practices that work for any service supporting prefix caching.
3. Plugging in Your Usage Structure: A Reusable Estimation Script
It is worth turning the estimation into a script instead of doing mental math every time:
def cost_per_100m(cache_price, input_price, output_price,hit=0.98, in_ratio=0.015, out_ratio=0.005):return (hit * cache_price+ (1 - hit) * input_price+ out_ratio * output_price) * 1e8 / 1e6
When using it, do three things: first, set the hit rate as a range (such as 0.90/0.95/0.98) for sensitivity analysis; second, calibrate the input/output ratios against real business data, since long-context summarization tasks often have a higher output ratio; third, convert the result into “cost per 10,000 requests,” which is closer to the business view than cost per 100M tokens.
4. Converting Peak/Off-Peak Pricing and Coding Plans
Beyond pay-as-you-go billing, two other mechanisms are common in the industry: time-based (peak/off-peak) pricing, where unit prices are lower during off-peak hours; and monthly subscription plans for AI coding tools, converted from the quota available within a billing cycle.
The right way to evaluate a plan is to convert it to an effective unit price, not to look at the absolute monthly fee:
Effective unit price = monthly fee ÷ total tokens available per cyclePlan discount vs. API = effective unit price ÷ pay-as-you-go price in the same period
A set of example figures from public sources: one plan tier at CNY 118 per month corresponds to about 317M tokens per week, or roughly CNY 160 of standard API quota; the CNY 538 tier corresponds to about 1.9B tokens per week, or about CNY 955 of quota; the CNY 1078 tier corresponds to about 4.433B tokens per week, or about CNY 2229 of quota. After conversion, plan unit prices land roughly in the 50%–70% range of the pay-as-you-go price.
The definition of peak hours matters just as much. Some platforms limit peak to 4 hours per workday, while others cover a longer window; the shorter the peak window, the lower the chance that a plan gets throttled, and the closer the usable quota is to the stated value.
5. Selection Checklist
- Measure the hit rate first: run real traffic for a week, measure the prefix reuse ratio, and only then discuss unit prices.
- Build a sensitivity table: take three levels each for hit rate and input/output ratio to form a 9-cell cost matrix.
- Define the peak window: write the overlap between business peaks and platform peaks into the evaluation.
- Judge plans by conversion: use the effective unit price and discount range, not the monthly fee.
- Keep flexibility: prefer pay-as-you-go for volatile, unpredictable-peak workloads, and plans for stable long-running tasks.
- Pair it with observability: use logging services and monitoring alerts to build a dashboard for tokens and cache hits, so cost is attributable.
In related practice at Baidu AI Cloud, these methods map directly onto general product categories: use a logging service to collect call details, use monitoring alerts to set thresholds on token consumption and hit rate, and use a container platform to run batch inference tasks so they fit off-peak windows, moving cost governance forward from monthly reconciliation to real-time control.
