AI Coding Cost Control: A Dual-Core Planning and Execution Workflow

    AI coding tools can improve throughput, but teams often face rising API costs and rework when the most expensive reasoning models are used for every task. This article introduces a dual-core workflow that separates planning from execution, enforces minimal and structured context, turns tool documentation into reusable context, adds automated quality gates, and uses cost observability and model routing to reduce the total cost per effective change.

    Many teams using AI coding tools go through a similar curve: first comes surprise, as completion, generation, refactoring, and test writing seem to boost productivity; then comes dependence, as almost every change is routed through a model; finally bills and rework arrive together—invocation costs rise, while code still requires substantial human review, debugging, and patching. The problem is often not that the model is insufficiently intelligent. It is that the workflow treats the most expensive reasoning capability as a universal tool: it is asked to do architecture planning, write repetitive boilerplate, and fix trivial type errors. Costs spiral, and efficiency may not improve.

    A more reasonable approach is to layer model capabilities, phase tasks, and turn context and quality control into engineering constraints. The conclusion first: use frontier models for planning, decomposition, risk identification, and acceptance criteria design; use cost-effective models for execution, batch changes, test completion, and documentation cleanup; then use strict context rules, standardized tool-context feeding, quality gates, and cost observability to turn AI coding from chat-based guesswork into an engineering process that can be measured, reused, and orchestrated.

    1. Behind the Bill Shock: The Productivity Paradox of AI Coding

    The first misconception about AI coding is equating a subjective sense of fluency with real productivity. Multiple public industry surveys and controlled experiments reveal a paradox worth attention: a substantial share of developers believe subjectively that AI improves their efficiency, but in controlled comparison tasks, developers using AI may actually spend more time. Public surveys also commonly surface two types of feedback: AI-generated solutions are “almost correct, but not quite correct”; and debugging AI-generated code can take longer than writing it from scratch. The former creates review cost, the latter creates localization cost, and together they form hidden cost.

    This is very similar to the Pareto principle: most daily tasks can indeed be accelerated by AI, but a small number of boundary problems that are “almost right” consume a large amount of time. A type mismatch, a missed boundary condition, or an async timing error can force developers to repeatedly describe requirements, paste logs, and ask the model to correct itself. Each conversation looks short, but cumulatively it becomes a double expense in bills and time.

    There are usually three root causes:

    1. Uncontrolled task granularity: Planning, implementation, review, and debugging are mixed into one conversation. The model acts as both architect and assembly-line worker, and the context becomes increasingly dirty.
    2. Unbounded context: Large numbers of unrelated files, historical conversations, and logs are inserted at once, diluting key constraints. The output looks reasonable but does not satisfy real constraints.
    3. Missing quality gates: Model output goes directly into human review without automated interception such as static checks, type checks, unit tests, or diff review. Rework naturally increases.

    Therefore, the core of cost reduction is not simply switching to a cheaper model. It is letting different models do the right work at the right stage.

    2. Dual-Core Workflow: Separate Planning from Execution

    The AI coding process can be split into two core roles:

    • Planning core: Carried by a frontier model. It is responsible for requirement clarification, task decomposition, interface design, risk lists, acceptance criteria, test strategy, and change boundaries. It does not necessarily write large amounts of code directly, but it must produce a structured plan.
    • Execution core: Carried by a cost-effective model. It is responsible for completing local implementation, boilerplate code, repetitive refactoring, test case completion, comment and documentation cleanup, and log and error message summarization according to the plan. It must work within clear boundaries.

    The key to this split is not model names but separation of responsibilities. The planning core outputs “what to do, why, where the boundaries are, and how to accept it”; the execution core outputs “small-step changes completed under constraints.” The planning core’s context should be short and precise, containing goals, constraints, relevant interfaces, and acceptance criteria. The execution core’s context should be narrow and accurate, containing only the file snippets, type definitions, test examples, and coding standards needed for the current task.

    A simplified pseudocode flow is as follows:

    1. plan = planner.generate(
    2. goal=requirements,
    3. constraints=architecture_constraints_and_coding_standards,
    4. interfaces=relevant_interface_signatures,
    5. acceptance=acceptance_criteria,
    6. risk=known_risks
    7. )
    8. for task in plan.tasks:
    9. context = build_minimal_context(task, relevant_file_snippets, test_examples, standards)
    10. patch = executor.generate(task, context)
    11. result = run_gates(patch) # static checks, type checks, unit tests, diff review
    12. if not result.passed:
    13. feedback = summarize_failure(result)
    14. patch = executor.repair(task, context, feedback)
    15. apply(patch)

    Cost reduction here comes from three aspects: the planning stage uses a strong model but makes few calls; the execution stage uses a cost-effective model with narrow context and small tasks; and failure repair has structured feedback, avoiding endless conversation.

    3. Context Rules: Keep Models Working Within Boundaries

    Context is not better when there is more of it. It should be “minimally sufficient.” It is advisable to write context management into team rules:

    • One task, one context: Each execution task keeps only the target file, relevant interfaces, failure logs, and acceptance criteria. Do not append entire historical conversations indiscriminately.
    • Structured summaries: Regularly compress long sessions into “goal, completed, not completed, constraints, risks, items to verify” to avoid context bloat.
    • File snippets instead of entire files: Provide only relevant functions, type definitions, and call sites to reduce noise.
    • Versioned standards: Coding standards, directory conventions, and error-handling conventions are fixed as short documents and used as reusable context.
    • Minimized failure information: Keep only error types, key stack traces, reproduction inputs, and expected outputs. Do not paste full-screen logs.
    • No implicit assumptions: Ask the planning core to list assumptions explicitly, and let the execution core work only on confirmed assumptions.

    The value of context rules is reducing the probability of “almost correct.” Model output is often unstable not because reasoning capability is insufficient, but because constraints are not clearly expressed. Structuring, localizing, and verifying constraints is more effective than simply increasing the context window.

    4. Tool Context Feeding: Turn Documentation and Interfaces into Callable Knowledge

    For the execution core to work reliably, it needs to be fed standardized tool context: interface signatures, data structures, error codes, configuration items, example calls, boundary conditions, and test examples. The focus here is not a specific protocol, but describing tool capabilities in a unified format so the model knows “what can be called, what the parameters are, what is returned, and how failures are handled.”

    A generic structure can be used:

    1. tool: create_task
    2. purpose: create a background task
    3. input:
    4. name: string, required, length limits enforced by business validation
    5. payload: object, required, must conform to task type definition
    6. timeout_ms: number, optional, default determined by configuration
    7. output:
    8. task_id: string
    9. errors:
    10. INVALID_INPUT: parameter validation failed
    11. DUPLICATE: duplicate task name
    12. examples:
    13. - input: {name: "sync", payload: {...}, timeout_ms: 30000}
    14. output: {task_id: "..."}

    Once tool context is fixed in this form, the planning core can design call order from it, the execution core can generate calling code from it, and quality gates can validate parameters against it. Documentation no longer scatters across chat records; it becomes an engineering asset that is searchable, reusable, and versionable.

    In a Baidu AI Cloud environment, general cloud capabilities such as log services, monitoring and alerting, and object storage can be combined to collect model invocation logs, task duration, failed retries, and cost labels into an observability loop. The key here is the combination of general capabilities, not dependence on a specific interface.

    5. Quality Gates: Move Debugging Cost Forward

    The biggest risk of AI-generated code is that it “looks right.” Therefore, after the execution core produces output, it should not go directly into human review. It should first pass automated gates:

    1. Formatting and static checks: Unify style and intercept obvious unused variables, null-pointer risks, and excessive complexity.
    2. Type checks: Let the type system handle the first round of correctness validation.
    3. Unit tests and regression tests: The planning core designs the test strategy first, the execution core completes tests, and the gates run the tests.
    4. Diff review: Review only the change set. Require each patch to be small, rollbackable, and explainable.
    5. Security checks: Check sensitive information, permission boundaries, input validation, and dependency risks.
    6. Failure summary feedback: After a gate fails, return structured failure information to the execution core for repair instead of asking developers to relay it manually.

    The purpose of quality gates is to convert the hidden cost of “debugging AI code” into repeatable automated cost. One gate failure may take only seconds, while manual localization may consume tens of minutes. Over time, the stricter the gates, the less rework the execution core produces, and the lower the overall cost.

    6. Cost Observability and Model Routing

    Without measurement, there is no optimization. For each AI task, record: task type, model tier, input and output size, duration, retry count, gate pass rate, number of human interventions, and final change size. Use this data for model routing:

    • Planning, architecture, and complex defect localization: use frontier models, but limit call frequency.
    • Boilerplate code, test completion, documentation cleanup, and local refactoring: use cost-effective models.
    • Simple formatting, naming suggestions, and log summarization: prefer rule-based tools or lighter-weight approaches.
    • High-failure-rate tasks: return to the planning core for re-decomposition instead of letting the execution core repeatedly try and fail.

    The goal of cost optimization is not to push the per-call price to the lowest level, but to reduce the “total cost per effective change.” A cheap model that fails repeatedly may cost more in total; a strong model used only for key planning can be more economical.

    7. Rollout Path and Common Anti-Patterns

    A rollout can follow three steps: first, choose a highly repetitive task domain and establish planning templates, context templates, and gates; second, split execution tasks into smaller ones and track gate pass rates and human intervention rates; third, adjust model routing and context strategy based on data.

    Common anti-patterns include: dumping an entire repository into the model at once; letting the execution core improvise without acceptance criteria; planning-core output that is too abstract for the execution core to implement; endless follow-up conversations after failures without recycling context; and focusing only on per-call price while ignoring rework and review costs. Avoiding these anti-patterns is necessary for the dual-core workflow to become truly stable.

    Conclusion

    The cost problem of AI coding appears on the bill, but its substance is the workflow. Treating the most expensive model as a universal tool leads to high costs and uncertain output. Separating planning from execution, constraining input with context rules, feeding knowledge through tool context, intercepting rework with quality gates, and driving routing with cost observability can significantly reduce costs and shorten debugging time without sacrificing quality. Developers should not become callers chased by bills; they should become orchestrators of AI workflows.