Frontier Models Are Expensive? Use Context Engineering for Low-Cost, High-Quality Coding
The highest-reasoning coding models are often too costly per token to run around the clock, while cheap lightweight models handle single-point edits but drift on cross-file work. This article argues the real problem is a task–capability mismatch, not a model ceiling, and shows how to split coding into four layers (context aggregation, solution design, code execution, verification), route each layer to a model that fits its cost profile, and connect them with a three-stage plan–design-execute workflow. You will get the exact prompts for each stage, a JSON handoff contract for keeping context consistent across sessions, four metrics for evaluating the pipeline, and the pitfalls that make it break down.
1. The Real Problem: A Task–Capability Mismatch, Not a Weak Model
Most teams hit the same contradiction in day-to-day work with coding agents. The strongest reasoning models are billed by usage and are too expensive to support high-frequency, all-day coding. Budget-friendly lightweight models handle single-point rewrites and local refactors well, but as soon as a request spans multiple files and modules, three symptoms show up:
- Requirement drift: the model latches onto the literal keywords in the request, ignores implicit constraints, and produces a plan that looks complete but goes the wrong way.
- Missing context: in long contexts it drops details, skipping critical files, dependency versions, or existing abstractions, and ends up reimplementing things.
- A high rate of low-level errors: type mismatches, missing imports, unhandled edge cases, and failing tests keep recurring.
It is worth stressing that these symptoms are not simply evidence of a model’s capability ceiling. They are mostly the result of a mismatch between task granularity and the way context is supplied. Handing a large, vague requirement to any model in one shot amplifies uncertainty. Split the task to an appropriate granularity, feed the model filtered and structured facts, and the same model’s output quality usually improves noticeably.
So the question this article addresses is not “which model should I pick,” but how to design a division of labor: let cost-controlled models do what they are good at, and spend the scarce high-quality reasoning budget only where genuine deep thinking is required.
2. Breaking Down the Work: Four Sub-Capabilities in Coding
Designing a division of labor starts with understanding what the task is actually made of. A complete coding loop breaks into four layers, each with very different demands on the model:
| Layer | Concrete work | Capability required | Cost sensitivity |
|---|---|---|---|
| Context aggregation | Retrieving relevant files, locating errors, mapping dependencies and call chains | Instruction following, information extraction and organization | High (frequent, batchable) |
| Solution design and architectural reasoning | Diagnosing root cause, defining the change boundary, assessing risk and alternatives | Long-chain reasoning, tradeoff judgment | Low (infrequent, high value) |
| Code execution and rewriting | Applying the agreed plan, adding tests, updating configuration | Syntactic accuracy, local rewriting | High (many iterations) |
| Verification and regression | Running tests, reading logs, locating failures, deciding whether to continue | Tool use, factual judgment | Medium |
The key insight: context aggregation and code execution are the two layers a cheap model can cover efficiently, while the solution design layer is where the expensive reasoning budget is genuinely worth spending. The traditional approach hands the entire chain to the strongest model, which means using the most expensive resource for information gathering and mechanical rewriting. The return on that investment is poor.
3. The Core Mechanism: A Three-Stage Plan–Design–Execute Workflow
3.1 Stage 1: Aggregate context in the coding agent’s planning mode
Start in an agent tool that is directly connected to the codebase and enable planning mode (analysis only, no edits). Give the lightweight model one clearly structured instruction that asks for a list of facts rather than a solution:
You are a codebase analysis assistant. Current goal: <one-sentence requirement description>Output facts only. Do not give modification suggestions:1. List of relevant file paths, each annotated with its relation to the goal (entry / implementation / config / test)2. A responsibility summary for each key file (no more than 3 lines)3. Relevant type definitions, interface signatures, and input/output contracts of public functions4. The complete error stack or exception message, plus the call site where it first appears5. Dependency version constraints (verbatim excerpts from build files)6. Locations of existing similar implementations, to avoid reinventing themUse a fixed Markdown structure. Mark any information you cannot find as "not found".
Three points matter in this prompt: restrict the output to facts, mandate a fixed structure, and require gaps to be marked explicitly. The third one matters most. Making the model openly admit “not found” is far safer than letting it confidently invent interface signatures.
Planning mode looks verbose and slow, but its value is that it front-loads uncertainty. Rather than editing code directly and then burning dozens of patch rounds on trial and error, spend a few minutes pinning down the context first. In real projects, one extra planning pass up front typically saves several times that amount in verification and rollback cost.
3.2 Stage 2: Hand the aggregated result to a strong reasoning model for design
Copy the structured context produced above into a model with stronger reasoning that can be used on a pay-per-call basis. (Most cloud providers offer metered model APIs, and such models can also be used through a browser-based interface or a standalone inference platform, with no long-running subscription required.) Ask it for an executable change contract:
Here are the code facts and the requirement (see above). Output:1. Root-cause judgment with an evidence chain, citing the specific files and code locations that support the conclusion2. Change plan: list items one by one as "file -> change point -> rationale"3. Compatibility risk assessment: which callers are affected, and what needs to be adjusted in sync4. Verification plan: which test cases to add or modify, and what behavior to assert5. If more than one viable path exists, compare them and give a recommendationConstraint: do not output complete code. Output change descriptions precise down to the "function level + behavior level".
“Do not output complete code” is an easily overlooked but very effective constraint. Once the design layer starts generating large blocks of code, its attention budget gets diluted by syntax details and architectural judgment actually degrades. Leaving code generation to the execution layer matches the intent of the division of labor.
3.3 Stage 3: Back in the codebase, land changes in small steps
Paste the design back into the agent’s planning mode and let it fill in the details and produce fine-grained execution steps based on the context it already holds. Then switch to execution mode and follow a one change point at a time rhythm:
- Have it modify only item 1 in the plan, and forbid refactoring any other code along the way;
- Run the build and tests immediately; if they fail, feed the complete error output back to it for a fix;
- Once that passes, move on to item 2, and so on, item by item;
- Commit after each logical unit so you always have a rollback boundary.
This constraint is the most practical defense against problems like an agent stubbornly inverting a requirement, or dropping items in long contexts. Lightweight models have a clearly higher success rate on narrow tasks than on broad ones, so cutting a large change into small steps is itself an engineering-level compensation for the model’s weaknesses.
4. Context Engineering: The Methodology at the Core of This Workflow
The workflow works because it converts a “model selection problem” into a context supply problem. It boils down to four practical principles:
- Make the context budget explicit. Treat the context window as a finite resource. Prioritize high-information-density content — interface contracts, type definitions, verbatim error messages, build constraints — and cut large blocks of irrelevant code and redundant logs.
- Layer and compress. Compress raw material before it reaches the reasoning layer: prune the directory tree to branches relevant to the requirement, keep only the lines around the error in logs, and keep only function signatures and key fragments from long files. Compressed material is typically an order of magnitude smaller, without a noticeable drop in retention of critical information.
- Turn intermediate artifacts into contracts. Handoffs between the three stages must be fixed-structure text or JSON, not free-form conversation. Structured handoffs make it possible to localize an error to a specific layer, and make scripted transitions easier.
- Facts over speculation. Explicitly requiring “say not found if you did not find it” in every prompt significantly reduces the debugging cost caused by hallucinated interfaces and invented parameters.
{"requirement": "Add pagination parameters to the order export API","facts": {"entry_files": ["src/api/order_export.py", "src/routers/order.py"],"type_contracts": ["OrderQuery(pydantic): page:int, size:int, ..."],"errors": ["TypeError: unexpected keyword argument 'offset'"],"deps": ["fastapi==0.11x", "sqlalchemy==2.0.x"],"gaps": ["No existing pagination tests found"]},"plan_version": "v3","changed_items": [1, 2],"verification": {"build": "pass", "tests": "2 failed / 31 passed"}}
Using this JSON as the single contract between stages, with an incrementing version number, keeps context consistent even across multiple sessions.
5. How to Measure: Don’t Evaluate the Process by Feel
Any process change has to be measurable, or you cannot tell whether it is worth it. Four metrics are worth tracking:
- First-pass task success rate: the share of change points that pass tests with no manual intervention.
- Average rework rounds: the number of model interaction rounds needed to take a single requirement from request to merge.
- Cost per unit of work: model invocation cost normalized per thousand lines of effective change, or per merged requirement, rather than looking at unit price alone.
- Human intervention density: the number of manual corrections required per completed change point.
A healthy signal is that context aggregation and execution are handled by low-cost models, while the design stage calls a strong reasoning model only for genuinely complex requirements. The vast majority of everyday work — changing a field, adding a parameter, filling in tests, adjusting configuration — does not need top-tier reasoning. Sending all of it to the strongest model just wastes cost on low-value judgment.
6. Common Pitfalls and How to Avoid Them
- Treating planning mode as skippable. Saving a few minutes of planning often costs tens of minutes of debugging.
- Not compressing the handoff material. Pasting a whole repository crowds out the reasoning budget and drowns critical constraints.
- Requesting several change points at once. Lightweight models show a clearly higher error rate when pursuing multiple goals in parallel; single-point progress is steadier.
- No deterministic verification. Without a build and test suite acting as an objective referee, correctness comes down to human eyeballing, and the process degrades into blind editing.
- Ignoring deployment and compliance differences. For a self-hosted open-source model, evaluate GPU resource usage, concurrency and throughput, monitoring and alerting, and log retention. For a cloud-hosted model service, watch for API protocol compatibility, key management, call quotas, and data boundaries. Platforms such as Baidu AI Cloud typically bundle model services, compute platforms, object storage, logging, and monitoring as general capabilities that can be combined per scenario — which helps avoid binding a critical path to a single implementation.
7. Summary
The right way to use an expensive reasoning model is not to use it everywhere, but to place it precisely. Break coding work into four layers — context aggregation, solution design, code execution, and verification/regression — let low-cost models carry the frequent organizing and rewriting, let strong reasoning models carry the infrequent but high-value judgment and design, and then tie the three stages together with planning mode, contractual intermediate artifacts, and deterministic tests. That gets you a large reduction in unit cost without a significant sacrifice in output quality.
The value of this approach goes beyond saving money. It forces developers to state requirements clearly, organize context properly, and define verification criteria up front — which is exactly where the real bottleneck lies in getting AI to write reliable code. Models will keep iterating and prices will keep falling, but the methodology of context engineering and task decomposition stays effective in the long run.
