A Practical Guide to Measuring the Real Cost of LLM Inference

    Comparing per-token list prices is a common but unreliable way to choose an LLM API, because pricing depends on at least four variables: cached input price, uncached input price, output price, and when requests run. This guide formalizes the cost model, shows how a 3-point change in cache hit rate can flip which plan is cheaper, lists the prefix-stability practices that keep hit rates high, and explains how off-peak scheduling and subscription plans factor into a realistic monthly bill. Readers will get a reusable Python cost-estimation function and a five-step plan for replacing assumptions with measured token data.

    1. Why “unit price comparison” almost always leads to the wrong conclusion

    In model selection and budget reviews for LLM applications, the most common argument is that “one vendor’s cache price is twice the other’s, so it must be more expensive.” Conclusions like this are unreliable because they compress a multi-dimensional pricing system into a single number. A real bill is determined by at least four variables: the price of the cached portion, the price of the uncached input portion, the price of the output portion, and the time distribution of when requests occur. A comparison on any single dimension can be reversed by the other three.

    More importantly, cache price and hit rate are strongly coupled. A low cache price often means the vendor has shifted the pricing weight onto the uncached path, while a high cache price usually comes with a lower uncached price. So the statement “cache costs twice as much” carries very little information on its own; it only becomes meaningful once you plug in a specific hit rate.

    2. Formalizing the cost model

    Let total processed tokens be T, cache hit rate h, input share i, and output share o. The three billed quantities are:

    1. cached = T * h
    2. uncached = T * i * (1 - h)
    3. output = T * o
    4. cost = cached * P_cache + uncached * P_in + output * P_out

    Note that i + o = 1, and the hit rate applies only to the input side. This is easy to miss: output never gets a cache discount, so in long-output scenarios such as code generation and document writing, the benefit from caching is significantly diluted.

    Using 100 million tokens as the baseline, assume two pricing schemes (Scheme A: higher cache price, lower uncached price; Scheme B: lower cache price, higher uncached price) and a typical mix of 98% cache + 1.5% input + 0.5% output:

    • Scheme A costs about 12.6 CNY per 100 million tokens
    • Scheme B costs about 9.4 CNY per 100 million tokens

    Here the low-cache-price scheme wins. But drop the hit rate to 95%:

    • Scheme A costs about 14.9 CNY per 100 million tokens
    • Scheme B costs about 18.3 CNY per 100 million tokens

    The conclusion reverses completely. A hit rate difference of just 3 percentage points produces a cost gap of more than 30%. This shows that the hit rate, not the unit price, is the dominant variable in the cost model.

    3. Why hit rates fall short of expectations

    Whether the hit rate can stay above 95% depends on the stability of the request prefix. The following engineering practices directly determine the hit rate:

    1. Put the system prompt first and keep it fixed byte for byte. Injecting any timestamp, random ID, or user nickname will cause a prefix mismatch.
    2. Maintain conversation history in append-only fashion; never summarize, rewrite, or reorder prior messages.
    3. Keep tool definitions (function schema) stable; field order and description text must not be assembled dynamically.
    4. Separate items that do not affect the prefix, such as temperature and sampling parameters, from the prompt so that tuning does not modify the prefix.
    5. In multi-tenant setups, isolate prefixes per tenant to prevent one tenant’s content from polluting another’s cache.

    A reusable hit-rate and cost estimation script:

    1. def estimate_cost(total, hit, in_ratio, out_ratio,
    2. p_cache, p_in, p_out):
    3. assert abs(in_ratio + out_ratio - 1.0) < 1e-6
    4. cached = total * hit
    5. uncached = total * in_ratio * (1 - hit)
    6. output = total * out_ratio
    7. return (cached * p_cache + uncached * p_in + output * p_out) / 1e8

    Treat hit as the independent variable in a sensitivity analysis and plot a curve from 90% to 99%, rather than reporting a single point value.

    4. Peak hours: the underestimated lever

    Besides caching, the second high-leverage variable is time. The industry commonly uses differentiated peak and off-peak pricing, with peak windows usually falling on weekday daytime and typically lasting a few hours per day. The shorter and more concentrated in the afternoon the window is, the friendlier it is to non-interactive workloads.

    The engineering response is to schedule latency-insensitive workloads (batch cleaning, offline evaluation, log summarization, nightly builds) into off-peak windows and keep interactive requests in peak windows. Isolating the two with queues and task priorities lowers the average price without sacrificing user experience.

    5. How to evaluate subscription plans

    For subscription plans (monthly fee with a token quota), value cannot be judged from the sticker price; it has to be converted into an “equivalent multiple of pay-as-you-go list price”:

    1. equivalent value = total pay-as-you-go list price covered by the monthly quota
    2. discount rate = monthly fee / equivalent value

    Assuming a 98% hit rate and off-peak usage, the equivalent value of three tiers is roughly 160 / 955 / 2,229 CNY, against monthly fees of 118 / 538 / 1,078 CNY, giving discount rates in the 50%–70% range. In other words, the plans work out to 50%–70% of pay-as-you-go pricing.

    But three hidden constraints must be included in the model: quotas are settled per cycle and usually do not roll over; consuming quota during peak hours is less efficient; and once the quota is exhausted, billing falls back to pay-as-you-go. Plans therefore suit teams with steady, predictable workloads, while bursty workloads are better served by pay-as-you-go.

    6. Conclusions and practical recommendations

    1. Never make a selection based on unit price alone; measure the hit rate first.
    2. Build a cost table with three parameters — hit rate, input/output mix, and time-of-day distribution — and run a sensitivity analysis.
    3. Use prefix-stability engineering to lock the hit rate high; this is the highest-return optimization.
    4. Move offline workloads to off-peak windows, then check whether a subscription plan’s discount rate covers your workload curve.
    5. If you already use Baidu AI Cloud services, you can reuse its monitoring, alerting, and logging services to collect real token distribution, replace the assumed values with measured data, and recalibrate the model automatically once a month.