Agent Context Governance: An Execution Sub-Agent Architecture in Practice
This article explains why raw terminal output overwhelms coding agents and how an execution sub-agent architecture solves the problem through context isolation, information compression, and single responsibility. It covers the call contract, why a small specialized model is sufficient, key data construction practices, a three-dimensional evaluation approach, and practical adoption steps. Readers will learn how to cut main-agent token usage while preserving task success rates.
1. The Problem: Terminal Output Is Overwhelming Coding Agents
A complete coding agent typically performs three kinds of actions: understanding requirements, modifying code, and verifying results. The text produced by the first two actions is relatively manageable. The real difficulty lies in the third: builds, tests, static checks, and runtime debugging. These stages must actually execute commands, and the length of command output is often beyond the agent’s control.
In the “direct execution mode” common in the industry, the main agent runs commands inside its own context and absorbs both standard output and standard error in full. Researchers generally consider this design rather primitive, and it creates three cascading problems.
First, token consumption is enormous. A single test run with verbose logging can easily produce tens of thousands of tokens; enabling debug-level logs or a framework’s detailed stack traces pushes the volume even higher.
Second, core information gets crowded out. What the agent actually relies on for subsequent decisions is code snippets, edit history, and task plans, yet these are pushed out of the effective attention range by lengthy logs.
Third, a snowball effect. The more interaction turns there are, the more each new round of terminal output compresses the available space, and the fewer actual repair steps the agent can take before hitting the limit.
The key observation is that what the main agent truly needs from a single execution is usually just a concise conclusion about “what happened”—for example, the first line of a stack trace identifying the error type, a list of failing test cases, or the test result summary table at the end of the log—rather than the complete raw echo.
2. The Core Idea: Isolate the Terminal into an Execution Sub-Agent
A proven effective architecture is to introduce an execution sub-agent: the main agent no longer touches the terminal directly. Instead, it hands “what to execute” to a dedicated execution sub-agent; all raw output settles in the sub-agent’s separate context, where it performs parsing, filtering, and summarization, and returns only the compressed conclusion to the main agent.
A schematic call contract can be written as:
result = execution_subagent.run(goal="Run unit tests and locate the cause of failures",command="make test",return_schema={"status": "pass | fail | error","failures": [{"case": str, "reason": str}],"evidence": "Up to 3 key log lines"})
The benefits of this design come from three points: context isolation, information compression, and single responsibility. The main agent’s window carries only the minimum sufficient information needed for decisions, while the sub-agent takes on dirty work like “reading logs.”
3. Why a Small Model Is Enough for the Execution Sub-Agent
The task distribution of an execution sub-agent is highly convergent: parsing output structure, recognizing error patterns, and summarizing according to a template. It does not need global architecture reasoning or cross-file refactoring capability. Therefore, a specialized small model with billions of parameters, trained for the purpose, can often cover the vast majority of scenarios.
More importantly from an engineering standpoint is the cost structure: the sub-agent is called frequently with long single inputs, so using a large-parameter model amplifies both cost and latency; small models have advantages in per-token cost, concurrent throughput, and deployment flexibility, and are easier to deploy privately on your own container platform.
It is worth emphasizing that specialization does not mean lowering standards. A reasonable approach is to keep a “fallback path”: when the sub-agent’s confidence is low or the returned structure is invalid, allow the main agent to request one more detailed summary, rather than unconditionally feeding raw logs back.
4. Training and Data Construction Essentials
To make a small model reliably produce structured conclusions, data construction matters more than model selection:
- Collect real execution traces, covering common categories such as build failures, dependency conflicts, test assertion failures, timeouts, and permission and network errors.
- Annotate the “minimum sufficient summary,” meaning the shortest expression that does not lose localization information, while retaining a few verifiable evidence lines.
- Introduce negative samples to train the model to explicitly output “cannot determine” when information is insufficient, suppressing fabrication.
- Apply structured output constraints with schema validation; invalid output is retried directly rather than entering the main context.
5. Evaluation: Success Rate Is Not Enough
Evaluating this architecture requires observing three dimensions at the same time. The first is task success rate, verifying that context compression has not harmed problem-solving ability; the second is token and turn cost, measuring the main agent’s total input and output volume and average number of interaction turns; the third is summary quality, including key information recall, evidence verifiability, and format validity. Practice discussed publicly shows that this kind of solution can reduce the main agent’s token usage by about 30% while keeping the success rate roughly on par, and on some metrics it approaches or even surpasses the approach of “using a large general-purpose model as the sub-agent.”
6. Adoption Recommendations
To practice this in your own system, start with the smallest change: first add an adapter layer for terminal calls to unify command execution and output truncation; then define a return schema to enforce structure; then connect monitoring, alerting, and logging services to continuously regress summary quality. Baidu AI Cloud can provide the hosting and observability foundation for this kind of architecture through general capabilities such as container platforms, log services, and monitoring and alerting.
It is foreseeable that coding agents are moving from “a single large model doing everything” toward a layered form where “the main agent handles planning and specialized small-model sub-agents handle execution.” Context governance is becoming one of the core competencies of agent engineering.
