Subagents and Parallel Agents in Git Worktrees
Why This Matters
One agent working through a long task fills its context window and slows down. Splitting work helps in two ways: subagents handle focused side tasks with their own context, and parallel agents work on independent parts at the same time. Done badly, parallel agents overwrite each other’s files. Git worktrees prevent that.
Subagents: focused helpers inside one session
A subagent is a separate agent with its own context window, its own instructions and optionally its own tools and model. The main agent delegates a task and gets back a summary, not all the noise. In Claude Code you define one as a Markdown file in .claude/agents/:
---
name: test-reviewer
description: Reviews test changes for weakened assertions, skipped tests
and missing edge cases. Use after any change under tests/.
tools: Read, Grep, Glob
---
You review test code. Report, do not edit.
Flag: .skip/.only, assertions replaced by toBeDefined/toBeTruthy,
expected values that changed without a code change, missing empty/error cases.
Answer with a list of findings per file, or "No findings".
The example repository ships a slightly extended version in .claude/agents/test-reviewer.md, which may also run read-only git commands to see the diff.
Because it has only read tools, it cannot “fix” anything; it can only report. Good subagents are narrow: a reviewer, a researcher that searches the codebase, a test-runner that summarises failures.
Parallel agents with git worktrees
A worktree is a second (third, fourth) checkout of the same repository in another folder, on its own branch. Each agent gets its own folder, so they never edit the same files on disk.
git worktree add ../wt/history-api -b feat/history-api
git worktree add ../wt/history-panel -b feat/history-panel
git worktree add ../wt/history-e2e -b test/history-e2e
# one terminal per worktree
cd ../wt/history-api && claude
Split work so it can merge cleanly
Parallel work only pays off when the parts are independent:
- Agree the contract first: types, function signatures, API shape. Commit that to main before you fan out.
- Give each agent a part that touches different files: API, component, tests.
- Merge one at a time, run the full check after each merge.
- Remove worktrees when done:
git worktree remove ../wt/history-api.
When it is overkill
Do not parallelise a change that takes one agent 15 minutes, or parts that all edit the same file. Coordination and merge time then cost more than you save. Two or three parallel agents is plenty for most teams; review capacity, not agent capacity, is usually the bottleneck.
Hands-on
- Create the
test-reviewersubagent and ask the main agent to use it after a test change. - Split a feature into a contract plus two independent parts.
- Run two agents in two worktrees, merge both, and run the full check.
- Note how long review and merge took compared to the work itself.
Walkthrough in TestRun Board
- Subagent:
.claude/agents/test-reviewer.mdis already in the repository. Change a test (for example loosen an assertion intests/lib/runs.test.tstotoBeDefined()) and ask “Let the test-reviewer review my test changes.” It should report the file, the line and why it matters. - Parallel agents on the flaky history feature, starting from the lesson 16, Lab: Your First Quality Feature solution:
git switch -c my/history-contract solution/lesson-16-pass-rate-column # agree the contract first: types for the panel in src/lib/types.ts, commit git worktree add ../trb-panel -b my/history-panel my/history-contract git worktree add ../trb-history -b my/history-logic my/history-contract - In
../trb-history, let one agent writelastFailure()andformatRunDate()test-first. In../trb-panel, let another buildHistoryPanelagainst the agreed types only. - Merge
my/history-logicfirst, runnpm run check, then mergemy/history-paneland run it again. - Compare with
solution/lesson-22-flaky-historyand clean up withgit worktree remove ../trb-panel ../trb-history.
Key Takeaways
- Subagents keep side tasks out of the main context; make them narrow and, for reviewers, read-only.
- Worktrees give each parallel agent its own folder and branch.
- Commit the shared contract before fanning out; merge one part at a time.
- Review capacity limits how much parallelism is useful.
