LLM API Cost Optimization: A Practical Guide to Cutting Token Spend After Price Drops
As major cloud providers cut large language model API prices, many teams focus on the headline numbers and miss the real cost drivers. This guide breaks down the three billing components—uncached input, cached input, and output—and explains why output should be tackled first, followed by improving cache hit rates, and only then switching to cheaper models. It covers four practical levers: output governance, prompt restructuring for prefix caching, tiered routing between lightweight and high-spec models, and context trimming. It also outlines the observability metrics you need, a safe migration and canary rollout path, and common pitfalls to avoid.
1. What a Price Cut Actually Signals
A cloud provider recently released two same-generation LLMs at once. The higher-spec version cut input and output unit prices by roughly 20%, and cached input by roughly 60%. A lightweight version kept the previous generation’s pricing: $2, $0.1, and $10 per million tokens for input, cached input, and output respectively. Both models are available in the same subscription plans and developer interfaces, and developers can call them directly by model identifier.
Abstracted from this announcement, there are three signals:
- The cuts are not uniform. Output dropped about 20% while cached input dropped about 60%, indicating the provider is actively steering developers toward context reuse rather than repeated uploads.
- The product matrix is now tiered. With a high-spec and a lightweight version coexisting, “choosing a model by task difficulty” has shifted from an optimization trick to part of architecture design.
- Billing is getting more granular. Three pricing tiers—input, cached input, and output—mean cost governance must be refined down to the request level.
2. Read the Bill Structure First: What the Three Billing Items Mean
Most teams only watch “price per million tokens,” but the actual bill has three parts:
- Input (cache miss): The billed portion of the full prompt, and often the most overlooked source of waste. System prompts, tool definitions, and conversation history that are re-sent in full every time cause costs to grow linearly.
- Cached input: Billed at a significantly lower rate once a prefix cache is hit. It rewards a prompt structure of “stable prefix + variable suffix.”
- Output: The most expensive tier. Verbose answers, uncontrolled reasoning displays, and repetitive restatements all inflate the bill directly.
A rough but useful estimation formula:
cost per call ≈ (uncached input tokens × input price+ cached input tokens × cached price+ output tokens × output price) / 1,000,000
The conclusion is straightforward: the optimization order should be “compress output first, then improve cache hit rate, and only then consider switching to a cheaper model,” because the output price is typically several to tens of times the cached input price.
3. Four Levers for Cost Optimization
3.1 Output Governance: The Fastest Payoff
- Explicitly constrain output format and length (e.g., “return JSON only, no more than 200 words”);
- Turn off unnecessary reasoning displays and repetitive summaries;
- Generate long tasks in segments to avoid producing large blocks of text at once and then rewriting them repeatedly.
3.2 Improve Cache Hit Rate: Restructure the Prompt
The prerequisite for a cache hit is a fully identical prefix. So put volatile content at the end:
[fixed] system role + tool/function definitions + output spec + a few examples[variable] the user's current-turn input, retrieved material, timestamps, etc.
A common anti-pattern is concatenating timestamps, random IDs, and retrieved snippets at the front, which makes the prefix different on every request and invalidates the cache entirely.
3.3 Tiered Routing: Let the Lightweight Model Handle Most Traffic
Start by using the lightweight version for classification, extraction, formatting, rewriting, and similar tasks, and escalate only complex reasoning and long-chain tasks to the high-spec version. Example implementation:
def route(prompt: str, task_type: str) -> str:if task_type in {"classify", "extract", "format"}:return "light-model"if len(prompt) > 60000 or task_type == "reasoning":return "pro-model"return "light-model"
The routing key should be passed explicitly by the business side rather than guessed from keywords, to avoid quality regressions caused by misclassification.
3.4 Context Slimming: Trim Retrieved Snippets Before They Enter the Prompt
Deduplicate, compress, and rank retrieved content by relevance, keeping only the necessary passages. Context length is roughly linearly related to cost, so trimming 20% of redundancy often yields a proportional drop in input cost.
4. Observability: No Measurement, No Optimization
Cost governance must be built on data. At a minimum, collect these dimensions: token consumption split by model and business line, cache hit rate, average output length, cost per request, and number of failed retries. You can use general-purpose logging services, monitoring and alerting, and message queues to collect and aggregate this data, and set threshold alerts for cache hit rate and unit cost.
A minimal call wrapper example:
def call_model(client, model, system, user, max_tokens=512):messages = [{"role": "system", "content": system}, # keep stable to favor prefix reuse{"role": "user", "content": user},]resp = client.chat.create(model=model, messages=messages, max_tokens=max_tokens)usage = resp.usagelog_cost(model, usage) # log input / cached input / output tokensreturn resp.choices[0].message.content
5. Migration and Canary Rollout: Keep Risk Acceptable
Switching models or changing prompt structure is a change and should follow a canary path: first compare quality on an offline evaluation set, then roll out to a small traffic slice while watching cache hit rate and unit cost, and finally go full traffic. Always keep a rollback switch, and validate outputs structurally to prevent format drift from breaking downstream parsing.
6. Common Pitfalls
- Watching only unit price and not usage structure, which leads to optimizing in the wrong direction;
- Cutting all traffic over to the lightweight model to save money, degrading quality on complex tasks and creating higher rework costs;
- Frequently changing the system prompt, so the cache can never hit;
- Lacking cost attribution by business line, making it impossible to locate the source of waste.
7. Conclusion
A price cut is an external windfall; real cost advantage comes from engineering capability: precise billing awareness, stable prefix design, clear tiered routing, and a solid observability system. Get these four things right, and even if the price structure changes again, the team can adapt quickly and keep unit call cost within a controllable range. If you already use relevant general capabilities from Baidu AI Cloud (such as logging services, monitoring and alerting, or object storage), you can reuse the existing data pipeline to build a cost dashboard and avoid duplicate work.
