Cost Optimization for LLM APIs After a 5x Price Increase
As LLM API prices rise sharply, the real challenge for developers is not the increase itself but the shift in billing structure. This article breaks down the cost formula, explains why output tokens cost more than input, and shows how to reduce spending through output compression, prompt caching, model routing, and observability. It also covers semantic caching, batching, context compression, and streaming as supplementary architecture-level techniques.
1. What Changed: A Shift in Billing Structure
The industry has recently seen a significant increase in LLM API prices. For the new generation of models, the output price reaches the range of hundreds of yuan per million tokens, the input price is in the range of tens of yuan, and the cache-hit price is only around one yuan. Compared with the previous generation, the overall price has roughly increased fivefold. For developers, what matters is not “how much it went up” but “what changed in the billing structure.”
From a billing perspective, the cost of a single request can be abstracted as:
Cost = In_tokens * P_in + Out_tokens * P_out + Cache_tokens * P_cache
The unit price of the cache-hit portion is typically only a few percent of the input price. This means the room for optimization on your bill mainly falls into three directions: reducing output tokens, improving the cache hit rate, and moving reusable context from “input” to “cache.”
2. Why Output Costs More Than Input: The Technical Root of Pricing
Output tokens must be generated autoregressively by the model, token by token, and cannot be parallelized. The compute time they occupy is far longer than the parallel prefill stage for input. Therefore, a higher unit price for output than for input is a common industry pattern. As the new generation of models becomes more capable and the inference chain grows longer, this gap is further amplified.
The engineering conclusion is clear: controlling output length is the highest-return optimization. You can compress redundant expression through prompt constraints, structured output, segmented generation, and result summarization, rather than simply truncating output.
3. Cache Hits: An Underrated Cost Lever
The general principle of context caching is that when a request prefix exactly matches already-cached content, those tokens are billed at an extremely low price. To improve the hit rate, follow three practical rules:
- Stable prefix: Put unchanging system prompts, tool descriptions, and knowledge snippets at the very front, with dynamic content placed afterward;
- Content discipline: Avoid inserting volatile fields such as timestamps or random IDs into the stable prefix;
- Reuse window: Reuse the same prefix in batches within the cache validity period to reduce repeated uploads.
An illustrative structure is as follows:
[Stable prefix: role setup + rules + reference docs] ← cache hit[Semi-stable area: conversation summary][Dynamic area: current user input] ← billed as input
4. Model Tiering and Routing: Send Requests to the “Right” Model
Not every task needs the most powerful model. You can build a three-tier routing scheme: simple classification, extraction, and formatting requests go to lightweight models; routine Q&A and code completion go to the main model; complex reasoning and long-document analysis go to high-capability models. Combined with confidence evaluation and a failure-retry mechanism, this can significantly lower the average unit price while keeping quality largely intact.
5. Observability and a Closed Loop for Cost Governance
Cost reduction must be measurable. It is recommended to log the input, output, and cache-hit token counts and the cost for every call, forming a cost dashboard by business line, by API, and by user, with budget alerts in place. You can build the collection and accounting pipeline using general capabilities such as the log service, monitoring and alerting, message queues, and object storage offered by Baidu AI Cloud, achieving a closed loop of “observable → attributable → optimizable.”
6. Additional Architecture-Level Techniques
- Semantic caching: Perform vector recall for high-frequency similar questions and return directly on a hit, avoiding a call;
- Batching and asynchrony: Merge non-real-time tasks into a single submission to improve throughput and spread cost;
- Context compression: Summarize and use retrieval-based recall for long histories instead of concatenating everything;
- Streaming output: Improve experience while making it easier to terminate invalid generation early.
7. Conclusion
A price increase is an external variable; the billing structure is the controllable one. By focusing optimization on output compression, cache hits, and model tiering, and pairing it with a solid cost observability system, developers can maintain service quality within budget constraints.
