LLM Inference Cost Calculation and Plan Selection: A Practical Guide

    Comparing per-token prices alone often leads to wrong conclusions about LLM inference costs. This article shows how to build a reusable cost model based on cache hit rate, input-output ratio, and time-of-day distribution, how to convert subscription plans into equivalent pay-as-you-go rates, and how to choose between them. It also covers practical ways to improve cache hit rates, shift deferrable workloads off peak windows, and avoid common budgeting mistakes, so teams can move from guesswork to data-driven cost optimization.

    1. Why Comparing Unit Prices Often Leads to Wrong Conclusions

    When deploying LLM applications, cost evaluation is the step most likely to be oversimplified. Many teams take two price sheets, compare a single number such as the cache-hit input price or the input price, and conclude that one provider is “twice as expensive.” The flaw in this approach is that a real bill is a usage-weighted sum of multiple billing items, so no single unit price can represent the overall cost.

    The cost of a single request usually consists of four parts: input tokens served from cache, input tokens not served from cache, output tokens, and, on some platforms, a separately listed inference acceleration add-on. The unit prices of these four can differ by an order of magnitude, which makes the mix the decisive variable. Ignoring the mix is like representing an entire curve with a single point.

    A more rigorous approach is to build a cost function that takes the hit rate, the input-output ratio, and the time-of-day distribution as independent variables, and the blended cost per token as the dependent variable. Evaluate it across multiple scenarios and check whether the ranking stays stable. Only when the ranking remains consistent within a reasonable range is the selection conclusion robust.

    2. Building a Reusable Cost Model

    Let the cache hit rate be h and the share of output tokens in total volume be o. The share of input tokens that miss the cache is then (1-h-o). The blended unit cost can be written as:

    1. def cost_per_mtok(p_cache, p_miss, p_out, hit=0.98, out_ratio=0.005):
    2. """Return the blended cost per million tokens (CNY)"""
    3. miss_ratio = 1 - hit - out_ratio
    4. return (hit * p_cache + miss_ratio * p_miss + out_ratio * p_out) * 1_000_000
    5. # Scenario A: high cache hit rate (98%), output share 0.5%
    6. # Scenario B: medium cache hit rate (95%), output share 0.5%
    7. for hit in (0.98, 0.95):
    8. print(hit, cost_per_mtok(0.115, 3.0, 30.0, hit=hit))

    The unit prices are deliberately parameterized rather than hard-coded. Prices change and models iterate; the only stable thing is the structure of the formula. Embedding the formula in a project’s cost dashboard has more long-term value than memorizing a quote from a given day.

    The quantitative results yield the first rule: when the cache hit rate is extremely high, the cache unit price becomes the dominant term and the provider with the lower cache price wins. When the hit rate drops by a few percentage points, the cache-miss input term is amplified quickly and the provider with the lower miss price takes the lead. The two cost curves must intersect somewhere, and the key to selection is judging which side of that intersection the workload actually runs on.

    3. Cache Hit Rate: The Undervalued Core Variable

    The hit rate is not a configuration item but a reflection of engineering capability. It depends on prefix stability, session orchestration, and request routing strategy. In practice, it can be improved in the following ways:

    • Prefix pinning: place the system prompt, tool definitions, and knowledge base snippets at the very front of the request, and keep their order and content stable. Avoid letting volatile fields such as timestamps or random IDs pollute the prefix.
    • Session affinity: keep the same session landing in the same cache domain as much as possible, reducing repeated computation caused by cross-instance jitter.
    • Batch reshaping: aggregate short requests that can be merged, lowering the chance that a fixed prefix is billed repeatedly.
    • Observability: log cached and uncached tokens for every request in the logging service, build a hit-rate time series, and set thresholds with monitoring and alerting.

    It is worth stressing that the hit rate must be measured, not estimated. During a canary phase, run real traffic for a full business cycle, compute P50/P95 hit rates, and feed them into the cost model. Budgeting with ideal values is a common root cause of cost overruns.

    4. Time-of-Day Structure: The Hidden Cost of Peak Windows

    Some platforms charge higher unit prices during specific periods, or split the allowance of a subscription plan between peak and off-peak. In that case, the cost function needs a time-of-day weight:

    1. blended cost = Σ(unit price per period × usage share of that period)

    Assume the peak window is 4 hours per weekday, which is a theoretical share of about 4/24 ≈ 16.7% (weekdays only). If the workload concentrates in daytime hours, the actual share rises significantly. So evaluating a plan requires two things: first, chart a histogram of your own request timing; second, move deferrable tasks (offline batch jobs, log summarization, bulk labeling) into off-peak windows. This step usually brings a noticeable cost reduction without any compromise in model capability.

    5. Converting Pay-as-You-Go and Subscription Plans to a Common Basis

    A subscription plan is essentially prepayment in exchange for a discount. To judge whether it pays off, convert the plan allowance into an equivalent pay-as-you-go amount and then compute the effective discount rate:

    1. def plan_effective_rate(plan_price, weekly_tokens, list_price_per_mtok):
    2. """Return the plan's discount rate relative to the list-price API"""
    3. weekly_value = weekly_tokens / 1_000_000 * list_price_per_mtok
    4. return plan_price / weekly_value
    5. # Example: discount rates of three plan tiers under different cache hit rates
    6. for hit in (0.98, 0.95):
    7. base = cost_per_mtok(0.115, 3.0, 30.0, hit=hit)
    8. for price, tokens in ((118, 3.17e8), (538, 1.90e9), (1078, 4.433e9)):
    9. print(hit, price, round(plan_effective_rate(price, tokens, base), 3))

    After conversion, the plan discount rate typically lands between 50% and 70% of the list price, and it drifts as the hit rate changes. This gives the selection criterion: teams with stable, predictable usage and a high hit rate are better off with a subscription plan; teams with volatile usage, clear peaks and valleys, or a validation phase face lower risk with pay-as-you-go, because unused allowance is not sunk.

    6. Decision Checklist and Common Pitfalls

    • Pitfall 1: comparing only the cache unit price. Compare blended cost instead, and make the source of the hit-rate assumption explicit.
    • Pitfall 2: budgeting with the full plan allowance. Convert from actual P50 usage and leave a 20% buffer.
    • Pitfall 3: ignoring the output share. For long-form generation workloads, output can be several times the input, which directly changes the ranking.
    • Pitfall 4: treating rate limiting as a cost problem. Rate limiting is a capacity problem, solved with queue-based peak shaving and asynchronous processing, and has nothing to do with unit price.

    The practical recommendation is to start with a minimal cost dashboard that visualizes three metrics: hit rate, time-of-day distribution, and input-output mix, then review the selection conclusion once a week. If you use Baidu AI Cloud capabilities, you can host the inference gateway on the container platform, store call logs in object storage, and close the metrics loop with the logging service plus monitoring and alerting, turning cost optimization from experience-based judgment into a data-driven process.