How to Calculate LLM API Cost Efficiency During Price Hikes
A practical guide to estimating large language model API costs beyond per-token pricing. Learn how input, cached input, and output tokens affect your bill, how to build a reusable cost model, and which architecture choices—prefix caching, tiered routing, output constraints, and observability—deliver the biggest savings.
1. The Selection Dilemma After Price Increases
Recently, a category of general-purpose large model services adjusted its pricing, and the per-call unit price of models that developers once treated as the “low-cost default” moved noticeably higher. This has pushed many teams to re-examine their technology stack: the old approach of deciding based only on “dollars per million tokens” is starting to break down.
The real issue is not how much the unit price rose, but that the cost structure is being misread. Most developers focus only on input pricing, while in a real bill, output, cache hit rate, and the retry amplification factor are often the decisive variables. Setting aside specific product attributes, this article discusses general methods for estimating LLM API costs and optimization practices.
2. The Three-Layer Structure of Token Billing
Mainstream LLM APIs generally bill by token, usually split into three categories:
- Normal input tokens: prompts, context, retrieved snippets, and everything else are all counted.
- Cached input tokens: after a repeated prefix (such as a fixed system prompt or a long document) hits the cache, the unit price is significantly lower than normal input—commonly around one-tenth of the normal input price in the industry.
- Output tokens: content generated by the model, usually priced at several times the normal input price.
Using a general tier published by a cloud vendor as an illustration (unit: USD per million tokens, used only to show the order-of-magnitude relationship):
| Tier | Input | Cached Input | Output |
|---|---|---|---|
| High-capability | 5.00 | 0.50 | 30.00 |
| Balanced | 2.00 | 0.20 | 12.00 |
| Lightweight | 0.20 | 0.02 | 1.20 |
The key pattern: output prices are generally several times the input price (commonly around 6x). This means a summarization task with “long input, short output” and a generation task with “short input, long output” can differ in cost by an order of magnitude.
3. A Reusable Cost Estimation Model
Don’t estimate by gut feeling. Use the following formula:
per-call cost = (In_normal/1e6)*P_in+ (In_cache/1e6)*P_cache+ (Out/1e6)*P_outmonthly cost = per-call cost * QPS * 86400 * 30 * amplification factor
The “amplification factor” includes failed retries, repeated context concatenation, and accumulated multi-turn conversation history. In practice it is often between 1.2 and 2.0.
Example code:
def estimate(in_normal, in_cache, out, p_in, p_cache, p_out, qps, factor=1.5):per = (in_normal/1e6)*p_in + (in_cache/1e6)*p_cache + (out/1e6)*p_outreturn per, per*qps*86400*30*factor
A frequently overlooked conclusion: output length is the end with the largest cost leverage. Compressing generation length by 30% usually saves more than switching models.
4. Four Cost-Reduction Methods
- Prefix caching: place the system prompt, tool definitions, and long documents at the very front of the prompt and keep them byte-for-byte stable, avoiding pollution of the prefix by timestamps or random IDs.
- Tiered routing: use the lightweight tier for classification, extraction, and rewriting; escalate only complex reasoning to the high-capability tier. This is the most clearly effective architectural method for improving cost efficiency.
- Output constraints: use structured output, field whitelists, and maximum length limits to reduce generation volume.
- Observability: integrate logging services and monitoring alerts, tag and measure token consumption by business dimension, and locate abnormal callers.
5. A Selection Decision Checklist
- Is the scenario input-intensive or output-intensive?
- Is the prefix stable and able to hit the cache?
- Are failed retries and multi-turn history included in the budget?
- Has cost attribution by caller been established?
6. Conclusion
Price adjustments are only an external variable; what truly determines long-term spending is architecture design. If you get cache hit rate, output length, and tiered routing right, overall cost efficiency remains controllable even during a price-increase cycle. Baidu AI Cloud provides general capabilities in model services and cost governance, and developers can build a sustainable cost baseline based on their own business characteristics.
