Dunea Code

Guide

How to run multiple Claude Code agents in parallel

Facts checked

Give each agent its own git worktree (a separate checkout on its own branch), copy your gitignored .env files into it, install dependencies there, and start its dev server on its own port. Then run claude (or codex) inside each worktree.

The commands are below, ending in one script that does all of it. At the end is how Dunea Code does the same thing for every task without the script.

Why two agents in one folder goes wrong

Two agents in the same checkout edit the same files, run the same tests against each other’s half-finished changes, and leave you one tangled diff. Git worktrees fix this: one repository, several working folders, each on its own branch. Commits made in any of them land in the same repo, so there is nothing to sync.

A worktree only holds tracked files. Anything gitignored (.env, node_modules, build output) is missing, and every worktree will try to start its dev server on the same port. Steps 2 to 4 deal with that.

1. Create a worktree per agent

From your main checkout, create one branch and one folder per task. Sibling folders keep them out of your repo:

terminal
git worktree add -b feat/login ../myapp-login main
git worktree add -b fix/header ../myapp-header main
git worktree list

-b creates the branch, the path is where the folder goes, and main is the starting point. If you’d rather keep worktrees inside the repo, use a folder like .worktrees/ and add it to .gitignore.

2. Copy your .env files

For a single file:

terminal
cp .env ../myapp-login/.env

In a monorepo there are usually several (apps/web/.env.local, apps/api/.env). This copies every gitignored file whose name starts with .env, keeping its path, and skips ignored folders like node_modules:

terminal
git ls-files --others --ignored --exclude-standard --directory \
  | grep -E '(^|/)\.env[^/]*$' \
  | while read -r f; do
      mkdir -p "../myapp-login/$(dirname "$f")"
      cp "$f" "../myapp-login/$f"
    done

Copy, don’t symlink, if an agent might edit them: with a symlink, one worktree changing DATABASE_URL changes it for all of them.

3. Install dependencies and set up

Each worktree needs its own install, and anything else your repo needs before it boots:

terminal
cd ../myapp-login
pnpm install            # or: npm ci, yarn, bundle install, uv sync
pnpm db:migrate         # whatever your repo needs

If agents will run migrations, give each worktree its own database so one agent’s migration doesn’t break another’s tests. A common pattern is a database per branch name, for example postgres://localhost/myapp_login, written into that worktree’s .env.

4. Give each worktree its own port

Every worktree runs the same dev script, so they all try the same port and the second one dies with EADDRINUSE. Pick a port per worktree and pass it in:

terminal
# Next.js reads PORT
PORT=3010 pnpm dev

# Vite takes a flag; --strictPort fails loudly instead of drifting to another port
pnpm dev --port 3020 --strictPort

# Rails and Django
bin/rails server -p 3030
python manage.py runserver 3040

Your own servers need to read the variable, for example app.listen(process.env.PORT ?? 3000). Leaving gaps of 10 between worktrees leaves room for an API, a worker or Storybook on the next ports. The EADDRINUSE guide covers offsets, Windows and direnv in more detail.

5. Start the agents

Open a terminal per worktree and start an agent in each:

terminal
cd ../myapp-login  && claude
cd ../myapp-header && codex

Or keep them in one tmux session, one window per agent:

terminal
tmux new-session -d -s agents -n login  -c "$PWD/../myapp-login"  claude
tmux new-window      -t agents -n header -c "$PWD/../myapp-header" codex
tmux attach -t agents

Tell each agent which port its app runs on, so it can open the right URL when it tests its work.

6. Review, merge, and clean up

terminal
git -C ../myapp-login diff main...feat/login     # what the agent changed
git -C ../myapp-login push -u origin feat/login
gh pr create --head feat/login --fill

# after it merges
git worktree remove ../myapp-login
git branch -d feat/login
git worktree prune

git worktree remove refuses if the folder has uncommitted changes, which is a useful last check.

All of it in one script

Save this as new-agent.sh somewhere on your PATH. It creates the worktree, copies env files, installs dependencies, and picks a port block that nothing on your machine is listening on (macOS and Linux; it uses lsof).

new-agent.sh
#!/usr/bin/env bash
# usage: new-agent.sh <branch> [base]
set -euo pipefail
branch="$1"; base="${2:-main}"
root="$(git rev-parse --show-toplevel)"
dir="$(dirname "$root")/$(basename "$root")-${branch//\//-}"

git -C "$root" worktree add -b "$branch" "$dir" "$base"

# gitignored .env files, same paths
git -C "$root" ls-files --others --ignored --exclude-standard --directory \
  | grep -E '(^|/)\.env[^/]*$' \
  | while read -r f; do mkdir -p "$dir/$(dirname "$f")"; cp "$root/$f" "$dir/$f"; done

# first block of 10 free ports from 3000
port=3000
while lsof -nP -iTCP:"$port"-"$((port + 9))" -sTCP:LISTEN >/dev/null 2>&1; do
  port=$((port + 10))
done
echo "export PORT=$port" > "$dir/.envrc"

(cd "$dir" && pnpm install)
echo "Ready: cd $dir && source .envrc && claude    (app on port $port)"

It is deliberately simple. Two worktrees created before either starts its server can get the same block, and you still need to remember which port belongs to which branch. Add .envrc to .gitignore so it doesn’t get committed.

The Dunea Code way

Dunea Code is a free desktop app (macOS, Windows, Linux) that does the steps above every time you start a task, with Claude Code or Codex on your own plan:

  • Worktree. Every task gets its own branch and git worktree.
  • Env files. Gitignored files are copied in before anything runs: whatever .worktreeinclude lists, or .env* by default.
  • Ports. The worktree leases a block of 10 ports, each checked free on your machine, and keeps it. Scripts see $DUNEA_PORT and $DUNEA_PORT_0 to $DUNEA_PORT_9.
  • Setup and run. .dunea/setup.sh installs and migrates, .dunea/run.sh starts the app. Turn on auto-run in settings and setup runs as soon as a worktree is created. Ask an agent to set up the repo and the built-in dunea-setup skill writes both scripts.
  • Review. Split diff with accept or revert per change, then create the PR, watch checks and merge from the app. Dunea Remote on iPhone lets you approve from your phone.
  • Overclock. Describe a feature and it is planned into tasks and run across several agents.

The equivalent of steps 3 and 4 is two short files:

.dunea/setup.sh
#!/usr/bin/env bash
set -euo pipefail
pnpm install
db="myapp_${DUNEA_WORKSPACE_NAME//[^a-zA-Z0-9]/_}"
createdb "$db" 2>/dev/null || true
echo "DATABASE_URL=postgres://localhost/$db" >> "$DUNEA_ENV_FILE"
.dunea/run.sh
#!/usr/bin/env bash
set -euo pipefail
exec pnpm dev --port "$DUNEA_PORT"

Coming from another tool? Compare Dunea Code with Conductor, Superset and the Claude Code desktop app.

FAQ

Can I run two Claude Code sessions in the same folder?

You can, but both agents edit the same files and share one git index, so they overwrite each other and their changes end up mixed in one diff. Give each agent its own git worktree instead.

How many Claude Code agents can I run at once?

Git has no practical limit on worktrees. The real limits are your plan’s usage limits, your machine’s memory when each worktree runs its own dev server, and how many diffs you can actually review. Start with two or three and add more once reviewing keeps up.

Does this work with Codex too?

Yes. Worktrees, ports and env files are plain git and shell, so the same steps work for Codex, Claude Code, or a mix. Start codex instead of claude in a worktree.

Do git worktrees share node_modules?

No. node_modules is gitignored, so each new worktree starts without it and needs its own install. pnpm’s content-addressed store makes repeat installs fast; npm and yarn re-download or copy from cache.

Why does my second dev server fail with EADDRINUSE?

Both worktrees run the same dev command, which binds the same port. Give each worktree its own PORT and pass it to the dev server. See the guide on git worktree port conflicts for fixes per framework.

Try Dunea Code on your own repo

Free on macOS, Windows and Linux. Bring your Claude Code or Codex plan; neither uses Dunea credits. See what Dunea Code does or read the docs.

Download for Mac

v0.2.43 · Apple Silicon · Free · Other platforms