Sub topic 7
Starting agents in parallel
This chapter assumes you can create worktrees with git worktree add, that each task already has its own branch, and that you have run a command-line coding agent on a single task. Here you start several agents at once, one per worktree, and wait for them to finish.
The goal
Each agent should run in its own worktree, with its own branch and its own copy of local config. The agents then cannot overwrite each other's files. Git does not start the agents for you. You launch them with ordinary shell job control.
Creating the worktrees first
Create all worktrees before you start any agent. Each one starts from main on a new agent/ branch.
git worktree add -b agent/auth ../myrepo-auth main
git worktree add -b agent/search ../myrepo-search main
git worktree listgit worktree list should show the main checkout plus one line per new worktree, each with its path and branch.
Launching one agent per worktree
Run each agent inside a subshell, so that cd affects only that agent. Add & to send the subshell to the background. Then call wait so the script blocks until every agent exits.
for task in auth billing search; do
(
cd "../myrepo-$task"
cp ../myrepo/.env .env
your-agent-command "Work on the $task task described in TASKS.md"
) &
done
waityour-agent-command is a placeholder for whichever agent CLI you use. The cp line copies the untracked .env file, because a new worktree contains only tracked files.
Common errors
- A missing worktree. If the
cdfails, the agent runs in your current directory, which may be the main checkout. Addset -eat the top of the script. Inside the subshell,cdthen aborts the subshell before the agent starts. - Unquoted variables. Write
"../myrepo-$task"with quotes, so paths with spaces still work. - Interactive agents. Many agent CLIs ask for input or approval. Several of them in one terminal will fight over the keyboard. Use the agent's non-interactive mode if it has one. Otherwise, run each agent in its own terminal tab or tmux pane.
- Interleaved output. Background jobs all write to the same terminal. Redirect each agent's output to a log file in a directory outside every worktree, such as
../logs.
mkdir -p ../logs
for task in auth billing search; do
(
cd "../myrepo-$task"
your-agent-command "Work on the $task task described in TASKS.md" \
> "../logs/$task.log" 2>&1
) &
done
waitRun tail -f ../logs/auth.log in another terminal to watch one agent.
Checking the results
wait returns when the jobs finish, but it does not tell you whether they succeeded. To get each agent's exit status, record the process IDs and wait on each one.
pids=()
for task in auth billing search; do
(cd "../myrepo-$task" && your-agent-command "Work on the $task task described in TASKS.md") &
pids+=($!)
done
for pid in "${pids[@]}"; do
wait "$pid" || echo "agent with pid $pid failed"
doneAn agent that exits with a non-zero status prints a failure message. Look at its log before you review its branch.
Limits to keep in mind
Parallel agents share your CPU, memory, and API rate limits. Three to five agents is a common starting point. More agents may be throttled or may slow your machine.
Exercise
A launcher script runs (cd ../myrepo-auth && your-agent-command "...") & for each task. Why is the cd placed inside parentheses, and what does wait do at the end?
Show answer
The parentheses create a subshell, so the cd changes the directory only for that background job. The launcher stays in its original directory and the next loop iteration is unaffected. wait pauses the launcher until all background jobs have exited.
Exercise
Your three agents start, but one of them edits files in the main checkout instead of its own worktree. Name one likely cause and one fix.
Show answer
The cd into the worktree probably failed, for example because the worktree was never created or the path was wrong, so the agent ran in the current directory. Fix it by creating the worktrees first, checking the path, and using cd ... && your-agent-command ... or set -e so the agent does not start if cd fails.