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

    Comparing per-million-token list prices between LLM providers almost always leads to the wrong conclusion. This guide shows how to build an effective price model that accounts for cache hit ratio and input/output mix, solve for the break-even hit rate that determines provider selection, measure and improve hit rates in practice, factor peak-hour capacity constraints into the model, and convert subscription plans into an equivalent discount multiple. It closes with a cost governance checklist for keeping token metering, alerting, and capacity reviews consistent.

    1. Why “price-per-token comparison” almost always leads to the wrong conclusion

    When evaluating LLM inference costs, the most common approach is to put two providers’ “price per million tokens” side by side and compare the numbers. The problem is that this compresses a multi-variable engineering problem into a one-dimensional comparison. A real bill is determined by at least four variables:

    1. Cache hit ratio: the price of cached input tokens and uncached input tokens can differ by an order of magnitude;
    2. Input-to-output mix: output tokens usually cost significantly more than input tokens, so a shift in the mix moves the blended average price;
    3. Time-of-day structure: some services apply different pricing or different quota policies during peak and off-peak hours;
    4. Billing granularity and minimum billing unit: whether short requests, streaming chunks, and failed retries are billed repeatedly.

    The correct approach, therefore, is not to compare list prices, but to first build an “effective price” model and then plug in a profile of your own workload.

    2. Effective price: putting cache and mix into a single formula

    Let the cache hit ratio be h, the input token share be p_in, the output token share be p_out, the cached-input price be c_hit, the uncached-input price be c_miss, and the output price be c_out. The effective price per million tokens is then:

    1. def effective_price(c_hit, c_miss, c_out, h, p_in, p_out):
    2. """Return the effective cost per million tokens (in the same currency unit)"""
    3. assert abs(p_in + p_out - 1.0) < 1e-6
    4. input_price = h * c_hit + (1 - h) * c_miss
    5. return p_in * input_price + p_out * c_out

    This formula reveals what that set of numbers in the original discussion really means: when h = 98%, input accounts for 1.5%, and output accounts for 0.5%, the cost is almost entirely dominated by the cache price, and the provider with the lower list price wins. But when h drops to 95%, the weight of the uncached portion (counted at 5%) is amplified 2.5x, and the provider with a slightly higher cache price but a lower input price pulls ahead instead. In other words, the ranking is not absolute; it is a piecewise function of h.

    3. Break-even hit rate: turning “who is cheaper” into a solvable equation

    The more valuable engineering output is not a comparison at a single point, but the critical point. Set the two effective prices equal and solve for h*:

    1. p_in * [h*c_hit_A + (1-h)*c_miss_A] + p_out*c_out_A
    2. = p_in * [h*c_hit_B + (1-h)*c_miss_B] + p_out*c_out_B

    After simplification, h depends only on the two sets of prices and the mix. Once you have h, the selection rule becomes very clear: if the measured hit rate is above h, choose A; if it is below h, choose B. It is advisable to make measuring h a standing instrumentation practice rather than a guess, because the hit rate is extremely sensitive to prompt structure, how system prompts are reused, and the distribution of session lengths.

    4. Hit rate is not a constant: how to measure and improve it for real

    The hit rate is essentially a statistic determined by your call pattern. Actionable ways to improve it include: placing stable system prompts and tool descriptions at the front and keeping them byte-for-byte unchanged; avoiding volatile fields such as timestamps, random IDs, and user nicknames in the prompt prefix; splitting long documents into reusable chunks and ordering them by access frequency; and, for multi-turn sessions, appending to the prefix monotonically rather than rewriting it.

    For measurement, it is recommended to record three quantities at the same time: cached tokens, uncached tokens, and billed retry tokens. Looking only at the “average hit rate” hides the long tail; observing buckets by P50/P90 is more instructive.

    1. hit_ratio = cached_tokens / (cached_tokens + uncached_tokens)
    2. effective_hit = cached_tokens / (cached_tokens + uncached_tokens + retry_tokens)

    5. Time-of-day constraints: converting “peak hours” into capacity cost

    Once a service introduces peak-hour differences, the cost model needs an additional constraint term. Let the peak duration be T_peak and let q be the share of business requests that fall in the peak window. Whether the peak quota is sufficient determines whether you need a second pay-as-you-go channel for the overflow. The key here is not price, but “schedulable capacity”: the shorter the peak and the further it is from your business peak, the higher the availability of the plan.

    Illustrative capacity check logic:

    1. def peak_ok(weekly_quota, weekly_demand, q, peak_hours, total_hours=168):
    2. peak_demand = weekly_demand * q
    3. off_peak_demand = weekly_demand * (1 - q)
    4. peak_capacity = weekly_quota * (peak_hours / total_hours)
    5. return peak_demand <= peak_capacity and off_peak_demand <= weekly_quota - peak_capacity

    6. Subscription plans vs. pay-as-you-go: converting to an “equivalent list-price multiple”

    Subscription plans (committed quota) and pay-as-you-go are two ways of distributing risk. Convert the plan quota into an “equivalent list price” using the same effective price model, then divide by the plan price to get the discount multiple. The range observed in the original discussion falls roughly between 50% and 70% of list price, which shows that the plan’s concession is real but limited; its true value lies in cost predictability and quota certainty, not in an absolute low price.

    Selection advice: if the workload is steady and predictable and the team size is fixed, use a plan to buy budget certainty; if the workload fluctuates heavily, with bursty peaks or short-term campaigns, use pay-as-you-go to avoid wasting quota; mixing the two (a plan as the base plus pay-as-you-go as a fallback) is usually the structure closest to the real bill.

    7. An actionable cost governance checklist

    • Build a unified gateway layer that centrally records token metering and time-of-day labels, avoiding inconsistent definitions across business lines;
    • Write the effective price and the break-even hit rate into the capacity review template as the basis for selection;
    • Put prompts under version control, and treat any prefix change as a “cache invalidation event” whose cost impact must be assessed;
    • Use monitoring alerts with dual thresholds on daily cost and hit rate, and on anomalies, correlate with the logging service to locate changes in call patterns;
    • Recompute the model parameters once per quarter, since prices and quota policies change over time.

    In related practice at Baidu AI Cloud, the methods above can be implemented by combining general-purpose monitoring and alerting, logging, and container platform capabilities: metering data goes into the logging service for detailed retention, metrics go into monitoring and alerting for trend and threshold management, and the inference gateway is deployed in a containerized way to make canary releases and rate limiting easier. The core conclusion is a single sentence: do not compare list prices; compare the effective price under your own hit rate and mix, and compute the break-even hit rate that determines the direction of your selection.