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

    Comparing LLM API prices by per-million-token rates alone almost always leads to wrong conclusions. This guide shows how to build a reusable cost model based on cache hit rate, input/output ratio, and peak/off-peak pricing, then use sensitivity analysis to compare providers. You will learn why a cheaper cache price does not guarantee a lower bill, how a 3-point drop in hit rate can reverse the ranking, practical ways to improve cache hits, how to evaluate subscription plans using an equivalent list-price multiple, and a decision checklist for choosing between pay-as-you-go and plans.

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

    When evaluating LLM API costs, the most common approach is to divide the per-million-token price of one option by another and conclude that one is “twice as expensive” or “half the price.” This is not technically sound, for three reasons:

    1. Billing is multi-dimensional. A single request is typically split into a cache-hit portion, an uncached input portion, and an output portion, and the unit prices for these three can differ by one to two orders of magnitude.
    2. There is a time dimension. Some platforms apply differentiated pricing for peak and off-peak hours, so the same tokens are billed differently depending on when the request runs.
    3. Real workloads have highly unbalanced input-to-output ratios. For scenarios such as code completion, long-document Q&A, and batch summarization, the input-to-output ratio can range from 1:1 to 200:1.

    The right way to estimate cost is therefore: build a billing model first, plug in your workload parameters, and then run a sensitivity analysis.

    2. Building a Reusable Cost Model

    Define:

    • Cache-hit unit price p_cache
    • Uncached input unit price p_in
    • Output unit price p_out
    • Cache hit rate h
    • Share of total tokens that are input r_in, and share that are output r_out

    The cost per 100 million tokens is:

    1. Cost = 1e8 * ( h * r_in * p_cache
    2. + (1 - h) * r_in * p_in
    3. + r_out * p_out )

    This formula is the core of the whole discussion. It shows that a low cache price does not mean a low total cost, because the uncached portion of the input is usually billed at full price, and a small drop in hit rate gets amplified.

    3. High Hit-Rate Scenarios: Off-Peak Pricing Has a Clear Advantage

    Take a typical set of parameters: input accounts for 1.5% and output for 0.5% of tokens (the rest being cache hits), with a hit rate of 98%. Assume Option A has a cache price of about CNY 0.115 per million tokens, and Option B has an off-peak cache price of about CNY 0.05 per million tokens.

    Plugging these into the model gives a cost per 100 million tokens of roughly CNY 12.57 and CNY 9.40 respectively, with Option B about 25% lower. This confirms an intuition: when almost everything is a cache hit, cost is driven mainly by the cache price, and whoever has the lower cache price wins.

    The engineering implication: if your workload is repeated reuse of long context (the same codebase, the same knowledge base, or the same system prompt called at high frequency), you should treat cache price as the first sorting criterion and try to schedule requests into off-peak windows.

    4. When the Hit Rate Falls to 95%: The Conclusion Reverses

    Keeping all other parameters unchanged and lowering the hit rate from 98% to 95%, Option A comes to about CNY 14.92, while Option B rises to about CNY 18.25.

    The hit rate dropped by only 3 percentage points, yet cost changed by about 19% and 94% respectively. The reason is that uncached input is billed at full price, and the gap between the two options’ full-price input rates is far larger than the gap between their cache prices. The hit rate is the most sensitive cost lever, which also explains why the option with the “cheaper cache” can end up more expensive when the hit rate is unstable.

    Practical ways to improve the hit rate:

    • Keep a stable prefix: put the system prompt, tool definitions, and fixed instructions at the very front of the request, and avoid inserting dynamic content ahead of them, which would invalidate the prefix;
    • Control the rate of change: move volatile fields such as timestamps, random IDs, and user nicknames to the end or remove them;
    • Standardize templates: have the team share one set of prompt templates and versions to reduce prefix divergence;
    • Monitor the hit rate: bring the cache hit rate in as a first-class metric in your logging service and monitoring alerts, with an alert threshold for drops.

    Example monitoring instrumentation:

    1. metrics = {
    2. "prompt_tokens": usage["prompt_tokens"],
    3. "cached_tokens": usage.get("cached_tokens", 0),
    4. "completion_tokens": usage["completion_tokens"],
    5. }
    6. metrics["cache_hit_rate"] = metrics["cached_tokens"] / max(metrics["prompt_tokens"], 1)
    7. log_service.emit("llm_usage", metrics)

    5. Peak/Off-Peak Windows and Plans: Bringing the Time Dimension into Cost

    Beyond pay-as-you-go billing, subscription-based coding plans are another path. Such plans usually also distinguish peak from off-peak, and the definition of the peak window varies: some use a fixed number of hours per day (for example, a stretch in the early afternoon on workdays), while others use a wider window. The narrower the window, the more room you have to schedule around it.

    To compare plans on a common basis, use an “equivalent multiple of list-price API”:

    1. Equivalent multiple = Plan monthly fee / (Monthly available tokens * List unit price)

    Taking a certain plan tier as an example, under a 98% hit rate and non-peak conditions, the ratio of the list-price API amount corresponding to the available tokens in each tier to the plan’s monthly fee falls roughly in the 50%–70% range. This means the plan is equivalent to 50% to 70% of list-price API cost, but only if:

    • Your request volume can actually consume the full allowance, otherwise the discount is only on paper;
    • Your workload can be shifted into off-peak windows, otherwise overflow during peak hours is still billed pay-as-you-go;
    • What you need is stable throughput rather than extreme elasticity.

    6. Decision Checklist

    1. Measure the hit rate before discussing unit price: replay real traffic to get the distribution of cache_hit_rate, rather than a single-point value;
    2. Run a sensitivity analysis: sweep the hit rate across 90%–99% and look for the crossover point of the cost curves;
    3. Pin down the input/output ratio: model batch summarization and interactive completion separately;
    4. If peak/off-peak is schedulable, prefer off-peak: place offline batch jobs, codebase indexing, and bulk refactoring in off-peak windows;
    5. Mix plans with pay-as-you-go: use a plan for the stable baseline and pay-as-you-go for bursts, so you do not pay for peak capacity all the time;
    6. Make cost observable: collect token usage, cache hits, and time-window labels together, and build a cost dashboard broken down by project or environment.

    7. Conclusion

    Comparing LLM API costs is essentially a multi-variable optimization problem: cache price, full-price input, output price, hit rate, peak/off-peak windows, and allowance utilization together determine the final bill. With the model and checklist above, you can give a credible cost range within ten minutes of any new option being released, instead of being led by a single unit-price number. If you already use relevant Baidu AI Cloud products, you can feed these metrics into their logging service, monitoring alerts, and object storage for long-term archiving, forming a cost baseline that can be optimized continuously.