LLM API Cost Optimization: A Dual-Model Planning and Execution Workflow

    This article explains why LLM API spending often exceeds budget and presents a practical dual-model approach that separates planning from execution. It covers the root causes of high early-stage consumption, how to route tasks between a stronger reasoning model and a faster lightweight model, how to pass context reliably between the two, and the engineering guardrails needed to manage the risks of lightweight models. It also outlines cost governance practices and a deployment checklist for stable delivery under a limited budget.

    1. The Problem: Why API Bills Spiral Past Budget

    A common pattern when building with LLM APIs is that the account balance runs dry before the project is finished. Many teams estimate only the labor cost at kickoff and overlook a key variable: the coupling between token consumption and model tier selection. Models with stronger reasoning cost more per call, while coding work tends to involve long contexts, multiple turns, and frequent invocations, which amplifies cost quickly.

    A typical consumption pattern looks like this: one model tier is called more than two thousand times and consumes hundreds of millions of tokens; another tier is called about a thousand times and consumes roughly half as many tokens; cumulative top-ups reach 70 RMB, of which 64 RMB has been spent. These numbers show two things. First, call count and token volume are not linearly related, because long-context turns significantly raise the cost per request. Second, the root cause of runaway cost is usually not unit price but the habit of using the most expensive model for every step.

    2. Root Cause Analysis: Four Sources of Early-Stage Overspending

    1. Tier mismatch. Handing solution discussion, code completion, documentation cleanup, and simple refactoring to the same high-capability model is like using heavy equipment for light lifting.
    2. No stage separation. Requirement clarification, architecture design, implementation, and self-test fixes are four different kinds of work with very different demands on reasoning depth and response speed, yet they share one configuration.
    3. Context bloat. As history keeps accumulating across turns, existing context is billed again on every turn, so long sessions see steadily rising costs.
    4. No budget gate. Without a daily or per-project token cap, and without a fallback strategy triggered near the threshold, overspending can only be discovered after the fact.

    3. The Core Approach: Dual-Model Collaboration That Separates Planning from Execution

    The idea is to split “thinking it through” from “writing it out” into two stages, each matched to a model with different strengths.

    • Planning stage: Use the model with stronger reasoning to produce module breakdowns, interface contracts, data structure designs, risks, and acceptance criteria. This stage consumes few tokens but demands high-quality conclusions.
    • Execution stage: Switch to a faster lightweight model to implement, modify, and self-test code against the confirmed plan. Context is synchronized automatically, with no manual copy and paste.

    A schematic workflow can be expressed as:

    1. Requirement input
    2. ↓
    3. [Planning mode] Strong reasoning model → module list + interface contracts + acceptance criteria
    4. ↓ Human confirmation (key checkpoint)
    5. [Execution mode] Fast model → module-by-module implementation + self-test
    6. ↓
    7. Commit and regression → move to the next module

    The benefits come from three directions. First, the expensive model handles only a small number of high-value decisions. Second, the high throughput of the fast model shortens waiting time. Third, once the plan is fixed, rework in the execution stage drops, indirectly reducing repeated token consumption. The most tangible result in practice is a clear reduction in waiting time, with work that originally took two days compressed to about a day and a half.

    4. Key Details for Context Handoff

    Whether dual-model collaboration works depends on reliable context transfer. Four points matter:

    1. Document the plan. Output from the planning stage must become structured text (modules, inputs and outputs, exception handling, acceptance points) rather than being scattered across the conversation.
    2. Converge context per module. When entering a new module, carry only the contracts and constraints that module needs, avoiding a full history dump.
    3. State explicit prohibitions. Write hard constraints into the execution-stage prompt, such as “do not change interface signatures” and “do not delete existing tests.”
    4. Define a fallback path. When execution results fail acceptance, fall back to the planning stage to revise the plan instead of letting the execution model trial-and-error repeatedly.

    5. Risk Control: Lightweight Models Are Not Risk-Free

    It must be acknowledged objectively that a less capable model may lose content, introduce syntax errors, or need a second prompt before fixing issues during file editing. Engineering-side guardrails are therefore required:

    • Local version control is the baseline. Set up a local code repository for the project and commit immediately after each small module is done, so any bad edit can be rolled back.
    • Small commits. The finer the commit granularity, the faster problems are located and the cheaper rollback is.
    • Automated checks up front. Use formatting, static analysis, and unit tests as three gates so syntax and convention issues surface before commit.
    • Two-person review for critical files. Keep a human confirmation step for high-risk files such as configuration, permissions, and data migrations.

    6. Cost Governance: Turning Budget into an Executable Policy

    1. Tiered routing. Build a mapping table from task type to model tier: simple completion goes to the lightweight tier, architecture decisions go to the strong reasoning tier.
    2. Context compression. Clear invalid history promptly and replace original text with summaries when necessary to reduce repeated billing.
    3. Budget threshold alerts. Set dual thresholds for daily and total project consumption, with automatic downgrade and notification as thresholds approach.
    4. Effect measurement. Record call count, token volume, rework rate, and delivery cycle together, so decisions are not based on unit price alone.
    5. Observability support. Use general-purpose logging and monitoring/alerting capabilities on the cloud to continuously observe call volume and consumption trends, so optimization is evidence-based.

    7. Deployment Checklist

    • Define tier routing rules and budget caps at project kickoff;
    • Document planning output and confirm it manually;
    • Converge context per module in the execution stage and write hard constraints into prompts;
    • Have the full set of local repository, small commits, and automated checks in place;
    • Review token structure and rework causes weekly, and keep adjusting the routing policy.

    In short, LLM API cost optimization is not about finding a cheaper model, but about placing each type of task in the right tier and using engineering discipline to cover the risk from capability differences. Dual-model collaboration that separates planning from execution, combined with version control and an observability system, is a pragmatic path to stable delivery under a limited budget; building a unified call, logging, and monitoring pipeline on top of relevant Baidu AI Cloud capabilities can also make this approach easier to sustain over the long term.