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

    This guide breaks down the true cost of large language model inference beyond headline per-token pricing. It presents a computable cost model driven by cache hit rate, input/output ratio, peak versus off-peak usage, and billing mode, then shows how to optimize hit rate through prompt prefix stability, schedule workloads by time window, and convert subscription plans into effective unit prices. Readers will find a step-by-step measurement workflow and sensitivity analysis method for making reliable model selection and budgeting decisions.

    1. The Starting Problem: Why a Low Unit Price Rarely Means a Low Total Bill

    In model adoption review meetings, the most common argument is about unit price: one vendor pushes its cache-hit price very low, another loosens off-peak pricing, and each side can produce a set of numbers showing “we are cheaper.” These arguments never reach a conclusion because unit price is only one coefficient in the cost function. What actually determines the bill are four variables: cache hit rate, the input-to-output ratio, whether requests land in peak or off-peak windows, and whether you use pay-as-you-go or a subscription plan.

    A typical pattern looks like this: cutting the cache-hit price from a higher level to a lower one appears to halve the cost, but in high-hit-rate scenarios the cached portion accounts for more than ninety percent of the total, so the unit-price advantage is diluted. When the hit rate falls back to around ninety-five percent, the uncached portion and the output portion begin to dominate the bill, and the low cache price advantage disappears quickly, or even reverses. Cost evaluation must therefore return to the metric of “cost per unit of effective work” rather than staying at the level of a price sheet.

    2. The Cost Model: Writing the Bill as a Computable Formula

    Let total throughput be N (tokens), cache hit rate h, input share p_in, output share p_out, cache-hit unit price c_hit, uncached input unit price c_in, and output unit price c_out. Then:

    1. Cost = N * [ h * p_in * c_hit
    2. + (1 - h) * p_in * c_in
    3. + p_out * c_out ]

    This formula has three engineering implications:

    1. The cache unit price only has leverage when h is high; the lower h is, the smaller the impact of c_hit.
    2. The output unit price is usually several times the input price, so when p_out rises from a few per thousand to a few percent, the overall bill magnitude steps up.
    3. All unit prices must be converted to the same currency and the same unit (for example, “per million tokens”); otherwise the comparison is meaningless.

    With a mix of “98% hit + 1.5% input + 0.5% output,” the cost per hundred million tokens falls into a relatively low range. Adjust the hit rate to 95%, and uncached input rises from 0.03% to 0.075%; adding the output term, total cost may climb by more than fifty percent, and the ranking of two vendors may even swap. This shows that every one-point drop in hit rate is equivalent to a hidden price increase on uncached input.

    3. Hit Rate Is Not a Constant: It Is an Optimizable Engineering Metric

    Treating h as an externally given value is the largest source of error in cost estimation. The hit rate is actually determined on the application side and can be optimized through:

    • Prompt prefix stabilization: put system instructions, tool descriptions, examples, and knowledge snippets at the very front in a stable order, avoiding changes on every request.
    • Session and document chunking strategy: keep chunk boundaries stable to reduce repeated-prefix invalidation caused by chunking drift.
    • Change isolation: move high-frequency changing content (timestamps, user identifiers, random IDs) to the end or strip it out, so it does not pollute the cacheable prefix.
    • Batching and merging: combine fragmented requests to reduce the share of short-prefix requests.

    Illustrative hit-rate calculation logic:

    1. def hit_rate(stats):
    2. total = stats["prompt_tokens"]
    3. cached = stats["cached_tokens"]
    4. return cached / total if total else 0.0
    5. # Replay real traffic before launch to get a distribution of h, not a single point value

    It is recommended to treat h as a metric with a distribution: record P50/P95, use P95 for budgeting and P50 for quoting, and avoid letting an average hide the long tail.

    4. Time-Window Strategy: Peak Hours Are an Invisible Unit-Price Multiplier

    Besides caching, the second easily overlooked variable is peak hours. Platforms define peak windows very differently: some count four hours per day, others count the entire workday. The narrower the window, the larger the schedulable space. In engineering, “time” should be treated as a first-class scheduling dimension:

    • Move all offline tasks (batch processing, evaluation, log analysis, index building) off peak;
    • Keep online interactions in peak hours, but compress cost through caching and short outputs;
    • Build queues and priorities, write deferrable requests into a message queue, and let consumers release volume during troughs.
    1. Priority P0: online chat, real-time completion -> peak allowed
    2. Priority P1: near-line summarization, tagging -> throttled at peak, scaled up at trough
    3. Priority P2: batch cleaning, offline evaluation -> trough only

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

    A subscription plan is essentially “prepay for a discount,” and evaluation must convert it into an effective unit price:

    1. Effective unit price = monthly plan fee / (total available tokens per month)

    Where monthly available volume = weekly quota × number of weeks × time-window utilization. If a plan offers several hundred million tokens per week off peak, converted over four weeks and compared with the original pay-as-you-go quota, the effective unit price typically lands between fifty and seventy percent of the original price. But note three discount erosion points:

    1. Time-window erosion: tasks concentrate in peak hours, the quota goes unused, and the effective unit price actually rises.
    2. Rollover erosion: leftover quota at month end does not carry over, which equals partial prepayment being voided.
    3. Overflow erosion: beyond the quota, billing switches to pay-as-you-go and marginal cost jumps.

    Therefore plans suit services with steady, schedulable, predictable loads; businesses with large fluctuations and high peaks are better served by pay-as-you-go plus cache optimization.

    6. An Actionable Measurement Workflow

    1. Sampling: replay one week of real traffic, and measure the distribution of h, p_in/p_out, and the peak-to-trough ratio.
    2. Modeling: plug into the formula above, output cost per hundred million tokens, and run sensitivity analysis (h from 0.90 to 0.99).
    3. Optimization: do prefix stabilization and off-peak scheduling first, then discuss vendor selection; optimization usually delivers more savings than switching providers.
    4. Review: set the budget ceiling using the P95 hit rate and peak share to avoid overspending.
    5. Attribution: tag bills by business line, scenario, and priority to locate high-consumption sources.

    7. Conclusion

    Real cost = unit price × hit-rate structure × time-window structure × billing mode. Discussing unit price without the latter three almost certainly distorts the conclusion. Make hit rate and time windows observable and schedulable engineering metrics, then run sensitivity analysis under a unified cost-per-hundred-million-tokens metric, and you can make robust decisions between selection and budgeting. Building this measurement and scheduling pipeline on top of the general capabilities of platforms such as Baidu AI Cloud (for example monitoring and alerting, log service, message queue, and container platform) turns cost from an “after-the-fact bill” into something controllable in advance.