How to Calculate the Real Cost of LLM Inference: A Practical Guide

    Comparing per-token list prices across LLM providers almost always leads to the wrong conclusion. This guide explains why cache hit rate, input/output ratio, and peak/off-peak timing dominate your actual bill, provides a reusable blended-cost formula and Python function, shows how to normalize subscription plans into an effective unit price, and lists practical levers for reducing inference cost.

    1. Why “Unit Price Comparisons” Almost Always Lead to the Wrong Conclusion

    As large model applications move into large-scale production, cost discussions have overtaken capability discussions. A common pattern is to put two providers’ cached-input prices side by side and conclude that one is “more than twice as expensive.” That comparison is unreliable because the cache price is only one component of the bill. The real shape of a bill is a weighted sum:

    1. unit cost = cache hit rate × cache price
    2. + input ratio × input price
    3. + output ratio × output price

    If any one of the cache hit rate, the input/output mix, or the time of day of your calls changes, the ranking can flip. The right approach is therefore not to compare “unit prices,” but to compare the weighted unit price under your own workload.

    2. Three Variables That Decide Your Bill

    2.1 Cache Hit Rate: The First Lever on the Cost Curve

    Prefix caching is billed such that the hit portion is priced significantly lower than the miss portion, usually by an order of magnitude. A change of just a few percentage points in the hit rate can therefore move total cost non-linearly. The hit rate is determined by the shape of your traffic: scenarios with a fixed system prompt, stable tool definitions, and multi-turn conversations that share a long prefix see high hit rates, while scenarios that concatenate a timestamp, a random identifier, or long personalized user text into every request push the hit rate very low.

    2.2 Input/Output Ratio: The Overlooked Amplifier

    The output price is typically several times the input price. For workloads centered on long-document summarization, code generation, or structured extraction, the output share can far exceed intuition. If you estimate costs using the default assumption that “input greatly outweighs output,” the error is amplified several times over.

    2.3 Peak and Off-Peak Windows: The Dividing Line Between Plans and Pay-as-You-Go

    Some providers price compute by time of day, with off-peak unit prices clearly lower. Coding-oriented plans also define “peak hours” differently: some count only a few hours per day, others count longer stretches. The narrower the peak window, the closer a plan’s usable capacity is to its nominal value.

    3. A Reusable Calculation Method

    Fix the variables above into a single function, and any quote can be converted into a comparable number in seconds:

    1. def blended_cost(cache_hit, in_ratio, out_ratio,
    2. p_cache, p_in, p_out, total=100_000_000):
    3. assert abs(cache_hit + in_ratio + out_ratio - 1) < 1e-6
    4. per_token = (cache_hit * p_cache
    5. + in_ratio * p_in
    6. + out_ratio * p_out)
    7. return per_token * total / 1_000_000

    Take a magnitude like “cached-input price 0.115 yuan, off-peak input price 0.05 yuan.” At a high hit rate (around 98%), the two options cost roughly 12.6 yuan and 9.4 yuan per 100 million tokens respectively, so the low-hit-rate option wins. When the hit rate falls back to around 95%, the result reverses to roughly 14.9 yuan and 18.3 yuan, and the option with the higher cache price becomes the better deal. The conclusion is clear: the level of the cache unit price alone cannot decide which option is better; the hit rate is the dominant variable in the ranking.

    4. How to Normalize Subscription Capacity

    The key to evaluating a plan is to divide the monthly fee by “nominal capacity × time-window discount × hit rate” to get an effective unit price, then compare it with the weighted unit price of pay-as-you-go. The empirical range is that a plan’s effective unit price is about 50% to 70% of the original API price, depending on the tier and time-window constraints. When converting, be sure to confirm three things: whether the quota resets weekly or monthly, the exact definition of the peak window, and how usage beyond the quota is billed.

    5. Optimization Checklist

    • Stabilize the prefix: place the system prompt, tool descriptions, and few-shot examples at the front and keep them fixed; avoid inserting random content.
    • Control output length: cap the maximum generation length and trim redundant explanations.
    • Tier your tasks: route simple requests to smaller models and escalate only complex ones.
    • Batch and defer: schedule deferrable jobs into off-peak windows.
    • Build observability: use logging services and monitoring alerts to track tokens and hit rates per business dimension, so the bill is attributable.

    6. Conclusion

    The essence of cost engineering is turning “unit price comparison” into “workload modeling.” Quantify your own hit rate, input/output structure, and time-of-day distribution first, then plug them into a unified formula; only then can you reach conclusions that are stable, reproducible, and actionable. If you need an integrated approach spanning collection, calculation, and dashboards, you can look at how Baidu AI Cloud combines general capabilities such as model services, logging, and monitoring alerts.