---
title: Parallel coding agents
description: Several coding agents on one repo, fully isolated from one another.
order: 1
icon: git-branch
tags: [agents, worktrees, git]
related: [babysit-pr, ssh-and-git-push, isolated-services]
---
import AgentPrompt from '../../components/AgentPrompt.astro';
import AgentPromptLink from '../../components/AgentPromptLink.astro';
import Agent from '../../components/Agent.astro';
import AgentOnly from '../../components/AgentOnly.astro';
import AgentPicker from '../../components/AgentPicker.astro';
## The problem
Two agents in one checkout trip over each other: one switches branch while the other is mid-edit, one's `git stash` swallows the other's work, both run the dev server on the same files. You want one working tree and one branch per agent, and a container for each so their processes stay apart too.
That is what repeat instances do. The first `devsandbox run` of a sandbox mounts the repo itself; every further instance of the same repo gets its own **git worktree** under the config dir, on its own branch, in its own container. Gitignored files a fresh checkout lacks, like `.env`, are copied or linked in for you.
<AgentPromptLink />
## Config
<AgentPicker />
```toml title="devsandboxes.toml" agent="claude"
[sandbox.app]
folder = "../app" # your repo, next to this config dir
image = "node:22" # any image with the repo's toolchain
caches = ["npm"] # one npm cache for every instance
worktree-link = [".env"] # one .env, shared by every instance
# Your Claude Code login and settings, from the host.
mounts = ["source=${localEnv:HOME}/.claude,target=/root/.claude,type=bind"]
containerEnv = { CLAUDE_CONFIG_DIR = "/root/.claude" }
[sandbox.app.postCreateCommand] # once, when an instance is created
deps = "npm install"
agent = "npm install -g @anthropic-ai/claude-code"
```
```toml title="devsandboxes.toml" agent="codex"
[sandbox.app]
folder = "../app" # your repo, next to this config dir
image = "node:22" # any image with the repo's toolchain
caches = ["npm"] # one npm cache for every instance
worktree-link = [".env"] # one .env, shared by every instance
# Your Codex login and settings, from the host.
mounts = ["source=${localEnv:HOME}/.codex,target=/root/.codex,type=bind"]
[sandbox.app.postCreateCommand] # once, when an instance is created
deps = "npm install"
agent = "npm install -g @openai/codex"
```
<AgentOnly agent="claude">
On macOS <Agent /> keeps its login in the Keychain: run `/login` once in any instance.
</AgentOnly>
## Try it
One instance per task:
```bash
devsandbox run app --name fix-login # your checkout, ../app
devsandbox run app --name add-profile-pic --branch feat/profile-pic # its own worktree
devsandbox ps
```
- `--name`: the instance name.
- `--branch`: the branch name (default `sandbox/<name>`).
Start <Agent /> in each:
```bash agent="claude"
devsandbox exec -it fix-login claude
devsandbox exec -it add-profile-pic claude
```
```bash agent="codex"
devsandbox exec -it fix-login codex
devsandbox exec -it add-profile-pic codex
```
When a task is done, push its branch ([ssh and git push](/examples/ssh-and-git-push)) and remove the instance, container and worktree:
```bash
devsandbox rm add-profile-pic
```
## How it works
- Worktrees live in `.worktrees/<name>` next to `devsandboxes.toml`; `rm` deletes them with the container.
- [`worktree-link`](/docs/config#devsandbox-extras) symlinks one shared copy into each worktree; `worktree-include` or a `.worktreeinclude` copies instead.
- <Agent />'s config dir is mounted from the host: one login for every instance.
## Agent instructions
<AgentPrompt
goal="Set up devsandbox so several <agent> sessions can work on my repo at <repo path> in parallel, each in its own sandbox instance on its own git worktree and branch."
steps={[
'In devsandboxes.toml, add a `[sandbox.<sandbox name>]` whose `folder` points at <repo path> (relative to the config dir), with an `image` that has the toolchain the repo needs. Install <agent> in `postCreateCommand` and mount its host config dir (its login) into the container.',
'Put gitignored files every instance needs, like .env, in `worktree-link`.',
]}
verify={[
'Start one instance per task with `devsandbox -C <config dir> run <sandbox name> --name <task>` for each of <task names>.',
'Check `devsandbox -C <config dir> exec <task> git status` in each instance shows its own branch, and `devsandbox -C <config dir> exec <task> <agent> --version` works.',
]}
/>