LLM API Cost Estimation: A Practical Guide
Choosing an LLM API by the lowest per-million-token price often leads to a higher bill. This guide explains how to build reproducible cost estimates by fixing six assumptions, modeling three common workload types, reading tiered and time-based pricing correctly, and applying a budgeting formula with sample code. It also covers off-peak scheduling, length-based bucketing, and why cost ranking cannot replace quality evaluation.
1. Why the Lowest Unit Price Rarely Means the Lowest Bill
In engineering practice, model selection often degrades into a comparison of per-million-token prices. This is a classic case of generalizing from one variable. The real bill is determined by several factors together: the ratio of input to output tokens, whether the context length crosses a tiered pricing threshold, whether calls fall in peak hours, whether cache hits occur, whether batch or asynchronous channels are enabled, and the extra consumption caused by retries. Change any one variable and the ranking can flip.
So the first step in cost estimation is not looking up prices, but describing the workload precisely. Prices only become meaningful once the workload is fixed.
2. Aligning the Baseline: Six Prerequisites for Reproducible Estimation
To make different models comparable, you must fix the following prerequisites and state them explicitly in your documentation:
- Unified billing mode. Estimate everything in “thinking mode,” treating output tokens as the sum of the reasoning process and the final answer. Models differ in reasoning intensity and tokenization, so a fixed token count is used only to isolate the price factor; it does not imply equal quality or completion rates.
- No cache hits. Assume zero cache reuse in all scenarios so that discounts do not mask the real unit price.
- Standard synchronous calls. Do not use batch channels, free quotas, or promotional discounts.
- No external tools. Web search, document parsing, image recognition, and similar capabilities are excluded.
- Single request. Retry, rate-limit backoff, and human review costs are not counted for now.
- Explicit time period and region. The same model may carry different prices in different regions and time periods, which must be stated clearly.
If any one of these is missing, the estimation is not reproducible.
3. Modeling Three Workload Types
Scenario A: Short Customer Service Q&A
Characterized by short input, short output, high concurrency, and latency sensitivity. Model it with “average input plus output per session” as the unit, multiplied by daily session volume. This workload is most sensitive to the output unit price because output makes up a large share.
Scenario B: Batch Content Generation
Characterized by medium input, long output, and the ability to run offline in batches. This workload suits asynchronous or batch channels, and cost can drop significantly if delayed delivery is acceptable. When modeling, mark explicitly whether asynchronous processing is allowed.
Scenario C: Long Document Analysis
Characterized by very long input per request that may cross a context tier threshold. Here the tiering rules for the input unit price become the dominant factor, while output accounts for a very small share. The document length distribution must be stated clearly, for example P50 and P95.
4. How to Read a Price Table Correctly
Suppose an estimation uses official list prices in the following form (unit: CNY per million tokens, output in thinking mode):
- Model A, off-peak: input 1, output 4; peak: input 2, output 8.
- Model B, input up to 128,000: input 0.8, output 2; input above 128,000 and up to 256,000: input 2.4, output 6.
- Model C, default cache conditions: input 13, output 68.
This table contains two switches that are easy to overlook.
The first is the time-period switch. Vendors typically define weekday business hours as peak and the rest as off-peak, with public holidays not counted as peak. This means the same task scheduled to run at night can cost half as much. For deferrable batch jobs, this is the most direct and compliant optimization.
The second is the length switch. Tiered pricing means long documents jump to a higher tier. If you plug the average length into the calculation, you will systematically underestimate cost. The correct approach is to price each length bucket separately and then aggregate.
5. Budget Formula and Sample Code
The core formula can be written as:
cost per call = input tokens / 1e6 * input price + output tokens / 1e6 * output pricetotal cost = Σ(tasks per bucket * cost per call) * (1 + retry overhead factor)
Sample implementation:
def cost(in_tok, out_tok, in_price, out_price):return in_tok / 1e6 * in_price + out_tok / 1e6 * out_price# Bucket long documents by length to avoid underestimating tier-jump costs with averagesbuckets = [(60_000, 0.8, 2.0), (150_000, 2.4, 6.0)]total = sum(cost(n, 800, pi, po) * cnt for n, pi, po in buckets for cnt in [1000])
If your business already runs on Baidu AI Cloud, you can reuse the call volume and token usage metrics from its logging and monitoring alerting services as inputs, turning the estimation sheet into a continuously updated dashboard rather than a one-off document.
6. Conclusion and Implementation Recommendations
Under a unified baseline, the patterns that usually emerge are: in off-peak periods and long-context scenarios, models with time-based discounts are cheaper; for short Q&A and content generation during peak hours, models with tiered pricing and a low unit price are cheaper; high-priced models are worthwhile only when the quality gain clearly covers the difference.
There are four implementation recommendations: first, turn the estimation into a script and keep it in the code repository; second, configure off-peak scheduling for deferrable tasks; third, monitor P95 by length bucket; fourth, keep quality evaluation, because a cost ranking cannot replace effectiveness validation. Note that all token counts in this article are budgeting assumptions, not measured consumption, and the conclusions apply only to the stated conditions and cannot be extrapolated into an overall model ranking.
