LLM API Cost Estimation and Model Selection: A Practical Guide
This article explains why standard per-token price lists often lead to inaccurate LLM API budgets and introduces a blended unit price model that combines cache-hit reads, cache-miss inputs, and outputs into a single comparable metric. It covers typical cost tiers, non-price variables such as time-of-day pricing and context-length brackets, a four-step selection workflow, and engineering practices for reducing cost through prefix stabilization, context trimming, output constraints, batching, and observability.
1. Why the Published Price List Is Not Enough for Budgeting
A common mistake in LLM API cost planning is multiplying the published input and output prices by an estimated token volume and treating the result as a precise budget. In practice, the estimate can be far off because real request cost is not one-dimensional. It is determined by three separate billing components.
- Cache-hit reads: When a request prefix closely matches previous requests, the platform can reuse the already computed context. This portion is billed at a read price, which is usually much lower than the standard input price.
- Cache-miss inputs: Tokens that genuinely require prefilling are billed at the input price, making this the most expensive segment.
- Outputs: Generated content is billed at the output price, which is typically higher than the input price.
Ignoring this three-part structure leads to two common misjudgments: overestimating cost by treating all input as cache misses, and underestimating cost by overlooking the combined effect of long outputs and peak-concurrency periods.
2. The Blended Unit Price Model: Reducing Three Billing Components to One Comparable Number
To compare models, time periods, and channels on the same basis, you can introduce an abstract metric called the blended unit price. Given a set of ratios observed on the business side (for example, 80% cache hit rate, 10% cache-miss input, and 10% output), the blended unit price can be written as:
blended unit price = r_hit × read price + r_miss × input price + r_out × output priceUnit: CNY per million tokens
Here, r_hit + r_miss + r_out = 1. In a typical long-context question-answering scenario with an 8
1 ratio, the formula becomes:
blended unit price = 0.8 × read price + 0.1 × input price + 0.1 × output price
This model is valuable for three reasons:
- Comparability: It compresses multi-dimensional pricing into one dimension that can be sorted directly.
- Explainability: Each coefficient maps to a real business distribution and can be traced back to logs.
- Sensitivity analysis: Changing
r_hitshows whether the ranking flips.
Illustrative code for batch calculation and ranking:
def blended_price(read_p, in_p, out_p, r_hit=0.8, r_miss=0.1, r_out=0.1):return r_hit * read_p + r_miss * in_p + r_out * out_prows = [("model_a", 0.02, 0.60, 2.40),("model_b", 0.05, 0.90, 3.00),]ranked = sorted(rows, key=lambda x: blended_price(x[1], x[2], x[3]))
3. General Patterns in Cost Tiers
Without pointing to any specific vendor, the industry price distribution shows a stable tiered structure. Understanding these tiers is more valuable than memorizing any single price table:
- Extreme value tier: A blended unit price on the order of a few tenths of a CNY per million tokens, suitable for large-scale batch processing, log summarization, and offline labeling.
- Mainstream balanced tier: A blended unit price of roughly 1–5 CNY, covering most online business workloads while balancing quality and cost.
- Capability-first tier: A blended unit price around 10 CNY, used for complex reasoning and long-chain tool calls.
- Flagship tier: A blended unit price of tens of CNY, used only when the quality gain clearly outweighs the cost.
Two non-price variables also matter. First, time-of-day pricing: peak and off-peak price sets can shift the ranking of the same model. Second, context-length brackets: the unit price jumps once a certain threshold is exceeded. Both must be modeled in the budget formula; otherwise, the ranking conclusion will be distorted.
4. From Ranking to Decision: A Four-Step Selection Method
- Measure the real distribution: Compute
r_hit / r_miss / r_outfrom call logs instead of assuming an 8
1 ratio. - Calculate and rank blended unit prices: Produce a cost sequence for the candidate set and remove items whose prices cannot be verified.
- Run sensitivity tests: Sweep
r_hitfrom 0.5 to 0.95 and check whether the ranking stays stable. If it does not, caching strategy deserves more investment than model selection. - Layered routing: Use low-cost models for high-frequency simple requests and high-capability models for a small number of difficult requests, forming a cost-quality optimal combination.
5. Engineering Checklist for Cost Reduction
- Prefix stabilization: Keep system prompts, tool definitions, and few-shot examples at the very front of the request to maximize cache hits.
- Context trimming: Use a sliding window plus summary compression to control the size of cache-miss input.
- Output constraints: Use structured output and maximum length limits to suppress ineffective generation.
- Batching and off-peak scheduling: Move offline tasks to low-traffic periods and asynchronous queues.
- Observability: Instrument the three token segments separately and combine monitoring alerts into a cost dashboard.
In related practices at Baidu AI Cloud, these methods are typically combined with general capabilities such as log services, monitoring alerts, and message queues to form a measurable, attributable, and optimizable cost loop.
6. Conclusion
The core of cost governance is not finding the cheapest model, but building a reusable set of three things: a blended unit price model, real distribution measurement, and layered routing. When the cache hit rate becomes the dominant variable, the marginal benefit of architecture optimization often exceeds that of switching models; when the output share rises, constraining generation length should take priority. Only when price, time period, and context-length brackets are converted into the same dimension does the budget become truly predictable.
