A Practical Guide to Coding Agents with Million-Token Contexts
This article explains how to build coding agents that deliver verifiable results rather than just plausible patches. It covers layered context engineering, retrieval and trimming, fact-versus-assumption separation, bounded tool-calling loops, failure recovery and regression control, and evaluation gates for release, using open-weight models and general-purpose cloud capabilities.
1. The Real Problem: A Patch Is the Start, Verification Is the Finish
Hand a coding tool a bug ticket and it will usually produce a plausible-looking change within seconds. The hard part comes afterward: once tests fail, does the change still converge? Are dependencies complete? Are boundary conditions covered? Is regression risk under control? Engineering-wise, the goal has to shift from “generate a patch” to “deliver a verifiable result.”
Long context matters because it lets the model see more engineering facts at once: directory structure, build scripts, commit history, failure logs, and related test cases. A larger context window does not automatically improve accuracy. It only solves the prerequisite of information availability; the process design still has to turn that information into reliable actions.
2. Context Engineering: Select, Don’t Stuff
2.1 Layered Organization
Split the context into four layers: the task layer (ticket, reproduction steps, expected behavior), the code layer (relevant files and interface definitions), the evidence layer (logs, stack traces, failing assertions), and the constraint layer (coding standards, dependency versions, build method). Use explicit separators between layers so the model does not confuse facts with speculation.
2.2 Retrieval and Trimming
Start with a coarse filter based on keywords and call relationships, then trim down to the “minimal reproducible set.” Sketch of the logic:
def build_context(issue, repo_index, logs):files = repo_index.search(issue.keywords, top_k=8)snippet = repo_index.expand_callers(files, depth=2)return {"task": issue.to_prompt(),"code": snippet,"evidence": logs.tail(200),"constraints": repo_index.build_policy(),}
2.3 Separate Facts from Assumptions
Require the model to explicitly list “known facts” and “assumptions to verify” before proposing a solution. This noticeably reduces the chance of invented interfaces and parameters.
3. Tool-Calling Orchestration: Make the Agent Actually Act
Open-weight models combined with tool calling are what turn an agent from an advisor into an executor. The minimal toolset is file read, file write, command execution, test execution, and log retrieval. For orchestration, use a bounded loop:
- Read the context and form assumptions;
- Generate the minimal change;
- Run unit tests and static checks;
- If it fails, parse the error and feed the evidence back into the evidence layer;
- Stop at the step or time limit and output a diagnostic report.
for step in range(MAX_STEPS):obs = run_tests(patch)if obs.passed: return patch, obs.summarycontext.evidence += obs.failurespatch = model.revise(context)
The key is “bounded” and “observable”: log every step so runs can be replayed and audited.
4. Failure Recovery and Regression Control
Failure is not the problem; retrying without direction is. Three strategies help: first, error classification (compile errors, assertion failures, timeouts, missing environment), with a different repair path per category; second, rollback points that preserve the last state that passed tests; third, impact analysis that uses the call graph to assess the blast radius of a change and automatically adds related regression cases.
At the environment level, use a container platform for a consistent build environment, object storage for build artifacts, and a logging service with monitoring and alerting to track resource usage and duration of long-running tasks. These general cloud capabilities substantially reduce the odds of “works locally, fails in production.”
5. Evaluation and Release Gates
Evaluation should cover four dimensions: functional correctness (test pass rate), minimality (lines and files changed), explainability (whether a root cause is given), and stability (variance across repeated runs). Release gates can be set as: all new and existing cases pass, static checks report no new warnings, and no performance regression on critical paths.
6. Conclusion
Million-token contexts and open weights give coding agents the basic conditions to handle real engineering noise. Actually finishing the dirty work depends on the engineering methods: layered context, tool orchestration, failure recovery, and quantitative evaluation. Built on general-purpose container, storage, and logging and monitoring capabilities from Baidu AI Cloud, teams can stand up a reproducible, auditable coding agent pipeline faster.
