LLM API Cost Optimization: A Practical Guide
This guide explains how to reduce large language model API costs after recent price changes. It breaks down the three token types—regular input, cached input, and output—shows how to calculate an effective price using your real workload distribution, and covers model tier routing, prefix caching, output constraints, context governance, batch processing, semantic caching, observability, and avoiding vendor lock-in.
1. Why You Must Rebuild Your Cost Model After a Price Change
Recent price adjustments have noticeably changed LLM API costs. The old approach of relying on a single low-cost model for everything is no longer reliable. The reason is simple: billing is never based on a single “price per million tokens” figure. Instead, there are three independent price curves—input, cached input, and output—layered with context-length tiers, batch discounts, and the hidden cost of failed retries.
More importantly, output tokens usually cost several times more than regular input tokens. Across many providers, this multiplier falls in the 4x to 6x range. This means a model that looks cheap can produce a much higher bill than expected if the workload involves long-form generation, code completion, or large structured JSON output. Conversely, a model with a higher unit price can be cost-effective if the workload is mainly short inputs, high cache hit rates, and very short outputs.
So the first step is not comparing unit prices, but establishing your own cost baseline.
2. The Cost Semantics of the Three Token Types
Understanding the billing structure is a prerequisite for making architecture decisions.
- Regular input tokens: Billed on every request. This is the main cost driver in long-context scenarios.
- Cached input tokens: When a request prefix matches previous content, the matched portion is typically billed at around 10% of the regular input price (commonly on the order of 0.1x). This is the most important cost-reduction lever for long system prompts, long-document Q&A, and fixed tool definitions.
- Output tokens: The most expensive, and they cannot be cached or reused. This is the only purely incremental cost.
This leads to a practical cost estimation formula:
Cost per request =input tokens × P_in+ cached input tokens × P_in × 0.1+ output tokens × P_outwhere P_out ≈ 4 ~ 6 × P_in
Plugging in your real workload distribution gives you an effective unit price rather than the list price on a pricing page.
3. Choosing Model Tiers by Task Type
Abstracting away specific products, the models on the market fall into three capability/price tiers: flagship reasoning, balanced, and lightweight high-throughput. The sensible approach is not to pick one, but to build routing.
- Flagship tier: For complex reasoning, long-chain agent planning, and difficult code refactoring. This should typically account for less than 5% of traffic.
- Balanced tier: Handles the majority of routine conversation, summarization, classification, and standard code generation.
- Lightweight tier: Takes on intent recognition, field extraction, format conversion, and high-concurrency batch labeling.
An illustrative routing logic:
def route(task):if task.difficulty >= 3 or task.requires_long_chain:return "flagship"if task.batch and task.output_tokens < 200:return "light"return "balanced"
In practice, keep the model identifier as a configuration item rather than hard-coding it, so you retain canary release and fast rollback capabilities.
4. Five Cost-Reduction Tactics You Can Apply Immediately
- Prefix stabilization: Put the system prompt, tool definitions, and few-shot examples at the very front of the request and keep them byte-for-byte unchanged to maximize cache hits.
- Output constraints: Use explicit word or field limits and structured schema constraints to compress output length. Savings on the output side are several times those on the input side.
- Context governance: In retrieval-augmented scenarios, rerank and remove redundancy first, injecting only truly relevant fragments instead of stuffing an entire document into the context.
- Batching and asynchrony: Use batch endpoints for offline tasks, which usually offer significant discounts while smoothing peak concurrency.
- Semantic caching: Build a result cache layer for high-frequency repeated questions. A cache hit returns directly, saving both money and latency.
5. Observability and Cost Governance
Without measurement, there is no optimization. Collect the input, cached, and output token counts, latency, and number of failed retries for each call, and aggregate them by business line, scenario, and tenant to produce daily reports and budget alerts. Also set a per-request token limit and circuit-breaker thresholds to prevent abnormal inputs from causing billing spikes.
6. Avoiding Vendor Lock-In
Prices change, and so do capabilities. Architecturally, you should do three things: build a unified abstraction layer over the calling interface to hide differences between providers; maintain a small evaluation set and score on three dimensions—accuracy, latency, and cost—rather than unit price alone; and retain the ability to run multiple providers in parallel and switch with one action. If you use Baidu AI Cloud services, you can combine general capabilities such as object storage, message queues, container platforms, log services, and monitoring alerts to build a complete calling gateway, asynchronous queue, and cost dashboard for unified governance and elastic scaling.
7. Conclusion
A price increase is not the end of model selection, but the starting point for re-examining your architecture. Break cost into three curves—input, cache, and output—calculate the effective unit price using your real workload distribution, and keep optimizing through tiered routing, prefix caching, output constraints, and observability-driven governance. Only then can you consistently achieve real cost-effectiveness amid price fluctuations.
