Overview
A subagent is a separate agent instance that a main agent delegates a scoped task to. It runs with its own context window, its own system prompt, and its own set of tools, and returns only its final message to the caller. The boundary that defines it is the context boundary. Work that produces bulky intermediate output — searching a codebase, reading logs, fetching API responses — happens inside the subagent, and the main conversation receives the conclusion instead of the raw material. The mechanism is not specific to one product, so this page treats the subagent as a general pattern and gives each tool’s concrete definition format and paths where they matter. It covers what subagents are for, how a definition is structured, how one is invoked and composed, and how to design one.What subagents are for
Delegating to a subagent buys four things. A given subagent usually exists for one of them, and naming which one clarifies how it should be defined.Context isolation
Exploration, log analysis, and API responses stay inside the subagent’s own
context window. Only the summary crosses back, so the main conversation
does not fill with material that no decision depends on.
Constrained capability
A subagent runs with the tools its definition grants and nothing else. A
judgment-only role can be given no write tools at all, which makes the
restriction structural rather than a matter of following instructions.
Reusable roles
A role definition is a file. Checked into a repository it is shared by the
team; placed in the user directory it follows one person across projects.
Cost control
Each subagent selects its own model, so mechanical classification runs on a
cheaper tier without lowering the model the main conversation uses.
Anatomy of a subagent
Definition file
A subagent is defined as a file holding two things: metadata that identifies the role and scopes its capability, and the instructions that become the subagent’s system prompt. The file format differs by tool. Claude Code and GitHub Copilot CLI use Markdown with YAML frontmatter, where the frontmatter is the metadata and the body is the system prompt. Codex CLI uses TOML, where the instructions are one field among the rest.Requirements differ by tool: Claude Code requires
name and description,
GitHub Copilot CLI requires only description, and Codex CLI requires
name, description, and developer_instructions. Each tool also adds
optional fields of its own — disallowed tools, permission or sandbox mode,
preloaded skills, lifecycle hooks, MCP servers. Confirm the field names and
requirements in the documentation of the tool you use.Where definitions live
Definition files are picked up from several locations, and the location decides who the subagent is available to. The scopes are the same across tools; the paths differ.
Check which side wins when the same name exists in both, because tools
disagree. Claude Code takes the project definition over the user one; GitHub
Copilot CLI takes the user’s home directory over the repository. Confirm the
order for whichever tool you use rather than assuming it.
Two further sources sit outside that pair. A plugin can ship agent definitions,
which become available wherever the plugin is enabled — distributing subagents
together with the skills and commands that call them is covered in
Plugins. And in Claude Code an organization can apply definitions
through managed settings, which take precedence over every other location.
Invoking a subagent
A subagent is reached in three ways, which differ in how reproducible the delegation is.- Automatic delegation. The main agent matches the request against each
descriptionand delegates when one fits. This is why thedescriptionstates both what the subagent does and when to use it. - Explicit request. Naming the subagent in the instruction steers the choice; a dedicated mention syntax, where the tool provides one, pins it outright and skips the matching.
- From a skill or command. A skill body specifies which subagent runs at which step, with what prompt. This is the reproducible path: the delegation is written down rather than re-decided each time.
Chaining subagents in sequence
Chaining passes one subagent’s result to the next — find the problems, then fix them. The handoff runs through the main agent, which receives the first result and puts it into the second subagent’s prompt. Each link receives only what the previous one concluded, not the material it read to reach that conclusion. The main agent holds those summaries and nothing else, so the chain can grow long without its context growing at each link. The cost of that is real: a later link cannot go back to evidence an earlier one saw and did not report. Where the second step needs the detail, either pass it explicitly in the prompt or give the second subagent the means to read it again.Fanning out in parallel
Fanning out runs independent subtasks at the same time. There is one condition: no subtask needs another’s result. Two shapes cover most fan-outs, and they differ in what is held fixed. Either the target is fixed and the purpose varies, or the purpose is fixed and the target varies.
Different purposes on the same target. For one diff, run an agent looking
for security problems, another for gaps in test coverage, another for naming.
All three read the same diff; what differs is what each is looking for.
The target is fixed, the purpose moves
The purpose is fixed, the target moves
Designing a subagent
Five decisions determine whether a subagent behaves the same way twice. Each is made in the definition, not in the prompt that happens to invoke it.Give one subagent one responsibility
A definition covering several responsibilities has to describe all of them in itsdescription, which blurs the delegation decision, and needs the union of
their tools, which widens what it can do. Split by responsibility, and both the
description and the tool list stay sharp.
That responsibility reaches the caller through the description, which is not
a summary for humans but the text the main agent matches against. State what
the subagent does, when to use it, and what it returns — in the words a request
would actually use.
Constrain capability, not just instructions
Diffs, pull request bodies, dependency changelogs, and third-party documentation are data, not instructions. State in the definition that text embedded in the material is never followed — then make that hold even when it does not, by granting only the tools the workflow uses. Why that instruction alone is not enough is covered in Security.- A judgment or review role needs no write tools. Withholding them turns “do not edit files” from an instruction into a property of the environment, and an agent holding no write tools cannot act on an injected instruction even if it accepts one.
- Granting
Bashgrants the whole shell. Which commands may actually run is narrowed outside the definition — by the session’s permission rules, which apply inside subagents too, or by a pre-tool hook that validates each command — not by the tool list. - Keep intermediate files out of the user’s working tree. Writing scratch data under a temporary directory keeps a read-only role read-only in practice.
* behaves differently depending on the space before it:
Define both ends of the interface
A subagent sees its prompt and returns its final message. Nothing else crosses the boundary in either direction. Both ends have to be specified, and for the same reason: what is not stated does not arrive.Make the judgment reproducible
Choose the model per role
A subagent that omitsmodel inherits the caller’s model. Pin it where the
role justifies doing otherwise.
Operational limits
Subagents have ceilings and costs that decide how far the pattern scales. Two are limits the implementation sets. Three are properties of delegation itself, which no configuration removes.The concurrency cap is rarely what binds first. Consolidation cost grows with
every result the caller has to merge, so a fan-out usually stops being worth
widening well before it stops running in parallel.
Subagents are the delegation primitive; the model that decides what to
delegate is covered in Agentic Workflow, and the unit
that writes the delegation down as a reproducible procedure is covered in
Skills.
Related pages
Agentic Workflow
The delegation model subagents implement — managing context, dividing roles
along context boundaries, and running work in parallel.
Skills
Package a workflow as a reusable unit that orchestrates subagents at
specified steps.
Context Engineering
Why a task is worth moving into its own window, and how the context that
stays behind is designed.
Security
Why constraining capability — rather than instructing behavior — is the
load-bearing control, starting from where the risk arises.