How to run multiple coding agents in parallel
Give each agent its own git worktree, port range and test database, merge through one integrator, and keep their test runs off the same machine at once.
Riley Hilliard8 min read
agentsclaude-codetesting
To run multiple coding agents in parallel, give each one its own git worktree on its own branch, then give each worktree its own ports, database and cache paths. Worktrees keep the agents out of each other’s files. They do nothing about the services and CPU the agents share, and that is where most parallel setups break: two agents start their test suites at the same moment, both try to bind port 5432 or truncate the same test database, and both report failures that have nothing to do with their code.
How do you run multiple coding agents in parallel?
Start with one worktree per agent. A worktree is a second (or fifth) checkout of the same repository in its own directory, sharing one .git object store. Each worktree has its own branch, its own working files and its own index, so an agent editing src/auth.ts in one doesn’t touch the copy another agent is editing in a different one.
git worktree add -b agent/auth ../myapp-auth
git worktree add -b agent/search ../myapp-search
Each command creates a new branch from your current HEAD and checks it out in the new directory. Point one agent at ../myapp-auth and another at ../myapp-search, and they can both edit, commit and run commands without seeing each other’s half-finished changes.
Claude Code can do this step for you. claude --worktree auth (or -w) creates a new worktree for the session, under .claude/worktrees/ inside the main checkout. That puts the worktree inside your repository’s directory, so a tool that searches upward for its config file can walk past the worktree and find the main checkout’s. The rr section below covers one such case.
These list and clean up worktrees:
git worktree list # every worktree, its path, commit and branch
git worktree remove ../myapp-auth
git worktree prune # forget worktrees whose directories were deleted by hand
Git refuses to check out the same branch in two worktrees, so two agents can’t end up committing to one branch. It also refuses to remove a worktree with uncommitted changes unless you pass --force, so an agent’s unsaved work isn’t thrown away by accident. The git worktree documentation lists the rest of the options.
Do git worktrees isolate everything?
Worktrees isolate files and branches: each agent gets its own working files, index, checked-out branch and untracked build output. Everything else is shared: network ports for dev servers, test servers and databases; local databases, Redis and anything running in Docker; caches at fixed paths such as /tmp/... or ~/.cache/...; CPU, memory and disk I/O; and the .git directory itself, with its refs, stash, hooks and config. An agent doesn’t know the other agents exist, so it won’t think to avoid them.
A new worktree also starts without anything git doesn’t track. There is no node_modules, no .venv and no .env, so each agent’s first step is an install, and its first test run may fail because the .env it expects only exists in the main checkout. Copy the example env file in as part of creating the worktree.
Why do parallel agents break each other’s tests?
Agents that work test-first run the suite after every change and again before every commit, so with three agents you get three suites starting whenever each one feels like checking its work.
Fixed resources collide outright. If the test setup starts a server on port 3000, the second agent’s run fails with “address already in use”. If both suites point at a database called myapp_test and each one truncates tables between tests, one agent’s setup deletes rows the other agent’s test just inserted. The test does fail, but nothing in either agent’s code caused it.
Caches collide more quietly. A tool that writes to a fixed path such as /tmp/myapp-fixtures lets one run read files that another run is halfway through writing.
CPU contention makes timing-sensitive tests flaky. A suite configured to use every core (pytest -n auto, a Jest run with default workers) assumes it has the machine to itself. Two of them each spawn a worker per core, the machine runs at twice its capacity, and tests with timeouts start failing at random. The laptop you are typing on becomes slow at the same time.
An agent can’t tell any of these apart from a real regression. It sees a red test and starts fixing code that wasn’t broken.
How do you give each agent its own ports and database?
Make every shared resource configurable through an environment variable, then set different values in each worktree. Most projects already read PORT and DATABASE_URL, so the work is finding whatever is still hardcoded.
This site’s own repository does it for the dev server. The Makefile sets PORT_WEB ?= 4321, so the default only applies when the variable isn’t already set, and a second worktree can run its own server alongside the first:
PORT_WEB=4322 make dev
For a project with a database and containers, a small script run once per worktree covers most of it:
# run inside each worktree; NAME is the worktree's name, N its slot number
NAME=auth
N=1
cat > .env <<EOF
PORT=$((3000 + N))
DATABASE_URL=postgres://localhost/myapp_test_${NAME}
COMPOSE_PROJECT_NAME=myapp-${NAME}
EOF
createdb "myapp_test_${NAME}"
COMPOSE_PROJECT_NAME makes Docker Compose prefix container, network and volume names with the worktree’s name, so two worktrees running docker compose up get separate containers. A per-worktree database name means each suite can truncate its own tables freely. Any cache directory or fixture path the tests write to should come from an environment variable too, or from a temporary directory created per run.
None of this helps with CPU: three suites on one laptop still compete for the same cores.
How do you stop agents from fighting over test machines?
Move the test runs off the laptop and make each run take a lock on the machine it uses. We built rr to do both. It syncs your working tree to a remote machine over rsync and SSH, runs the command there and streams the output back. Before syncing, it takes a lock on the host, and if that host is locked it tries the next one. When every host is busy it cycles through them until one frees up, so a second agent’s run goes to another machine or waits its turn instead of running on top of the first. The manual version, rsync plus ssh, is in how to run tests on a remote machine over SSH.
$ brew install rileyhilliard/tap/rrYour hosts go in ~/.rr/config.yaml; the project’s tasks and sync rules go in .rr.yaml, which you commit. A minimal pair looks like this:
# ~/.rr/config.yaml
version: 1
hosts:
mini:
ssh: [mac-mini.local, mac-mini-tailscale]
dir: ${HOME}/projects/${PROJECT}
# .rr.yaml
version: 1
local_fallback: never
tasks:
test:
run: pytest -n auto
Each agent then runs rr test from its own worktree. rr knows about worktrees: in a linked worktree, ${PROJECT} expands to repo@worktree-name, so each agent’s tree syncs to its own directory on the remote and two agents never overwrite each other’s files there either. When you delete a worktree locally, the next sync removes its remote directory, and rr prune cleans hosts that haven’t been synced to since.
A few settings decide how waiting behaves. lock.timeout (default 5m) is how long rr waits for a single host, and lock.wait_timeout (default 1m) is how long it cycles through several busy hosts before failing. A run that gives up fails with the error code LOCK_HELD and reports who holds each lock, so an agent can branch on the code instead of parsing a message. The lock holder refreshes it every 30 seconds, and a lock whose holder died goes stale after 90 seconds, so a crashed run doesn’t block the others for long. rr unlock clears one by hand.
local_fallback: never is already the default, and writing it into .rr.yaml keeps it that way for every agent and teammate, whatever their global config says. With fallback on, rr can run the suite on your own machine when no host is free, and an agent may count that run as a pass without noticing where it happened. rr reports a fallback loudly, but with fallback off there is no local run for an agent to mistake for a pass.
You don’t need remote machines to get the queueing. rr host add --local --name dev adds your own machine as a host that runs commands in place, with no SSH or sync, and takes the same lock as a remote. Concurrent agents on one laptop then queue for it instead of starting their suites at the same moment.
If your agents use Claude Code’s --worktree flag, commit .rr.yaml. rr only looks for its config up to the top of the checkout you’re in, and a worktree under .claude/worktrees/ without its own copy fails with CONFIG_NOT_FOUND rather than silently using the main checkout’s config, which would run the main checkout’s code.
Who merges the work back together?
Each agent commits on its own branch, and one integrator, a single agent or a person, merges the branches into main one at a time:
git switch main
git merge agent/auth
make test
Then the next branch rebases onto the new main and goes through the same steps. Merging one branch at a time means the full suite runs against each combined state, so when two agents’ changes conflict in behavior rather than in text, you find out which merge did it.
Splitting the work along file boundaries before the agents start keeps the integrator’s job small. Two agents both editing the same module will produce conflicts no matter how well their environments are isolated. Agents working on separate modules mostly produce branches that merge cleanly.