Skip to main content

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

Three agent terminals side by side, each in its own worktree and branch: the API, the panel component and the e2e test, each ending ready for review

Split work so it can merge cleanly

Parallel work only pays off when the parts are independent:

  1. Agree the contract first: types, function signatures, API shape. Commit that to main before you fan out.
  2. Give each agent a part that touches different files: API, component, tests.
  3. Merge one at a time, run the full check after each merge.
  4. 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

  1. Create the test-reviewer subagent and ask the main agent to use it after a test change.
  2. Split a feature into a contract plus two independent parts.
  3. Run two agents in two worktrees, merge both, and run the full check.
  4. Note how long review and merge took compared to the work itself.

Walkthrough in TestRun Board

  1. Subagent: .claude/agents/test-reviewer.md is already in the repository. Change a test (for example loosen an assertion in tests/lib/runs.test.ts to toBeDefined()) and ask “Let the test-reviewer review my test changes.” It should report the file, the line and why it matters.
  2. 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
    
  3. In ../trb-history, let one agent write lastFailure() and formatRunDate() test-first. In ../trb-panel, let another build HistoryPanel against the agreed types only.
  4. Merge my/history-logic first, run npm run check, then merge my/history-panel and run it again.
  5. Compare with solution/lesson-22-flaky-history and clean up with git 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.