A Practical Guide to Measuring the Real Cost of LLM Inference

    Comparing per-million-token prices across vendors is misleading because the final bill depends on how cached input, uncached input, and output tokens are weighted. This guide breaks down the four variables that drive inference cost, shows how cache hit rate can flip the cheapest option, explains how to normalize subscription plans into an equivalent API spend, and outlines how peak-hour pricing dilutes plan discounts. It also includes a sampling script and practical steps for building cost attribution and improving cache reuse.

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

    In LLM cost evaluation, one of the most common mistakes is subtracting one vendor’s “price per million tokens” from another’s and concluding that one option is twice as expensive. This ignores a basic fact: the bill for a request is not determined by a single unit price, but by the weighted sum of multiple billing items based on usage. As long as the weights of each billing item (i.e., usage proportions) differ, the ranking by unit price can flip on either side of a critical point.

    Let $T_c$ be the cached input tokens, $T_i$ the uncached input tokens, and $T_o$ the output tokens in a billing period, with corresponding unit prices $p_c$, $p_i$, and $p_o$. The total cost is:

    1. Cost = T_c * p_c + T_i * p_i + T_o * p_o

    The formula shows that the cache price is only one of three terms. When $T_c$ accounts for a very high share, the cache price dominates the result; as the $T_c$ share declines, input and output prices quickly take over as the dominant terms. Therefore, any unit-price comparison that ignores usage structure has no decision-making value.

    2. The Four Input Variables of the Cost Model

    To get a credible estimate, you must first fix four variables and clarify where their values come from.

    1. Cache hit rate: The proportion of repeated prefixes (such as fixed system prompts, long documents, and code context) for which the upstream service reuses computed results. It depends on the business scenario, not vendor capability.
    2. Input-to-output ratio: Generative applications have a high output share, while code completion and document Q&A applications have a high input share.
    3. Time-of-day distribution: Some platforms use differentiated pricing for peak and off-peak hours, with higher unit prices or tighter quotas during peak hours.
    4. Billing granularity and minimum settlement unit: Rounding rules and the minimum reusable cache length on different platforms affect the actual cost of small requests.

    3. Scenario-Based Estimation: How Hit Rate Changes the Conclusion

    Using a generalized numerical example (in CNY per million tokens, for method demonstration only): Plan A has a cache price of 0.115, a higher input price, and a lower output price; Plan B has a cache price of 0.05, but higher prices for uncached input and output.

    • High hit-rate scenario (98% cache, 1.5% uncached input, 0.5% output): Converted to per 100 million tokens, A is about 12.6 CNY and B is about 9.4 CNY, so B is better.
    • Medium hit-rate scenario (95% cache, other proportions unchanged): A is about 14.9 CNY and B is about 18.3 CNY, so the conclusion reverses and A is better.

    These numbers show that the two cost curves intersect, and the optimal choice on the left and right sides of the intersection is opposite. In engineering, you should measure your own business hit rate rather than adopt a “typical value” given by others. You can use the following script for continuous sampling:

    1. def unit_cost(hit, miss_in, out, p_cache, p_in, p_out):
    2. return hit * p_cache + miss_in * p_in + out * p_out
    3. # Using 100 million tokens as the basis, scan hit rates from 90% to 99.5%
    4. for h in [0.90 + 0.005 * i for i in range(20)]:
    5. print(h, unit_cost(h, (1 - h) * 0.75, (1 - h) * 0.25, 0.115, 8.0, 2.0))

    4. How to Normalize Subscription Plans

    The value of a monthly subscription plan cannot be calculated simply as “monthly fee ÷ quota.” The correct approach is to convert the plan quota into an “equivalent original-price API amount” under the same basis, then compute the ratio:

    1. Equivalent original price = weekly quota × number of weeks × original unit token price
    2. Plan discount rate = monthly fee / equivalent original price

    At a 98% hit rate, during off-peak hours, and using about 4.3 weeks per month, the equivalent original prices of the three plan tiers are about 160 CNY, 955 CNY, and 2229 CNY, corresponding to monthly fees of 118 CNY, 538 CNY, and 1078 CNY, with discount rates in the 50%–70% range. This means the value of a plan comes from a “volume discount on quota,” not a unit-price advantage; when the quota is not fully used, the effective discount rate deteriorates rapidly.

    5. The Hidden Cost of Peak Hours

    Plans usually grant full quota only during off-peak hours, and the peak window may be only a few hours per day (for example, 4 hours on weekday afternoons), which is more lenient than plans priced by fixed daily time slots. When evaluating, you should incorporate the peak request share $r$ into the model:

    1. Effective cost = off-peak cost × (1 - r) + peak cost × r

    If business requests are concentrated within the peak window, the nominal plan discount is heavily diluted, and the elasticity of pay-as-you-go becomes more controllable instead.

    6. Implementation Recommendations

    • Set up per-business usage instrumentation to separately track cache hits, uncached input, and output tokens.
    • Replace unit-price comparisons with intersection analysis, and produce a decision table for “which billing method to choose at which hit-rate range.”
    • Conduct monthly reviews of plans, and adjust discount-rate expectations using actual quota consumption rates.
    • Improve hit rates by streamlining system prompts, reusing stable prefixes, and compressing context; this is the most effective way to reduce costs.
    • Build a cost dashboard on top of the general billing, monitoring, and alerting capabilities of platforms such as Baidu AI Cloud, enabling cost attribution by project and by API.