LLM API Cost Optimization: How Permanent Price Cuts Reshape Architecture and Engineering Practices
This article explains how the shift from temporary discounts to long-term low pricing for large language model APIs changes the cost model for AI applications. It breaks down the standard billing dimensions, offers a reusable cost estimation formula, and shows why output tokens, cache hit rates, and long context are the main cost drivers. Readers will find practical techniques for building stable prompt prefixes, constraining output with JSON Schema, applying tiered routing and semantic caching, and planning capacity with a monthly token budget. It also covers observability and vendor-neutral abstraction layers to keep costs under control while preserving quality.
1. From Limited-Time Discounts to Permanent Low Prices: A Structural Shift in the Cost Curve
Over the past two years, LLM API pricing has evolved from pay-as-you-go rates with relatively high unit costs, to periodic discounts, and finally to long-term price reductions with locked-in low rates. On the surface, this is just a pricing adjustment. From an engineering perspective, however, it means the cost per unit of intelligence has entered a sustained downward trend, and the architectural assumptions on the application side need to be rewritten.
This article does not discuss any specific vendor’s products or pricing. Instead, it abstracts the billing dimensions common across the industry to help readers build a transferable cost model. The mainstream billing dimensions typically include input tokens, output tokens, input tokens served from cache, and, in some scenarios, batch inference and long-context surcharges. After a sustained price reduction, feature designs that were previously considered infeasible—such as full-context injection, multi-round reranking, and automated quality checks—move back into the feasible range.
2. Build the Right Mental Model for Billing
Most cost overruns do not come from unit price but from misunderstanding how billing is calculated. A reusable estimation formula is as follows:
Cost per request =input tokens × input unit price+ output tokens × output unit price- cached input tokens × (input unit price - cache unit price)
This yields three engineering conclusions:
- Output tokens are usually significantly more expensive than input tokens, so “making the model say less” is often more effective than “making the model read less.”
- Cache hits only apply to repeated prefix content. The prefix must be byte-for-byte stable; any timestamp, random ID, or dynamic ordering will break the cache.
- Long context is not free. Retrieval-augmented generation (RAG) must compress retrieved fragments down to what is truly relevant.
3. Cache Hit Rate: The Biggest Lever in the Low-Price Era
With long-term low prices combined with cache discounts, the cache hit rate becomes the primary cost factor. In practice, the following is recommended:
- Place the system prompt, tool definitions, domain glossary, and few-shot examples at the very front of the prompt to form a stable prefix.
- Put volatile content in the conversation (user input, retrieval results) at the end to avoid polluting the prefix.
- For repetitive tasks such as document summarization and code review, build a mapping from “content fingerprint → prefix template” and keep the template unchanged.
Illustrative code (pseudocode, structure only):
def build_prompt(system_block, retrieved, user_query):# system_block must be stable: no timestamps or random IDsprefix = system_block + render_fewshot(FEWSHOT_SET)return prefix + "\n" + retrieved + "\n" + user_query
4. Output-Side Optimization: Structure and Length Constraints
The core way to reduce output cost is to constrain the output shape. Recommended practices include using a JSON Schema to constrain the response structure, setting explicit word or item limits, separating “explanation” from “conclusion” (conclusion first, optional explanation after), and using a smaller-parameter model for pre-filtering on small tasks.
{"type": "object","properties": {"verdict": {"type": "string", "enum": ["pass", "fail"]},"reasons": {"type": "array", "items": {"type": "string"}, "maxItems": 3}},"required": ["verdict"]}
5. Cost Control at the Architecture Level
- Tiered routing: route between lightweight and stronger models based on task complexity, using rules or a small model as the discriminator.
- Semantic caching: cache results for high-frequency similar questions, with a similarity threshold and expiration policy.
- Batching and asynchrony: send offline tasks through batch channels and keep only necessary calls in interactive paths.
- Observability: instrument each request to record input, output, and cached token counts, forming a cost dashboard and alerts.
- Vendor neutrality: wrap calls through a unified abstraction layer to avoid tight coupling between business code and a single interface, making migration and price negotiation easier.
6. Capacity and Budget Planning
It is recommended to use a “monthly token budget” instead of a “monthly spend budget,” because the former scales linearly with business volume. The approach is to first measure the average token consumption per business action, then multiply by the expected call volume, and reserve a 30% buffer for retries and evaluations. At the same time, establish a regression evaluation set: any prompt or model change should be compared on two dimensions—“quality metrics + cost per request”—to prevent cost reduction from causing a collapse in quality.
7. Conclusion
Permanent low prices are not simply a cost dividend but a window of opportunity for an architecture upgrade. Only by building the billing model, caching strategy, output constraints, tiered routing, and observability system together can you reinvest the savings into higher-value capabilities in an environment of continuously falling prices. If an organization already uses cloud products from Baidu AI Cloud, the methods above can be mapped onto general capabilities such as object storage, message queues, container platforms, log services, and monitoring and alerting to form a complete AI engineering loop.
