Large Language Model API Cost Estimation and Model Selection: A Practical Guide
A practical guide to estimating large language model API costs beyond input pricing. It explains the three-layer token billing structure, why output multipliers amplify expenses, how cache hit rates affect long-context economics, and how tiered routing helps match tasks to the right model tier. It also outlines a reusable selection workflow and a checklist of common cost-estimation mistakes.
1. Why a Low Per-Token Price Does Not Mean a Low Bill
When evaluating the cost-effectiveness of large language model APIs, the most common mistake is comparing only the input price per million tokens. A real bill is determined by three token categories: regular input, cache-hit input, and output. Their unit prices usually form a tiered structure: cache-hit input is the cheapest, output is the most expensive, and the gap between output and input can be several times. As a result, the same model can have a unit task cost that differs by an order of magnitude across different workloads.
A more reliable approach is to introduce a unit task cost metric:
Cost per task = (input tokens × input price + cache-hit tokens × cache price + output tokens × output price) / 1,000,000Unit task cost = cost per task / average value produced per task
Once you plug in the real token distribution of your workload, the selection decision shifts from a “feeling” to something you can actually calculate.
2. The Three-Layer Token Billing Structure
The first layer is regular input. It covers prompts, retrieved snippets, conversation history, and tool return results, and it usually scales linearly with context length. The second layer is cache-hit input. When multiple requests share a stable prefix (system prompts, knowledge base snippets, long documents), this content can be billed at a significantly lower price, which is key to reducing costs in long-context scenarios. The third layer is output. Output occupies the generation phase, carries the highest price, and is affected by the maximum output length, reasoning depth, and structured output constraints.
In engineering practice, these three layers should be tracked separately rather than only looking at the total:
# Example: estimate the cost of a single call by token layerdef estimate_cost(in_tokens, cached_tokens, out_tokens, price):return (in_tokens * price["in"]+ cached_tokens * price["cached"]+ out_tokens * price["out"]) / 1_000_000
3. Output Multipliers: The Underestimated Cost Amplifier
A pricing structure in which output costs several times more than input is common across the industry. This means that for a summarization task with 10,000 input tokens and 2,000 output tokens, the output portion may contribute more than half of the cost; while for a classification task with 20,000 input tokens and 200 output tokens, the cost is almost entirely driven by input.
Two practical conclusions follow. First, anywhere you can compress output, the payoff is far greater than compressing input, for example by limiting answer length, requiring the conclusion first, or replacing long explanations with structured fields. Second, for tasks with extremely short output, prefer a lower-priced model tier, because the amplification effect from the output multiplier is limited in that case.
Task A: input 20000, output 200 → cost is input-dominated; a lower-price tier is more cost-effectiveTask B: input 1000, output 2000 → cost is output-dominated; focus on controlling generation length
4. Cache Hit Rate Determines Long-Context Economics
Scenarios such as long-document question answering, code repository understanding, and multi-turn customer service have highly repetitive prefixes. If a caching mechanism is available and hits reliably, the cached portion is often priced at around one-tenth of regular input, and overall costs can drop by a substantial margin. Ways to improve the hit rate include: placing unchanged content up front as a stable prefix, avoiding timestamps or random identifiers at the top of the prompt, fixing knowledge snippets into stable chunks, and controlling how often the system prompt is rewritten mid-session.
Note that cache hits depend on the specific platform implementation and lifecycle policies. In engineering, use the measured hit rate as an input to capacity planning rather than assuming a 100% hit rate.
5. Tiered Routing: Assign Tasks to the Right Model Tier
Mainstream platforms typically offer three to four model tiers: a flagship tier for complex reasoning and specialized tasks, a balanced tier that trades off capability and cost, and a lightweight tier for high-concurrency, low-complexity scenarios. A sensible approach is to route by task difficulty rather than using the flagship tier uniformly across the entire site.
def route(task):if task["complexity"] >= 0.8:return "flagship" # complex reasoning, long tool-call chainsif task["complexity"] >= 0.4:return "balanced" # regular generation and extractionreturn "lightweight" # classification, rewriting, batch labeling
Combined with an offline evaluation set, set a quality floor for each tier. If the lightweight tier fails to meet it, automatically fall back to the balanced tier, achieving a stable balance between cost and quality.
6. A Reusable Selection Workflow
Step one: sample real traffic and measure the token distribution of input, cache hits, and output instead of using theoretical values. Step two: build a small evaluation set covering typical and boundary samples. Step three: use a unified formula to calculate the unit task cost of each candidate tier under the real distribution. Step four: recompute after introducing caching and routing strategies to observe marginal gains. Step five: set budget alerts and usage monitoring, and track deviations on a daily basis.
For deployment, you can combine a container platform to host self-managed inference services, use object storage to hold evaluation sets and call logs, use a message queue to smooth out traffic peaks and troughs, and use logging and monitoring with alerts to form a closed loop for cost observability. Baidu AI Cloud offers a fairly complete combination of these general capabilities, making it easier for teams to turn cost governance into a routine process.
7. A Checklist of Common Mistakes
First, looking only at the input price while ignoring the output multiplier; second, treating cache hits as inevitable without measuring the hit rate; third, using the highest model tier uniformly across the entire site; fourth, not setting a maximum output length, allowing long-tail requests to pull up the average; fifth, lacking cost attribution by business dimension, making it impossible to locate the real cost drivers. The core of avoiding these problems is to keep the cost model in sync with business metrics and make decisions with data rather than impressions.
