AI Coding Subagents: A Practical Guide to Scoped Execution Contexts
This article explains how subagents solve the problem of overly broad AI coding assistant behavior by packaging a prompt, a tool whitelist, and model parameters into a single dispatchable unit. It covers the core differences from general chat sessions, a step-by-step approach to defining responsibility boundaries, using structured output, tiering models for cost control, composing agents into pipelines, and adding observability and governance, along with common pitfalls to avoid.
1. The Starting Problem: Why “Can Do Anything” Is Actually Hard to Use
Many teams run into the same experience the first time they connect an AI coding assistant: you only want it to scan the codebase for security risks, but it also hands you a refactoring plan, performance suggestions, and a full set of test cases. The output looks rich, but it buries the information you actually needed.
This is not a model capability problem. It is a task boundary problem. A general-purpose conversational agent faces an open-ended intent and will proactively fill in whatever context it thinks is valuable. Work like code review requires the opposite: convergence. Find issues only, provide location and evidence only, and do not touch the code.
Abstracted out, this yields three clear engineering requirements:
- Single responsibility: the agent’s behavior scope is declarative and predictable;
- Least privilege: the agent can only access the tools and data required for its responsibility;
- Shareable configuration: capability definitions live in version control and become team assets rather than individual tricks.
The subagent mechanism is designed around exactly these three points.
2. What a Subagent Really Is: A Dispatchable Unit of Prompt, Tools, and Model
A subagent is not “another smarter model.” It is a constrained execution context made of three parts: a role declaration (the prompt), a capability list (the set of callable tools), and runtime parameters (for example, which model tier to use).
In most implementations it is stored as a file in a project’s agent configuration directory, one file per agent. A typical structure looks like this:
---name: code-reviewerdescription: Focused on code quality and security risk review, read-onlytools: [Read, Grep, Glob]model: small---You are a code review expert. Output only a list of issues, including: location, risk type, evidence, and fix direction.Do not provide directly applicable patches. Do not discuss refactoring or style preferences unrelated to defects.
The top half is structured metadata used by the dispatcher for intent matching and permission assembly. The bottom half is a natural-language behavior contract. This “configuration as code” form makes agent definitions reviewable, diffable, and revertible.
3. Three Key Differences from a Normal Conversation
Focused. A general chat session tends to “do a little extra while it’s at it,” while a subagent’s behavior is explicitly narrowed by its prompt. A review agent outputs only defect items. It does not cross the line and modify files, and it does not steer the topic toward architecture evolution. The signal-to-noise ratio of the output improves, and the review record stays clean.
Restricted. The tool list is a whitelist; any capability not listed is unavailable. A review agent mounted with only read and search tools physically cannot modify repository content. This turns “do not change the code” from a verbal agreement into a mechanism-level constraint. The security boundary is guaranteed by the permission model, not by the model’s self-discipline.
Reusable. Definitions live in the project, so team members share the same standard. New hires do not need to be told verbally “how to ask”; they invoke the review agent directly and get reports in a consistent format. When migrating across projects, copying the configuration carries the whole set of engineering standards with it.
4. How to Put It into Practice: From a Single Agent to Agent Orchestration
4.1 Draw responsibility boundaries before writing prompts
A practical split is three categories: read-only analysis, change execution, and verification feedback. Analysis agents get the narrowest permissions. Execution agents get the broadest permissions and must be paired with branch and review workflows. Verification agents run tests and static checks and report conclusions. Once boundaries are clear, the prompt only needs to describe inputs, output format, and prohibitions; there is no need to repeatedly stress “stay focused.”
4.2 Replace free text with structured output
Have agents return fixed fields so downstream automation can consume them:
{"findings": [{"file": "src/db/user.go", "line": 42,"type": "injection risk", "severity": "high","evidence": "query built by string concatenation", "suggestion": "switch to parameterized queries"}]}
With fixed fields, reports can be stored in a database, aggregated for trend statistics, and used as input for pipeline gates.
4.3 Tier models to control cost
Not every subtask needs the strongest model. Retrieval and location, formatting, and list categorization can go to a lightweight model; cross-module reasoning and concurrency defect analysis can be escalated to a higher-capability model. The tiering strategy should be written into the configuration rather than decided ad hoc each time, so that spending stays stable and predictable.
4.4 Compose them into a pipeline
One workable sequence: a retrieval agent locates relevant files → a review agent produces the defect list → a fix agent makes changes per the list → a verification agent runs the build and tests → a summary agent generates the review digest. Each step’s output is a strongly constrained input for the next, which prevents agents from drifting off target in a long chain.
4.5 Observability and governance
For every invocation, retain the agent name, model tier, tool call records, and elapsed time, and hook into general logging, monitoring, and alerting. Set a human confirmation step for high-risk agents. Configuration changes go through code review to prevent permissions from being quietly expanded.
5. Common Pitfalls
- Splitting agents too finely: an exploding agent count brings scheduling and context-passing costs; three to five is usually enough.
- Writing prompts as disclaimers: repeatedly writing “do not do anything else” is less effective than simply tightening the tool whitelist.
- Ignoring the output contract: without a fixed format, results cannot enter automated workflows.
- Granting full permissions up front: grant on demand and revoke periodically.
6. Conclusion
The value of subagents is not making AI do more. It is making it do fewer, more precise things within clear boundaries. When responsibilities, permissions, and model tiers all become versionable configuration, AI-assisted development turns from “chat skills” into “engineering capability.” For enterprise teams that want to improve delivery efficiency and raise the baseline of code security, consolidating this configuration into team standards on a platform such as Baidu AI Cloud is a high-return step.
