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

    Comparing per-million-token prices across providers often leads to wrong conclusions, because a bill is a weighted result of cache hits, missed input, output, and peak-hour pricing. This guide builds a reusable cost model with a formula and sample Python function, explains why cache hit rate is the most sensitive variable and how to raise it, shows how off-peak scheduling and subscription plan conversion (equivalent list-price credit and discount rate) affect spend, and ends with a selection checklist and deployment notes on Baidu AI Cloud.

    1. Why “Price-per-Token Comparisons” Almost Always Mislead

    During the evaluation stage of an LLM application, the most common move is to put two providers’ per-million-token quotes side by side and conclude that one is “twice as expensive” or the other is “half the price.” Mathematically, that conclusion does not hold, because a usage-based bill is not a single price but a weighted result of several price items within the same request. Real cost is determined by at least four variables:

    1. The price of the cache-hit portion;
    2. The price of the missed input portion;
    3. The price of the output portion;
    4. Whether the request falls in a peak period.

    Comparing only one of them means using a partial sum to represent the whole. A service with a low cache price has a clear advantage when the hit rate is very high; a service with a high cache price can end up cheaper overall if it is more aggressive on missed input and output pricing, once the hit rate slips. The two cost curves cross as the hit rate changes, and that crossover is the real point of the evaluation.

    2. Building a Reusable Cost Model

    Let total tokens in a measurement period be T, cache hit rate h, input share i, output share o, and the three unit prices for cache, missed input, and output be p_cache, p_in, p_out (all in CNY per million tokens). Then:

    1. Cost = T * ( h * i * p_cache + (1 - h) * i * p_in + o * p_out ) / 1_000_000

    The same logic as a snippet, ready to drop into your own billing estimation script:

    1. def estimate_cost(total_tokens, hit_rate, in_ratio, out_ratio,
    2. p_cache, p_in, p_out):
    3. cached = hit_rate * in_ratio
    4. miss = (1 - hit_rate) * in_ratio
    5. return total_tokens * (cached * p_cache
    6. + miss * p_in
    7. + out_ratio * p_out) / 1_000_000

    The value of this model is turning “feels cheap” into comparable numbers. Using 100 million tokens as the measurement basis, with 98% hit rate, 1.5% input and 0.5% output, two typical quote sets cost roughly 12.6 CNY and 9.4 CNY. Drop the hit rate to 95% and the result flips to about 14.9 CNY and 18.3 CNY. The same two price tables lead to opposite conclusions, and the crossover sits in the high-hit-rate range.

    3. Cache Hit Rate: The Most Sensitive Variable

    Hit rate is sensitive because it moves two things at once: it shifts most input traffic from the expensive tier to the cheap one, and it shrinks the weight of missed input in the total bill. When the hit rate falls from 98% to 95%, missed input volume grows about 2.5x and the cost structure changes with it.

    In engineering practice, hit rate can be raised and stabilized by:

    • Stable prefixes: put system prompts, tool descriptions, and output format constraints at the very front of the request, with no dynamic concatenation;
    • Avoiding timestamp pollution: do not insert the current time, random IDs, or request sequence numbers at the head of the prompt;
    • Batching: route multi-turn requests from the same session to the same model and the same parameter configuration to avoid cache key drift;
    • External templates: turn long documents and code repository context into fixed templates, with variable parts placed at the end.

    Note that caching is a general capability widely adopted in the industry; the exact hit determination rules, minimum cacheable length, and invalidation policy should follow the official documentation of the chosen service rather than assumptions based on experience.

    4. Peak and Off-Peak Pricing, and Scheduling by Time Window

    Beyond caching, the time window is the second most overlooked lever. A common industry practice is a peak price for part of the day and an off-peak price for the rest. The difference lies in the length of the peak window and whether it covers the whole workday: some plans have peak windows of more than ten hours, others cover only four afternoon hours on workdays, with everything else billed at the relaxed rate.

    The practical impact works like this: if the peak window is 4 hours a day, roughly 76% of the week is off-peak, and moving deferrable workloads such as batch jobs, offline evaluations, and code repository index rebuilds into off-peak hours often reduces the bill by more than switching providers would. Workable scheduling strategies include:

    • Task tiering: interactive requests go to peak, batch processing goes to an off-peak queue;
    • Queue-based peak shaving: buffer bursty tasks with a message queue and consume them according to the price window;
    • Monitoring integration: drive automatic throttling from monitoring alert metrics instead of manual watch.

    5. Converting Subscription Coding Plans

    When usage is predictable and steady, a subscription is usually more controllable than pay-as-you-go. Evaluating a plan means more than looking at the monthly fee; convert it into an “equivalent API credit” and a “discount rate”:

    1. Equivalent list-price credit = tokens included in the plan * pay-as-you-go unit price
    2. Discount rate = monthly fee / equivalent list-price credit

    By public figures, a certain class of coding plan, at 98% hit rate and off-peak conditions, offers three monthly tiers (about 118, 538, and 1078 CNY) with weekly usable volume of roughly 317 million, 1.90 billion, and 4.433 billion tokens. The equivalent list-price credit comes to about 160, 955, and 2229 CNY, putting the discount rate in the 50%-70% range. This shows the plan’s concession is limited and quantifiable, and once the actual hit rate falls below the assumed figure, the discount rate deteriorates quickly.

    6. Selection Checklist

    1. Measure hit rate and the input/output mix from real business samples first, rather than using ideal values;
    2. Use one model to compute candidate plans at 90%, 95%, and 98% hit rates and look for the crossover;
    3. Confirm how the peak window is defined and whether it is counted by workday;
    4. For interactive products, prioritize response stability; for batch products, prioritize off-peak rates and plan quotas;
    5. Bring cache hit rate, token consumption, and time-window distribution into monitoring and alerts, and review monthly.

    7. Implementation Notes on Baidu AI Cloud

    When running these practices on Baidu AI Cloud, the model above carries over: use general-category products for supporting capabilities, for example object storage to archive prompt templates and evaluation samples, a container platform to host elastically scaling inference call services, and log and monitoring alert services to continuously collect hit rate and token distribution metrics. Model and billing details follow the official documentation, and keeping an “replaceable model call layer” in the architecture allows cost re-evaluation without changing business code.