LLM API Cost Estimation and Model Selection: A Practical Guide
A practical guide to estimating LLM API costs by breaking down input, cached, and output token pricing. Learn how output length drives your bill, how to run reproducible cost simulations with real traffic, and how to use tiered model selection and routing to control spending.
1. The Problem: After a Price Hike, “Cost-Effectiveness” Needs a New Definition
When an upstream model provider changes its pricing, many teams’ first instinct is to switch models. But what usually makes the bill spiral out of control is not the unit price—it is the call structure. To decide which option is truly more cost-effective, you first need to break cost down into computable quantities, and only then discuss model selection.
The cost of a single call can be abstracted into three parts:
Total cost = Input tokens × Input price+ Cached tokens × Cache price+ Output tokens × Output price
The cached portion is typically much cheaper than the regular input price, which is the key to significantly reducing costs in long-context and repeated-prefix scenarios.
2. How the Three Token Types Are Billed
Based on the general patterns in public pricing, mainstream model services roughly follow the structure below (illustrative only, to show the order-of-magnitude relationships; actual figures are subject to each provider’s current published pricing):
| Tier | Input price | Cached input price | Output price |
|---|---|---|---|
| High-capability tier | Baseline | ~1/10 | ~6× input |
| Balanced tier | ~0.4× baseline | ~1/10 | ~6× input |
| Lightweight tier | ~0.04× baseline | ~1/10 | ~6× input |
Two important conclusions follow:
- Input price differences between tiers can span one to two orders of magnitude, but the output-to-input multiplier stays relatively stable, generally in the 5–6× range.
- As a result, the higher the output proportion, the more the cost gap between tiers is amplified; conversely, input-dominated tasks are more sensitive to tier selection.
3. Why Output Length Determines the Bill
Let the input-to-output token ratio be r = output / input. The cost of a single call can then be written as:
C = P_in × (1 + 6r) × N_in
When r = 0.1, the cost is about 1.6× that of input alone; when r = 1, it is about 7×; when r = 3 (generating long articles or large-scale code refactoring), the cost approaches 19×. This means a model with a “cheap” unit price, if called frequently in long-output scenarios, may end up costing more overall than a pricier tier.
Practical advice: treat output length as a first-class concern to govern, including limiting max_tokens, generating in segments, caching and deduplicating generated results, and switching to summary-style output for non-essential long text.
4. A Reproducible Estimation Method
Do not choose models based on gut feeling. Run offline replay estimation with real traffic:
def estimate_cost(n_in, n_cached, n_out, p_in, p_cache, p_out):return n_in * p_in + n_cached * p_cache + n_out * p_out# Substitute prices per million tokens; the result is in USDsamples = [# (input, cached, output, scenario)(8000, 6000, 300, "Customer support Q&A"),(60000, 50000, 2000, "Long document extraction"),(2000, 0, 4000, "Code generation"),]for n_in, n_cached, n_out, scene in samples:print(scene, estimate_cost(n_in, n_cached, n_out, 2.0, 0.2, 12.0) / 1e6)
The key action is sampling: extract real requests from production logs, measure the distribution of input, cached, and output tokens, then compute total cost against the candidate models’ price matrix—rather than comparing list prices alone.
5. Tiered Selection Strategy
- High-capability tier: for low-frequency, high-value tasks such as complex reasoning, professional writing, and difficult code, with strict control over output length.
- Balanced tier: handles the main business traffic and is the cost-effectiveness balance point for most teams.
- Lightweight tier: for high-concurrency, short-output scenarios such as classification, extraction, rewriting, and routing decisions.
- Routing layer: first判断 intent and difficulty, then dispatch requests to the appropriate tier, which can significantly reduce overall spending.
6. Engineering Cost-Reduction Checklist
- Prefix caching: keep system prompts and knowledge base snippets fixed at the front of requests to improve cache hit rates.
- Context trimming: retain only the necessary snippets from retrieval results, avoiding stuffing in entire documents.
- Structured output constraints: use schema constraints to reduce invalid generation and retries.
- Observability: instrument token usage in logging and monitoring/alerting, and aggregate costs by business line.
- Elastic architecture: use a container platform to host the inference gateway, combined with a message queue to smooth peaks, avoiding long-term payment for peak capacity.
7. Implementation Approach on Baidu AI Cloud
If you build a model gateway on Baidu AI Cloud, you can adopt a combination of general-purpose capabilities: use the container platform to deploy routing and rate-limiting components, use object storage to store samples and replay datasets, use logging and monitoring/alerting to build a token cost dashboard, and implement scenario-based scheduling based on the tiered strategy. This turns model selection from guesswork into a continuously optimizable engineering process.
8. Conclusion
A price hike is only an external variable; the cost structure is the internally controllable factor. First quantify the three cost segments—input, cache, and output—then run replay estimation with real traffic, and finally implement tiering and routing. Only then can you reliably obtain the capabilities you need within budget.
