LLM Inference Cost Calculation and Plan Selection: A Practical Guide

    Comparing per-token list prices is the most common mistake when evaluating LLM inference services, because the real bill is driven by cache hit rate, input/output ratio, and peak-hour share rather than any single price. This article builds an effective cost model that turns the bill into a function, shows how the ratio between cache read and cache miss input prices decides which option is cheaper, and explains why cache hit rate is an architecture metric you can engineer through prefix stabilization, context layering, session stickiness, and expiry governance. It also covers off-peak scheduling, how to convert subscription plans into equivalent on-demand discounts, and how to run unified cost governance on Baidu AI Cloud.

    1. Why Comparing List Prices Usually Gives the Wrong Answer

    When evaluating LLM inference services, the most common mistake is comparing the listed price of a single token category. A real bill is made up of four token categories: cache hit reads, cache miss inputs, outputs, and inference-side add-ons that some platforms bill separately. Since cache read pricing is typically only one tenth to one few-hundredth of the cache miss input price, a change of just two or three percentage points in the cache hit rate shifts the total cost noticeably.

    The right approach is therefore not to compare unit prices but to build an “effective unit price” model: explicitly model three variables from your workload, namely the cache hit rate, the input/output ratio, and the share of peak-hour traffic, then plug them into each provider’s billing rules to compute the blended cost per hundred million tokens. Only then is the comparison reproducible.

    2. The Cost Model: Writing the Bill as a Function

    Let the cache hit rate be h, the input share p_in, the output share p_out, and the unit prices for cache reads c_cache, cache miss inputs c_in, and outputs c_out (all normalized to CNY per million tokens). The effective cost per million tokens is then:

    1. def effective_cost_per_mtok(h, p_in, p_out, c_cache, c_in, c_out):
    2. assert abs(p_in + p_out - 1.0) < 1e-6
    3. return h * p_in * c_cache + (1 - h) * p_in * c_in + p_out * c_out
    4. # High cache hit scenario (98% hit, 1.5% input, 0.5% output)
    5. low = effective_cost_per_mtok(0.98, 0.015, 0.005, 0.115, 3.0, 12.0)
    6. # Medium cache hit scenario (95% hit, same mix)
    7. mid = effective_cost_per_mtok(0.95, 0.015, 0.005, 0.115, 3.0, 12.0)
    8. print(low, mid)

    The original measurements abstract into one key rule: at a cache hit rate of about 98%, the option with the higher cache price actually ends up cheaper in blended terms (roughly 12.6 CNY per hundred million tokens versus roughly 9.4, where the gap is compressed by the output and cache miss terms); when the hit rate falls back to 95%, the ranking flips (roughly 14.9 versus roughly 18.3 CNY per hundred million tokens). What decides “who is cheaper” is the ratio between the cache read price and the cache miss input price, not any single listed price.

    The engineering implication is direct: if your system can reliably sustain a high cache hit rate (long system prompts, fixed tool definitions, repeated code context), prioritize the option with the deepest cache read discount; if requests are highly diverse and prefix reuse is low, weight the cache miss input price more heavily.

    3. Cache Hit Rate Is Not a Constant, It Is an Architecture Metric

    The hit rate is determined by how requests are constructed, and it can be improved significantly with the following practices:

    • Prefix stabilization: put the system prompt, tool/function definitions, and few-shot examples first in a fixed order, and keep volatile content such as timestamps and random IDs out of the prefix.
    • Context layering: assemble a “stable layer (hundreds to thousands of tokens) + session layer + retrieval layer” separately, so the stable layer reuses the cache while the retrieval layer is injected on demand.
    • Session stickiness: route the same session to the same cache domain as much as possible to reduce cross-node invalidation.
    • Expiry governance: set a sensible time to live for the cache to avoid hit rate jitter caused by frequent rewrites.

    Illustrative monitoring instrumentation:

    1. metrics = {"prompt_tokens": 0, "cached_tokens": 0, "completion_tokens": 0}
    2. def on_usage(u):
    3. metrics["prompt_tokens"] += u.prompt_tokens
    4. metrics["cached_tokens"] += getattr(u, "cached_tokens", 0)
    5. metrics["completion_tokens"] += u.completion_tokens
    6. def hit_rate():
    7. return metrics["cached_tokens"] / max(metrics["prompt_tokens"], 1)

    Wire hit_rate() into your logging service and monitoring alerts with thresholds at 95% and 98%, and you can catch prefix pollution before the bill runs out of control.

    4. Peak Hours: The Underrated Cost Lever

    Some inference services use time-of-day pricing, with cache and input prices dropping significantly during off-peak hours; subscription-based coding plans typically define only four hours per weekday as peak and treat the rest as relaxed periods. This means the same code completion and refactoring work can cost several times more or less depending on whether it is scheduled into an off-peak window.

    Actionable scheduling strategies include: running non-real-time work such as batch refactoring, dependency upgrades, and test generation off-peak; keeping interactive completion in peak hours; and using a message queue to smooth the load so worker nodes on a container platform consume it during off-peak windows. Pseudocode:

    1. def schedule(task):
    2. if task.latency_sensitive:
    3. return run_now(task)
    4. return enqueue(task, run_at=next_off_peak_window())

    5. Converting Between Pay-as-You-Go and Subscription Plans

    The value of a subscription plan should be measured by its “equivalent list-price API quota”: divide the monthly plan fee by the token quota it includes to get the plan’s effective unit price, then divide it by the on-demand effective unit price to get the discount rate. The original measurements put the plan unit price at about 50% to 70% of list-price API, corresponding to three tiers with roughly 317 million, 1.9 billion, and 4.433 billion off-peak tokens per week.

    Conversion and decision logic:

    1. def plan_discount(plan_price, weekly_tokens, mtok_price):
    2. monthly_value = weekly_tokens * 4.345 * mtok_price / 1e6
    3. return plan_price / monthly_value
    4. def choose(load_mtok, on_demand_price, plans):
    5. on_demand = load_mtok * on_demand_price / 1e6
    6. best = min(plans, key=lambda p: p.price + max(0, load_mtok - p.quota) * on_demand_price / 1e6)
    7. return min(on_demand, best.price + max(0, load_mtok - best.quota) * on_demand_price / 1e6)

    Three rules of thumb: choose pay-as-you-go when monthly usage is below the lowest tier’s quota; a plan is most cost-effective when usage lands between 60% and 90% of a tier’s quota; when usage far exceeds the highest tier, billing the overflow at list price quickly eats up the discount, so switch to spreading load across multiple keys or an enterprise quota arrangement.

    6. Unified Cost Governance on Baidu AI Cloud

    If you already use Baidu AI Cloud’s LLM services, you can harden the model above into an internal cost dashboard: collect the token composition of every call with the logging service, track cache hit rate and peak-hour share with monitoring alerts, archive billing details to object storage for offline regression, and run off-peak scheduled workloads on the container platform. Selection then stops relying on intuition and produces a verifiable conclusion every time about which billing form is better under the current workload profile.

    7. Conclusion

    Real cost = cache hit rate x cache unit price + cache miss input x input unit price + output x output unit price, plus a peak-hour factor. Profile the workload first, compute the effective unit price second, and cross-check with the plan discount rate third: three steps are enough to avoid the misjudgments that come from looking at list prices alone.