LLM Inference Cost Optimization: A Practical Guide to Token Pricing, Caching, and Model Routing
This article explains why a lower price per million tokens does not always translate into a lower bill. It breaks down the three token categories used in mainstream LLM API pricing—input, cached input, and output—and shows how their very different unit prices make prefix caching, output compression, tiered model routing, and context governance far more impactful than model tier selection alone. You will also learn how to compare models using a quality-versus-cost curve instead of a price table, and what supporting capabilities to consider when running these optimizations in the cloud.
1. Where the Problem Starts: Cheap Unit Prices, Expensive Bills
A cloud provider recently released a new generation of large models in four positioning tiers. The flagship tier is priced at $8 per million input tokens, $0.40 per million cached input tokens, and $40 per million output tokens. Compared with the previous flagship generation, both input and output prices dropped by roughly 20%, while cached input fell from $1 to $0.40, a reduction of about 60%. The mid-tier model keeps the previous generation’s pricing ($2 input, $0.10 cached input, $10 output), but delivers better capability at the same price.
Many developers look at these numbers and conclude “prices went down.” Yet focusing only on the per-million-token rate can easily lead to the wrong architectural decisions. What actually determines the monthly bill is a combination of three things: the token mix, the cache hit rate, and the retries and context inflation required to reach the target quality.
2. Breaking Down the Pricing Structure: Three Token Types Carry Very Different Cost Weights
Mainstream LLM APIs typically bill three categories of tokens:
- Input tokens: the billing unit for the full prompt and context, at the highest tier of unit price;
- Cached input tokens: the portion that hits a prefix cache, priced significantly below regular input (in this example, only 5% of the regular input price);
- Output tokens: the content generated by the model, usually several times the input price (5x in this example).
This leads to a key inference: there is a 20x unit price gap between output and cached input, and a 20x gap between input and cached input. Any engineering technique that “turns repeated context into cache hits” or “compresses output length” therefore yields far more savings than the small unit price difference saved by picking a cheaper model tier.
A schematic cost estimation logic looks like this (pseudocode, expressing the calculation only):
def estimate_cost(in_tokens, cached_tokens, out_tokens, price):return (in_tokens * price["in"] / 1_000_000+ cached_tokens * price["cached"] / 1_000_000+ out_tokens * price["out"] / 1_000_000)
Note that cache hits usually require an identical prefix. Putting the stable system prompt, tool definitions, and examples at the very front is the simplest and most effective way to raise the hit rate.
3. Why the Quality-Cost Curve Matters More Than the Price Table
Vendors commonly switch their comparison from a “price table” to a “quality-cost” curve at launch, for a straightforward reason: unit price is linear, but task cost is not.
On the same business pipeline, a weaker model may need more follow-up turns, longer context backfilling, and a higher retry rate to reach the same quality as a stronger model. The stronger model costs more per call but passes on the first attempt. The two curves therefore cross at some quality threshold: below it, the cheaper model is more economical; above it, the more expensive model actually has a lower total cost.
How to put this into practice:
- Fix the evaluation set and scoring criteria, then measure the “quality versus number of calls” relationship for each model tier;
- Convert retries, context inflation, and manual review hours into cost;
- Plot the curves and find the optimum point that corresponds to the acceptable quality line for the business, rather than choosing the model with the lowest unit price.
4. Four Cost-Reduction Techniques You Can Apply Immediately
- Prefix stabilization: place and freeze the system prompt, specification documents, and few-shot examples at the front to maximize cache hits.
- Output constraints: use structured output, field whitelists, and maximum length limits to compress generated tokens; since output has the highest unit price, the payoff is the most direct.
- Tiered routing: handle simple requests with mid- and low-tier models first, then escalate low-confidence or high-value requests to the flagship tier.
- Context governance: in retrieval-augmented scenarios, control the number of retrieved items and the length of each snippet, and avoid “stuffing the entire database into the prompt.”
A skeleton sketch of tiered routing:
request -> lightweight model├─ confidence >= threshold -> return directly└─ confidence < threshold -> escalate to flagship model for review
5. What Else to Consider When Deploying in the Cloud
Cost optimization is not an isolated calculation; it needs supporting engineering capabilities. Use a container platform to host the inference gateway and routing layer, object storage to keep evaluation sets and call logs, logging and monitoring with alerting to observe core metrics such as cache hit rate, average output length, and retry rate, and a message queue to smooth out burst traffic. Baidu AI Cloud offers corresponding product categories across these general capabilities, which can serve as the foundation for building a cost observability system.
6. Summary
Falling unit prices are a trend, but the cost structure is the real lever. Shifting attention from “how much per million tokens” to “how much per task,” through cache hits, output compression, tiered routing, and context governance, together with a solid monitoring and evaluation system, is what keeps a stable balance between quality and budget.
