LLM Inference Cost Calculation and Plan Selection: A Practical Guide
Comparing per-million-token prices across providers is a common but unreliable way to choose an LLM service, because the real bill is a product of several variables rather than a single rate. This article breaks down the three decisive factors—context cache hit rate, input/output token ratio, and peak-hour pricing—shows how to model expected cost per million tokens, provides a reusable Python script for cost and sensitivity analysis, and explains how to convert subscription plans into an effective unit price. It also includes a practical decision checklist for matching billing models to workload patterns.
1. Why “unit price comparison” almost always leads to the wrong conclusion
During the evaluation stage of an LLM application, teams most often compare the per-million-token price of different service plans and simply pick the lower number. This approach does not hold up in engineering terms: an inference bill is not a function of a single unit price, but a product of multiple variables.
The same model service can cost several times more in one business scenario than in another, and the reasons usually come down to three things: the hit ratio of context caching, the structural ratio of input to output tokens, and whether requests fall within a provider’s discounted hours. Any conclusion based only on “cache price” or “output price” implicitly assumes the other two variables are identical—something that almost never happens with real traffic.
So the right starting point for cost evaluation is not the price sheet, but quantifying your own traffic profile first.
2. Three decisive variables
2.1 Cache hit rate: the first lever on the cost curve
Mainstream inference services generally offer context caching: when multiple requests share the same prefix (system prompts, long documents, codebase context, few-shot examples), those tokens are billed at a price significantly lower than the regular input rate.
The key point is that the gap between the cache price and the regular input price is often larger than the price difference between providers. This means raising the hit rate from 95% to 98% can reduce cost more than switching to a cheaper provider. The hit rate is determined by the shape of your workload, not by the pricing page:
- High-hit scenarios: a fixed system prompt plus repeated Q&A over long documents; code completion (the same repository context reused frequently).
- Low-hit scenarios: every request concatenates different user-uploaded material, timestamps, or random session identifiers.
A common and entirely avoidable problem is a “polluted” cache prefix: putting timestamps, request IDs, or randomly ordered fields at the very front of the prompt makes the prefix different every time and invalidates the cache entirely.
# Bad example: volatile content first, unstable prefix, hard to hit cacheprompt_bad = f"Current time {now()}, request {uuid4()}\n" + SYSTEM + user_input# Good example: stable content first, volatile content lastprompt_good = SYSTEM + STATIC_DOC + f"\nCurrent time: {now()}\n" + user_input
In practice, split the prompt into a “stable section” and a “volatile section.” The earlier, longer, and more fixed the stable section is, the higher the hit rate.
2.2 Input/output ratio: which side of the curve you land on
Let the cache hit rate be h, the input share be a, the output share be b, the cache unit price be p_c, the regular input unit price be p_i, and the output unit price be p_o. The expected cost per million tokens is:
cost_per_1M = a * (h * p_c + (1 - h) * p_i) + b * p_o
Plugging in two typical parameter sets (for illustration only, not representing any specific pricing): when h = 0.98, input accounts for 1.5%, and output accounts for 0.5%, cost is almost entirely dominated by the cache term and the gap between providers is compressed; when h drops to 0.95, the uncached portion of regular input is magnified 3x, the cost structure reverses quickly, and the originally “expensive” plan turns out to be cheaper.
The conclusion is clear: plans with a low cache price win in high-hit scenarios, while plans with a low regular input price win in low-to-medium-hit scenarios. Selection must use your own h value, not someone else’s conclusion.
2.3 Peak hours: the easily overlooked multiplier
Some services apply a higher unit price during a fixed “peak window” of a few hours each day, with discounted prices outside it. Differences in window length (for example, 4 hours a day versus a longer window) directly change the monthly cost, and batch workloads are especially sensitive to this. The engineering countermeasure is to schedule deferrable batch jobs (offline summarization, batch labeling, nightly regression tests) outside the window and keep interactive requests inside it.
3. A reusable calculation script
def cost_per_1m(p_cache, p_in, p_out, hit, in_ratio, out_ratio):return in_ratio * (hit * p_cache + (1 - hit) * p_in) + out_ratio * p_outdef monthly_cost(qps, hit, peak_hours=4, peak_mult=1.0, **price):seconds = 86400 * 30off_ratio = 1 - peak_hours / 24base = cost_per_1m(**price, hit=hit) * qps * seconds * off_ratiopeak = cost_per_1m(**price, hit=hit) * qps * seconds * (peak_hours / 24) * peak_multreturn (base + peak) / 1e6
It is a good idea to treat h as a random variable in a sensitivity analysis: compute at 0.90 / 0.95 / 0.98 separately and observe the range in which the conclusion flips. That reversal point is the real dividing line for selection.
4. How to convert subscription plans
Subscription plans (monthly fee, included token quota, peak and off-peak distinction) cannot be judged by the sticker price alone; they need to be converted into an effective unit price:
Effective unit price = monthly fee / total tokens available that monthDiscount rate = effective unit price / pay-as-you-go price under the same conditions
Measured this way, common plans land at roughly 50% to 70% of the pay-as-you-go price—a discount of around 40%. But the discount is not everything; three more things matter: whether the quota resets monthly, how overage is billed, and whether the peak window eats into your main business hours. If your workload happens to concentrate in the peak window, the plan’s effective discount is noticeably diluted.
5. Decision checklist
- Instrument your traffic to measure the real h, input/output ratio, and time-of-day distribution before discussing selection.
- Use the formula above to compute three h values and find the cost reversal point.
- For long-term workloads with stable QPS and predictable usage, prefer a subscription plan; for highly variable or unpredictable peaks, keep pay-as-you-go.
- Continuously optimize prompt structure and compress redundant context to raise the hit rate—this usually pays off more than switching providers.
- Build a monthly cost dashboard with alerts on cache hit rate and cost per token.
If you need a complete cost-observability chain spanning model services, container platforms, log services, and monitoring alerts, Baidu AI Cloud’s related products can cover every step from call metering to cost attribution, making it easier to put the calculations above into daily operations.
