Sub topic 3
What a subagent is
This chapter assumes you have used an AI coding agent in a terminal or editor, know what a context window and a tool call are, and can read basic Python. Later chapters cover briefs, parallelism, and verification. Here we define the term and describe the working model.
The idea in one paragraph
A subagent is a separate agent instance that another agent, called the parent or orchestrator, starts to handle one scoped piece of work. The subagent runs its own loop of model calls and tool calls. It has its own context window, and it returns a result to the parent when it finishes. The parent continues from that result without needing to see every step the subagent took.
How it differs from the parent
The parent and the subagent run the same kind of loop. They differ in what each one holds.
| Property | Parent agent | Subagent |
|---|---|---|
| Context at start | The full conversation so far | Only the brief it was given |
| Context at end | Parent history plus the subagent's summary | Discarded, except for what it returns |
| Tools | Whatever the session allows | A set chosen by the parent, often smaller |
| Lifetime | The whole session | One task |
The key point is the first row. A subagent does not inherit the parent's history. If the brief leaves out a fact, the subagent does not know it.
Why this helps
Every file the subagent reads and every command output it sees stays in its context. The parent receives only the summary. This keeps the parent's context small, so it has room for later steps and is less likely to lose track of earlier decisions. The next chapter covers this effect in detail.
A subagent also lets the parent give a task different limits. You can allow a research subagent to read files but not edit them, or allow a test-writing subagent to edit only one directory.
A minimal orchestrator
The following Python code shows the structure. The parent starts two subagents at the same time and records a failed status if one raises an error, so one failure does not abort the run.
import asyncio
from dataclasses import dataclass
@dataclass
class Task:
name: str
brief: str
async def run_subagent(task: Task) -> dict:
# Placeholder: replace with a call to your agent runtime.
# The subagent receives only the brief, not the parent's history.
await asyncio.sleep(0.1)
return {"name": task.name, "status": "ok", "summary": f"finished {task.name}"}
async def orchestrate(tasks: list[Task]) -> list[dict]:
outcomes = await asyncio.gather(
*(run_subagent(t) for t in tasks), return_exceptions=True
)
results = []
for task, outcome in zip(tasks, outcomes):
if isinstance(outcome, Exception):
results.append({"name": task.name, "status": "failed", "summary": str(outcome)})
else:
results.append(outcome)
return results
if __name__ == "__main__":
tasks = [
Task("update-docs", "Update README usage section for the new --verbose flag."),
Task("add-tests", "Add unit tests for parse_args in tests/test_cli.py."),
]
for result in asyncio.run(orchestrate(tasks)):
print(result)run_subagent takes a Task that holds only a name and a brief. That is the whole input the subagent gets. return_exceptions=True makes asyncio.gather return exceptions as values instead of raising the first one, which lets the loop mark that task as failed and keep the other results.
Common mistakes
- Assuming the subagent knows what you discussed earlier. It does not, so write down every fact it needs.
- Treating the summary as proof. The summary describes what the subagent says it did. Check the actual changes before you accept them.
- Starting a subagent for a task of two or three tool calls. The cost of writing a brief and waiting for a new context often exceeds the benefit.
Exercise
A parent agent spent an hour deciding that all date handling must use UTC. It then starts a subagent to fix a bug in a date-parsing function, and the brief says only "Fix the bug in parse_date." The subagent returns code that uses local time. Why did this happen, and what is the fix?
Show answer
The subagent does not see the parent's history, so it never learned the UTC decision. The fix is to put that constraint in the brief, for example "Use UTC for all date handling," along with the goal and the files in scope.