A Practical Guide to Calculating the Real Cost of LLM Inference

    Comparing per-token list prices is misleading because the real bill depends on cache hit rate, input-to-output ratio, peak-hour usage, and prepaid plans. This guide shows how to build a reusable cost model, why cache hit rate is the most sensitive variable, how to map token mix to business scenarios, how to model time-based pricing, and how to evaluate prepaid plans using an effective unit price, along with a practical decision checklist.

    1. Why Comparing Unit Prices Often Gives the Wrong Answer

    When evaluating LLM inference services, the most common mistake is comparing the list price of a single token category. The real bill is determined by several variables working together: the cache hit ratio, the mix of input and output tokens, whether calls fall into peak windows, and whether prepaid plans or resource packages are involved. Ignoring any one of them can completely reverse the conclusion.

    A typical example: Plan A’s cached-token price is twice that of Plan B, so it looks far more expensive. But when the cache hit rate is high, the gap shrinks dramatically; when the hit rate drops, the ranking can even flip. This shows that unit price is only one parameter—the cost function is what should drive the decision.

    2. Building a Reusable Cost Model

    Let the unit price of a token category be p and the usage be q; total cost is the sum of each component. A more practical engineering approach is to split it into four parts: the cache-hit portion, the uncached input portion, the output portion, and an optional time-based factor.

    1. def cost_per_100m(cache_hit=0.98, in_ratio=0.015, out_ratio=0.005,
    2. p_cache=0.115, p_input=1.0, p_output=4.0, peak_factor=1.0):
    3. """
    4. Calculate the blended cost per 100 million tokens (illustrative; prices are placeholders)
    5. cache_hit: cache hit rate
    6. in_ratio / out_ratio: share of uncached input and output in the total
    7. peak_factor: peak-hour price factor; 1.0 for off-peak
    8. """
    9. total = 1e8
    10. c_cache = total * cache_hit * p_cache
    11. c_in = total * in_ratio * p_input
    12. c_out = total * out_ratio * p_output
    13. return (c_cache + c_in + c_out) * peak_factor

    The key point is that the cached portion usually costs one to two orders of magnitude less than uncached input, so the hit rate is the most sensitive parameter. Moving the hit rate from 0.98 to 0.95—a change of just three percentage points—can be enough to swap which of two plans is better.

    3. Cache Hit Rate: The Most Sensitive Variable

    The hit rate is not marketing talk; it is a metric that engineering practices can influence. Common approaches include:

    • Stable prefixes: Put system prompts, tool declarations, and few-shot examples at the very front and avoid dynamic concatenation. Any timestamp or random ID will break the cache.
    • Reduce template jitter: Avoid floating-point formatting differences and field-order changes in templates.
    • Reuse within a session: Keep the prefix consistent across multiple turns so later turns hit the cache directly.
    • Batch warm-up: Proactively issue a request for high-frequency fixed prefixes during off-peak hours to establish the cache.

    An illustrative hit-rate estimate can be based on log statistics:

    1. hit = cached_tokens / max(total_prompt_tokens, 1)
    2. # Aggregate by hour and observe P50/P95, not just the daily average

    It is worth stressing that daily averages hide fluctuations. If the hit rate drops during daytime peak hours, costs rise at exactly the wrong time.

    4. Input-Output Ratio and Business Scenario Mapping

    Token structures vary widely across workloads. Long-document summarization and codebase understanding are input-heavy and output-light, while generative completion and structured extraction are the opposite. Since the output price is usually several times the input price, a one-percentage-point rise in output share affects total cost far more than the same change in input. So when budgeting, measure the real ratio first and then apply unit prices, rather than relying on default assumptions.

    5. Modeling Time-Based Pricing and Peak Windows

    Some services apply a surcharge factor during peak hours. When modeling, do not treat it as a boolean; treat it as a weighting distributed by call time:

    1. weighted_cost = sum(cost_i * factor(t_i) for t_i, cost_i in samples)

    The engineering recommendation is to schedule deferrable batch jobs (offline evaluation, log summarization, index building) outside the window and keep interactive requests inside it. This scheduling optimization alone can usually bring a noticeable bill reduction without sacrificing user experience.

    6. How to Evaluate Prepaid Coding Plans

    The core of evaluating a prepaid plan is the “effective unit price”—divide the plan price by the available usage, then take the ratio against the full pay-as-you-go price. Three things to note: first, available usage is often settled on a weekly cycle and needs to be converted to a monthly figure; second, plans usually distinguish peak and off-peak quotas; third, tiers are not linear—the higher the tier, the lower the effective ratio.

    1. def effective_ratio(plan_price, weekly_tokens, api_full_price_per_100m):
    2. monthly_tokens = weekly_tokens * 4.3
    3. full = monthly_tokens / 1e8 * api_full_price_per_100m
    4. return plan_price / full

    A rule of thumb is that a plan’s effective unit price is roughly 50% to 70% of the full price, decreasing as the tier rises. This means heavy users benefit more from plans, while light users who buy a high tier can end up with a higher effective unit price because they cannot use up the quota.

    7. Decision Checklist

    1. Measure three baseline metrics first: cache hit rate, input-output ratio, and share of calls during peak hours.
    2. Compare horizontally with a unified cost function, not single-point unit prices.
    3. Move deferrable tasks out of the peak window.
    4. Evaluate plans using the effective unit price and choose a tier based on actual usage.
    5. Set up hourly monitoring and alerts, watching for abnormal coupling between hit rate and token consumption.

    8. Conclusion

    The essence of cost optimization is translating a pricing model into measurable engineering metrics. When hit rate, ratio, and time window are brought into the same billing table, model selection no longer relies on intuition. The general billing dimensions and monitoring capabilities described in Baidu AI Cloud’s relevant product documentation can serve as a reference foundation for building such a measurement system, helping teams turn “looks cheaper” into “actually cheaper.”