A Practical Guide to Optimizing LLM API Costs
This article explains why LLM API spending often exceeds the initial budget and presents a practical approach to controlling it. The core idea is to split planning and execution across two model tiers: use a stronger reasoning model for planning and a faster, cheaper model for implementation. It also breaks down the multiplicative nature of API cost, outlines engineering safeguards such as version control and automated checks, and describes how to make token usage and spending observable. Readers will learn where cost overruns originate, how to structure a tiered workflow, and which common misconceptions to avoid.
1. Background: Why API Spending Often Exceeds the Budget
A pattern that keeps repeating in real AI-assisted development projects: a clear token budget is set at kickoff, yet actual spending at the end of the project is often several times the estimate. In one real engagement, the account was topped up with 70 CNY and 64 CNY was ultimately consumed. A strong reasoning model accounted for roughly 2,788 requests and 673 million tokens, while the fast model handled only 999 requests and 314 million tokens. These numbers point to a key issue: cost overruns do not come from unit price, but from a mismatch between model selection and task type.
The strong reasoning model (referred to below as the “planning model”) offers better complex reasoning and solution generation, but costs more per token and responds with higher latency. The fast model (referred to below as the “execution model”) responds quickly and costs less, making it suitable for coding tasks with a high degree of certainty. When developers hand every stage to the planning model — requirement clarification, architecture design, code writing, and debugging — token consumption scales linearly or even exponentially.
2. Core Approach: Separate Planning from Execution
The solution can be reduced to one principle: let the strong model make decisions, and let the fast model do the execution.
In practice, this relies on the “mode switching” capability in AI coding tools: the planning model is called in planning mode to produce a breakdown of functional modules, interface contracts, data structure designs, edge cases, and acceptance criteria. Once the plan is confirmed, you switch to build mode, the context is synchronized automatically to the execution model, and it implements the code strictly according to the agreed plan.
A schematic workflow looks like this:
Requirement input↓[Planning mode / planning model]- Module breakdown and dependency ordering- Interface and data structure definitions- Risk points and acceptance criteria↓ Human confirmation of the plan[Build mode / execution model]- Implement module by module according to the plan- Commit in small steps, verify continuously↓Integration and regression testing
The value of this pattern is that a high-quality planning output constrains the freedom of the execution stage and reduces rework caused by the execution model “improvising.” Feedback from practice shows that with comparable token consumption, overall effort dropped from two days to one and a half days, and the most noticeable change was a large reduction in waiting time.
3. Cost Structure: The Multiplicative Effect of Token Volume and Unit Price
Cost can be simplified to a multiplicative relationship: total cost ≈ Σ(requests × tokens per request × unit token price). Most developers focus only on the third term and overlook the fact that the first two are also controllable.
- Request count: frequent small requests amplify fixed overhead; merging context and cutting unnecessary round trips reduces the count significantly.
- Tokens per request: repeatedly carrying long context is a hidden cost driver, so clearing irrelevant history in time and isolating sessions by module is important.
- Unit price: model tiering shifts most of the token volume onto the cheaper model.
A common misconception is “let’s try the strong model first.” Unrestrained use of the planning model early on is the main cause of budget overruns; switching to the execution model for the bulk of the coding later improved both efficiency and cost. This shows that the critical point for cost optimization is early in the project, not a late-stage fix.
4. Quality Safeguards: Known Weaknesses of the Execution Model and Engineering Constraints
It must be stated objectively that the execution model still has issues in file editing scenarios, such as data loss, generating syntax errors, and needing a second reminder before it corrects a mistake. Engineering constraints therefore cannot be skipped:
- Mandatory version control: create a local Git repository at project start, so any automated change happens under conditions where it can be rolled back.
- Small commits: commit immediately after finishing each small module, keeping the rollback granularity at the smallest unit.
- Plan first: freeze interfaces and data structures in the planning stage to avoid repeated changes during execution that trigger cascading edits.
- Diff review: review the diff of automatically generated changes, focusing on deleted lines and full-file rewrites.
- Automated validation: make compilation, static checks, and unit tests a mandatory gate before each commit.
A schematic commit rhythm:
git init# After completing one modulegit add <module-path>git commit -m "feat: complete core user module capabilities"
5. Budget Control: Turning Cost into an Observable Metric
Token usage and cost should be brought into the everyday observability system, with request counts, token volume, and spending tracked daily or per module, plus threshold-based alerts. When daily consumption crosses the budget progress line, model tier adjustment should be triggered immediately. If you rely on general cloud capabilities, usage collection and notifications can be built with logging services, monitoring and alerting, and message queues to close the loop.
It is also worth building a “budget-to-task” mapping: allocate the remaining budget across modules, keep more planning-model quota for modules with high uncertainty, and route deterministic modules to the execution model as much as possible.
6. Common Misconceptions and Lessons Learned
- Misconception 1: a more expensive model always saves time. On deterministic tasks, the fast model’s response advantage often offsets the reasoning gap.
- Misconception 2: longer context means smarter output. Redundant context adds cost and dilutes key constraints.
- Misconception 3: write code first and add the plan later. Without plan constraints, the execution model’s mistakes get amplified.
- Lesson 1: adopt the tiered pattern as early as possible. The earlier you find the right combination, the more you save.
- Lesson 2: treat version control as a prerequisite, not an option.
- Lesson 3: constrain output with acceptance criteria rather than natural-language descriptions.
In summary, cost governance for LLM APIs is essentially an engineering methodology: match model tiers to task characteristics, freeze the plan to reduce rework, backstop quality with version control and automated validation, and close the budget loop with usage observability. Only by combining all four can you deliver reliably at a controlled cost.
