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

    Comparing per-million-token prices alone often leads to the wrong conclusion about which LLM inference service is cheaper. This guide shows how to build a reusable cost model based on cache hit rate, input/output ratio, peak-hour pricing, and subscription plans, plus a decision checklist for running sensitivity analysis and setting up cost monitoring.

    1. Why “Price Comparison” Almost Always Leads to the Wrong Conclusion

    When evaluating LLM inference services, the most common approach is to put the “price per million tokens” of two candidate options side by side and pick the lower one. This is unreliable because a complete inference request is usually split into four billing categories: cached input, uncached input, output, and, in some options, an additional thinking or reasoning component. The unit prices of these four categories often differ by an order of magnitude, so which option is cheaper depends entirely on the real traffic structure of your workload, not on the numbers in the price list.

    The phenomenon described in the original discussion is exactly this: in scenarios with a very high cache hit rate and a tiny input share, the option with the lower cache price wins; once the hit rate drops by a few percentage points, the high-priced uncached input tokens scale up quickly and the conclusion reverses. So the right approach is to build a cost model first, and only then discuss vendor selection.

    2. Building a Reusable Cost Model

    Let the total number of tokens be N, the cache hit rate be h, the input share be a, and the output share be b (a + b = 1). Let the cache hit price, uncached input price, and output price be p_c, p_i, and p_o respectively. Then:

    1. Total cost per request C = N × [ h·a·p_c + (1-h)·a·p_i + b·p_o ]
    2. Unit cost (per million tokens) = C / N × 1,000,000

    This formula has three engineering implications:

    1. The cache term is scaled by both h and a, so when the hit rate is high and the input share is small, it contributes very little to the total cost;
    2. The uncached input is amplified by (1-h), making it the most sensitive term when the hit rate fluctuates;
    3. The output term is unaffected by caching, so long-form generation workloads are dominated almost entirely by p_o.

    Verify with a set of illustrative numbers: assuming a 98% hit rate, 1.5% input, and 0.5% output, the cost per 100 million tokens for option A and option B is roughly 12 yuan and 9 yuan respectively; drop the hit rate to 95% and it becomes roughly 15 yuan and 18 yuan. The conclusion completely reverses, and not a single word on the price list has changed.

    3. Hit Rate Is Not a Static Parameter

    Treating h as a constant is the most common mistake in cost estimation. The hit rate is actually determined by how stable the request prefix is:

    • Whether the system prompt, tool definitions, and few-shot examples stay word-for-word identical;
    • Whether volatile fields such as timestamps, random IDs, and user nicknames are placed at the very front of the prompt;
    • Whether multi-turn conversations append to history each turn instead of rewriting it;
    • Whether concurrency and routing strategies consistently send the same prefix to the same cache domain.

    The engineering recommendation is to layer your prompts: put the stable layer (role settings, guidelines, long documents) first, the semi-stable layer (retrieved snippets) in the middle, and the volatile layer (the current turn’s question) last. An illustrative structure:

    1. [SYSTEM fixed guidelines][TOOLS fixed definitions][DOC long context][SESSION history][USER current input]

    Pair this with monitoring, alerting, and logging services, and build a dashboard for hit rate, average input length, and output length. Only then can capacity planning be driven by measured data rather than guesswork.

    4. Peak Hours: The Overlooked Second Dimension

    Many inference services use time-of-day pricing, with unit prices rising during peak hours. The key point is that platforms define “peak” very differently: some count only a few hours per day, others count the entire workday. To calculate real cost, you must first measure the time distribution of your requests, then apply the following weighting:

    1. Effective unit price = peak share × peak price + (1 - peak share) × off-peak price

    For workloads dominated by automation scripts and batch offline jobs, scheduling tasks into off-peak windows often delivers greater cost savings than switching options. Online workloads built for human interaction naturally have a high peak share, so the practical value of time-of-day discounts is limited.

    5. How to Evaluate Subscription Plans

    Monthly subscription plans (for example, tiers that provide a monthly token allowance) should not be judged by the sticker price alone; convert them into an “equivalent multiple of list price”:

    1. Equivalent allowance value = tokens included in the plan × unit cost
    2. Discount rate = plan price / equivalent allowance value

    Under the off-peak, high-hit-rate assumptions, the three plan tiers in the original material land at a discount rate of roughly 50% to 70%. This raises two constraints that must be checked: first, whether the allowance resets on a weekly or daily rolling basis; second, whether peak hours are separately throttled or downgraded. If your workload’s peak is concentrated in the peak window, the plan’s effective discount is significantly diluted.

    6. Decision Checklist

    • Use one week of real traffic to measure h, a, b, and the time distribution, then plug them into the model;
    • Run sensitivity analysis on h (for example 90%/95%/98%) and on peak share, and budget for the worst case;
    • Optimizing prompt structure and cache friendliness is usually more effective than pushing unit prices down;
    • Schedule batch jobs off-peak, and prioritize stability for online workloads;
    • Wire the cost dashboard and alerts into daily operations instead of reconciling at month end.

    In related practices at Baidu AI Cloud, the model above can be applied directly with its general product categories (such as log service, monitoring and alerting, object storage, and container platforms) to collect data and handle scheduling, turning “feels cheaper” into numbers that can be recomputed and audited.