Subagents and parallel worktrees
Subagents give a task its own context. Worktrees give each agent its own copy of the repo. Together they let you run work in parallel without collisions.
Words on this page (7)
- subagent
- : A helper agent started by another agent for a focused job, with its own context.
- worktree
- : A second checked-out copy of a git repository on its own branch in its own folder.
- agent
- : A model run in a loop that chooses its own tools and steps until the task is done.
- repo
- : Repository: a project folder tracked by git, usually hosted on GitHub.
- context window
- : The maximum number of tokens a model can look at in one call: your prompt, the conversation, tool results and the answer together.
- git
- : The standard tool for tracking changes to code.
- tool
- : A function the model can ask your program to run, described by a name, description and argument schema.
What it is
A is a worker the main starts for a focused job, such as searching a codebase or reviewing a diff. It has its own and returns only a summary, so the main conversation stays clean. A is a second checkout of the same on its own branch in its own folder.
Why it matters
Two agents editing the same checkout overwrite each other and test against each other's half-finished changes. Give each its own worktree and branch, then merge. Use read-only subagents freely for research since they cannot conflict. Use them to isolate noisy work: a subagent that reads fifty files and returns three lines saves your main context. Do not spawn agents for small steps, since each one carries startup cost.
A worked example: three jobs in parallel
Job 1 (worktree A): add a search feature. Job 2 (worktree B): fix a login bug. Job 3 (read-only subagent): survey how errors are logged across the codebase. Jobs 1 and 2 cannot overwrite each other, job 3 returns ten lines. You review each result and merge one at a time.
When not to
For a five-minute change, one session is simpler. Each agent adds startup cost and one more thing to review.
How the pieces fit
When the main agent starts a subagent, it hands over a written task. The subagent works in its own fresh context, reads whatever it needs, and sends back a final message. Everything it read along the way is discarded from the main chat. If it read fifty files and replied with five lines, the main agent only carries the five lines.
A worktree is made with git, the version control . Both folders share one history but have separate files and branches. Changes in one do not appear in the other until you merge.
A second example: where it does not help
You ask three agents to refactor the same module, each in its own worktree. All three change the same functions. When you merge, you hit conflicts, and you must decide among three versions of the same code. Worktrees prevent overwriting, not overlapping work. Divide tasks so they touch different files. If two jobs need the same file, run them one after the other. Similarly, a subagent that must understand the whole conversation gains nothing, because its summary will lose context.
Trade-offs
Parallel work saves your waiting time but adds review time, since you read several diffs. It also multiplies the cost, because each agent has its own usage. The summary a subagent returns can leave things out. If a decision depends on a detail, ask for it explicitly, or check the source yourself.
What goes wrong
Merge conflicts, as above. A subagent that reports success without evidence. Forgotten worktrees that pile up on disk and old branches that confuse you later. To check a subagent's claim, ask which files it read and which command proved the result, then run that command. Clean up with the list command for worktrees and remove finished ones.
Try it yourself
In a repo you own, make a second worktree on a new branch, edit a file there, and look at the original folder.
- Confirm the original file is unchanged.
- Merge the branch.
- Remove the extra worktree.
Expect the change to appear in your main folder only after the merge.
Common mistake
Running two agents in the same folder. Their half-finished edits collide and tests fail for confusing reasons.
You ask two assistants to renovate two rooms. If they share one workbench they trip over each other's tools. Give each a separate workbench and room, and they finish at the same time. Git worktrees are separate workbenches for code, and subagents are assistants with their own notebooks.
Check yourself
Why run two agents in separate worktrees?
Not quite. A shared checkout means shared half-finished edits. Separate worktrees isolate them.
What does a subagent return to the main agent?
Not quite. The point is to keep the main context clean by returning only the result.
You need a five-minute fix in one file. What should you do?
Not quite. For a small change, parallel agents cost more than they save.
You got 0 of 3 right.
Go deeper
- Docs
Anthropic · Defining custom subagents with their own tools and prompts.
- Article
Anthropic · Orchestrator and subagent design, with real cost and failure data.
- Docsgit worktreeDocs
Git · The reference for creating and removing linked working trees.
Spotted a mistake or something unclear? Report it on GitHub.