How to Calculate the Real Cost of LLM Inference

    A practical guide to estimating real LLM inference costs beyond per-token pricing, covering a reusable unit cost formula, cache hit rate sensitivity analysis, peak-hour scheduling, and subscription plan conversion.

    1. Why a Low Unit Price Doesn’t Mean a Low Bill

    When evaluating LLM application costs, 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 within peak-hour windows, and the billing granularity (displayed per million tokens or per thousand tokens). The same model can have a unit cost that differs by several times across different business scenarios.

    Typical scenarios vary widely. Code completion applications have highly repetitive context and often achieve a high cache hit rate, but produce very short outputs. Document summarization applications take long inputs and produce medium-length outputs. Multi-turn tool-calling agents accumulate input continuously while output grows step by step. For this reason, any conclusion about “which is cheaper” that ignores the business profile does not hold. The correct approach is to build your own cost model first.

    2. Building a Reusable Unit Cost Formula

    Let the cache price be P_cache, the non-cached input price be P_in, the output price be P_out, the cache hit rate be h, the input share be a, and the output share be b (a + b + h = 1). The cost per million tokens is:

    1. Cost_per_1M = h * P_cache + a * P_in + b * P_out

    Take a high-reuse profile of “98% cache hit + 1.5% non-cached input + 0.5% output” as an example. The cache term almost dominates the total price, and the gap in total cost between two options is compressed into a very small absolute range. When the hit rate drops to around 95%, the non-cached input term is amplified by more than 3x, and the ranking may completely reverse.

    The conclusion is clear: the option with the lower cache price wins in high-hit-rate scenarios, while the option with the lower non-cached input price wins in medium-to-low hit-rate scenarios. Before making a selection, you must measure the h value from real logs rather than adopting the “best case” advertised by vendors.

    3. Hit Rate Sensitivity Analysis: Finding the Break-Even Point

    It is recommended to scan h as the only independent variable, output a cost curve, and find the intersection of the two curves, which is the break-even hit rate:

    1. def cost(p_cache, p_in, p_out, h, a=0.015, b=0.005):
    2. return h * p_cache + a * p_in + b * p_out
    3. for h in [0.90, 0.95, 0.98, 0.99]:
    4. print(h, round(cost(0.115, 3.0, 9.0, h), 3))

    Engineering techniques for improving h include: keeping the System Prompt and tool descriptions in a stable order, placing variable content at the end, using a fixed prefix for long documents, and reusing session history instead of repeatedly concatenating it. These optimizations can usually raise the hit rate from 0.9 to above 0.95, and the payoff is often greater than switching providers.

    4. Peak Hours and Subscription Plan Conversion

    Some platforms apply different policies during peak hours (for example, several hours in the afternoon on workdays). The narrower the peak window, the more flexible task scheduling becomes. Workloads with no latency requirements, such as batch jobs, offline evaluation, and log analysis, can be shifted off-peak through a queue.

    Subscription-based billing needs to be converted into an equivalent original-price API quota:

    1. Equivalent multiplier = tokens available in the plan * original unit price / monthly plan fee
    2. Discount rate = monthly plan fee / equivalent original price

    Within common ranges, the subscription discount rate mostly falls between 50% and 70%, equivalent to 50%–70% of the original price. Whether it is worthwhile depends on whether monthly usage is stable: when usage fluctuates significantly, pay-as-you-go is more economical; when usage is steady and predictable, a subscription is better.

    5. Implementation Recommendations

    1) First, measure the three metrics h, a, and b from real call logs; 2) compare options horizontally with a unified formula rather than comparing single list prices; 3) treat cache-friendly refactoring as the first-priority optimization; 4) use a queue to shift load off-peak; 5) set up monitoring alerts and budget thresholds for cost, and tag and aggregate data by business dimension in your logging and alerting systems. Relevant general-purpose capabilities from Baidu AI Cloud (such as container platforms, message queues, logging services, and monitoring alerts) can serve as infrastructure references for carrying out the cost governance process described above.