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

    Comparing per-million-token prices is not a reliable way to estimate LLM inference cost. This guide shows how to build a parameterized cost model based on cache hit rate, input-to-output ratio, peak-hour distribution, and plan discounts, so you can predict spending before the bill arrives.

    1. Why “price per token” comparisons almost always mislead

    When evaluating LLM inference costs, the most common approach is to put two providers’ “price per million tokens” side by side and declare a winner. This does not hold up in engineering practice, for a simple reason: the unit price is only one coefficient in the billing formula. What actually determines the bill are three variables outside that coefficient—cache hit rate, the input-to-output ratio, and the time distribution of requests.

    A typical case from the original discussion: one model charges about CNY 0.115 per million tokens for cache reads, while another charges about CNY 0.05, making the first look more than twice as expensive. But at a 98% hit rate with a mix of 1.5% input and 0.5% output, the cost per 100 million tokens comes out to roughly CNY 12.57 versus CNY 9.40. Drop the hit rate to 95% and the result flips to about CNY 14.92 versus CNY 18.25. The same products, with only one assumption changed, produce the opposite conclusion.

    The takeaway: cost evaluation must be modeled, not reduced to price comparison.

    2. Breaking the bill into a computable formula

    Most inference services bill along four token dimensions: cache-hit reads, cache-miss input, output, and (for some services) cache writes. Let:

    1. total_cost = N × (r_hit·P_hit + r_miss·P_in + r_out·P_out)
    2. where N is total tokens, r_* are the share of each type, and r_hit + r_miss + r_out = 1

    The key point is that r_hit is not a parameter the client can set directly; it is determined by the shape of the workload:

    • Long system prompt + fixed tool definitions + slightly varying user input → high hit rate;
    • Every turn concatenates large numbers of timestamps, random IDs, and retrieved snippets in unstable order → low hit rate;
    • Multi-turn conversations with a stable prefix that only grows at the tail → high hit rate.

    So “cache is cheap” and “cache is expensive” can both be true, depending on how stable your prefix is.

    3. Cache hit rate: the biggest lever in the cost model

    A drop from 98% to 95% looks like 3 percentage points, but the impact on the bill can exceed 50%. The reason is that cache-miss input is usually billed at the full input price, several times the cache-read price, so shifting 3% of the volume is enough to wipe out any price advantage.

    In engineering terms, prefix stability can be improved by:

    1. Fixed ordering: system prompt → tool/schema definitions → long-term knowledge → conversation history → current turn input, never reordered.
    2. Removing volatile fields: move timestamps, request IDs, and random values to the end of the prompt or replace them with placeholders.
    3. Controlling write frequency: cache writes are often billed separately, and frequently rewriting the prefix leads to “write a lot, read little.”
    4. Setting a reasonable TTL: keep the cache alive across gaps in traffic to avoid paying for cold starts repeatedly.

    Illustrative pseudocode:

    1. def build_prompt(system, tools, history, turn_input):
    2. # The prefix must be byte-for-byte stable to be cache-hit
    3. prefix = stable_serialize(system, tools, history)
    4. return prefix + normalize(turn_input) # volatile content goes last

    4. Input-output ratio: the underestimated amplifier

    Output tokens are usually the most expensive part of the whole chain, and they cannot be optimized by caching. The 0.5% output share in the original case is typical of batch extraction, classification, and rewriting tasks; for conversational or code-generation tasks, the output share often rises to 5%–20%, and the conclusion changes again.

    So when estimating, always sample real traffic to measure the three shares rather than borrowing someone else’s ratios. Recommended practice: enable server-side logging or build your own instrumentation, aggregate r_hit / r_miss / r_out daily, and use a moving average to smooth out spikes.

    5. Peak hours: a variable equivalent to a discount rate

    Many services use time-of-day pricing, with peak and off-peak prices differing by several times. In that case, the “length of the peak window” and “which time zone it falls in” are directly equivalent to a discount factor:

    1. effective_unit_price ≈ off_peak_price × (1 - h) + peak_price × h
    2. h = peak_hours / total_billed_hours

    If peak is 4 hours per weekday, h is about 4/24 ≈ 0.167; if peak covers 8 or more hours of every weekday, h doubles and the bill differs substantially. For deferrable offline jobs (batch summarization, log analysis, offline evaluation), concentrating scheduling in off-peak windows often saves more than switching models.

    6. Converting subscription plans to pay-as-you-go terms

    A monthly plan cannot be judged by its sticker price alone; it has to be converted into an “equivalent list-price API credit” and an “actual cost per million tokens”:

    1. def plan_metrics(price, weekly_tokens, list_price_per_mtok):
    2. monthly_tokens = weekly_tokens * 4.345
    3. return {
    4. "equiv_list_value": monthly_tokens / 1e6 * list_price_per_mtok,
    5. "effective_per_mtok": price / (monthly_tokens / 1e6),
    6. "discount": price / (monthly_tokens / 1e6 * list_price_per_mtok),
    7. }

    In the original case, three plan tiers were equivalent to about CNY 160, CNY 955, and CNY 2229 of list-price credit, priced at CNY 118, CNY 538, and CNY 1078 respectively—roughly 50%–70% of pay-as-you-go. That range is the core threshold for deciding whether a plan is worth buying:

    • If your actual usage is below 50% of the plan allowance, pay-as-you-go is cheaper;
    • If you consistently use the full allowance and concentrate it off-peak, the plan discount plus time-of-day pricing can bring cost down to 30%–40% of pay-as-you-go;
    • If usage fluctuates widely, a hybrid strategy (base plan + pay-as-you-go overflow) usually beats the top tier.

    7. A reusable cost dashboard

    It helps to fix the variables above into a single table and change only the assumptions for each evaluation:

    Variable Meaning Typical source of values
    r_hit Cache hit rate Instrumentation data
    r_miss Cache-miss input share Instrumentation data
    r_out Output share Instrumentation data
    h Peak-time share Scheduling plan
    U Total monthly tokens Capacity planning

    Combined with monitoring alerts and log services for daily aggregation, this lets you forecast cost before the bill arrives instead of doing a post-mortem at month end.

    8. Conclusion

    Real cost = billing structure × workload shape × time distribution. Any unit-price comparison that ignores hit rate, output share, and the peak window has no decision value. Build your own parameterized model, drive the assumptions with real samples, and use plan discounts and off-peak scheduling as the two tuning knobs—that is the controllable, reproducible, and continuously optimizable path to cost management. If you already use model services from Baidu AI Cloud, the same approach applies: build an internal estimation sheet with the formulas above, and shift the selection discussion from “whose unit price is lower” to “who is cheaper under my workload.”