# Run agents in parallel

> Give each coding agent its own flockvm VM, create them with the CLI, fan tasks out over SSH, collect the branches, pick the best of N, and clean up.

- URL: https://flockvm.com/docs/agents/parallel-agents
- Section: Coding agents
- Last updated: 2026-11-05

Two agents in one checkout overwrite each other's files, fight over port 3000 and migrate the same database. One VM per agent removes all of that: each one gets its own files, ports, database and Docker daemon. This page shows how to set up several VMs, start one agent in each over SSH, and bring the results back.

## When one VM per agent pays off

| Setup                               | Good for                                                                               | What breaks first                     |
| ----------------------------------- | -------------------------------------------------------------------------------------- | ------------------------------------- |
| Git worktrees + tmux on your laptop | 2 to 4 agents on code that needs no server or database                                 | Ports, the shared database, RAM       |
| One flockvm VM per agent            | Agents that start servers, run migrations or Docker, or run without permission prompts | Your review time, and the host's room |

The trade-offs for each agent count are in [running Claude Code agents in parallel](https://flockvm.com/learn/run-claude-code-agents-in-parallel). A VM does not change your model plan's usage limits: N agents use quota about N times as fast.

## Create the VMs

1. **Name them after the task**

   Names take lowercase letters, digits and dashes, up to 40 characters. Use one prefix per task and a number per attempt, so the VM list reads like a batch: `fix-auth-1`, `fix-auth-2`, `fix-auth-3`.

2. **Create each VM**

   With the [CLI](https://flockvm.com/docs/cli), one command per agent: same size, the **Agent** image (the default), your saved SSH key, a different name.

   ```bash title="On your laptop"
   for i in 1 2 3; do
     flockvm vm create "fix-auth-$i" --size small --wait
   done
   ```

   In the [dashboard](https://flockvm.com/dashboard), it is **New VM** once per agent, with the same choices. The [API](https://flockvm.com/docs/api/vms#create-a-vm) does the same from your own code.

   If a size does not fit the host right now, the VM is refused with `409 capacity` (the form says so too): pick a smaller size. See [Limits](https://flockvm.com/docs/vms/limits).

3. **Write down the ports**

   Each VM has its own SSH port. Write them into a file, one VM per line:

   ```bash title="On your laptop"
   flockvm vm list --json \
     | jq -r '.vms[] | select(.name | startswith("fix-auth-")) | "\(.name) \(.ssh.port)"' \
     | sort > vms.txt
   ```

   ```text title="vms.txt"
   fix-auth-1 22001
   fix-auth-2 22002
   fix-auth-3 22003
   ```

   Without the CLI, copy the ports from the SSH command of each VM in your VM list.

   ![The VM list with several VMs](https://flockvm.com/docs/screenshots/fleet.webp "Each row shows the VM's state and its SSH command.")

Optionally, add the VMs to your SSH config so you can type `ssh fix-auth-1` (`User agent` on Minimal VMs):

```ssh-config title="~/.ssh/config"
Host fix-auth-*
  HostName HOST
  User user
Host fix-auth-1
  Port 22001
Host fix-auth-2
  Port 22002
Host fix-auth-3
  Port 22003
```

## Prepare every VM in one loop

On the Agent image, Claude Code and Codex are already installed, and you log in as `user`. Two credentials go into each VM: the agent's sign-in, and a key to clone and push. Create both once, on your laptop:

- `claude.env`, a file with one line, `export ANTHROPIC_API_KEY=…`, with your organization's Claude Console key (or turn on [Agent credentials](https://flockvm.com/docs/vms/environments#agent-credentials) and skip this file). With a Claude plan instead, leave the file empty, and after the loop below sign each VM in from inside it: `ssh -t -p 22001 user@HOST claude`, then `/login`, then `/exit`. Never copy a plan token into a VM. See [Sign Claude Code in](https://flockvm.com/docs/agents/claude-code#sign-claude-code-in).
- A deploy key for the repository, separate from your personal key: `ssh-keygen -t ed25519 -N '' -f ~/.ssh/flockvm-deploy`, then add `~/.ssh/flockvm-deploy.pub` under **Settings → Deploy keys** on GitHub, with write access.

Then copy them to every VM, trust GitHub's host key and clone the repository:

```bash title="On your laptop"
while read -r name port; do
  echo "== $name"
  scp -q -P "$port" claude.env user@HOST:.claude.env < /dev/null
  scp -q -P "$port" ~/.ssh/flockvm-deploy user@HOST:.ssh/id_ed25519 < /dev/null
  ssh -n -p "$port" user@HOST '
    chmod 600 ~/.claude.env ~/.ssh/id_ed25519
    grep -q claude.env ~/.bashrc || echo ". ~/.claude.env" >> ~/.bashrc
    ssh-keyscan -t ed25519 github.com 2>/dev/null >> ~/.ssh/known_hosts
    [ -d repo ] || git clone -q git@github.com:your-org/your-repo.git repo'
done < vms.txt
```

`ssh -n` and `< /dev/null` keep `ssh` and `scp` from reading the rest of `vms.txt` as their input.

**On Minimal VMs: create the agent user and install Claude Code first**

The Minimal image has no agent CLI and you log in as `root`. Run this script as root on each VM before the loop above, and use `agent@HOST` instead of `user@HOST` everywhere on this page. Adapt the install line for [Codex](https://flockvm.com/docs/agents/codex).

```bash title="setup-vm.sh"
#!/bin/bash
# Runs as root inside a fresh Minimal VM.
set -euo pipefail
id agent >/dev/null 2>&1 || useradd -m -s /bin/bash agent
install -d -m 700 -o agent -g agent /home/agent/.ssh
install -m 600 -o agent -g agent /root/.ssh/authorized_keys /home/agent/.ssh/authorized_keys
usermod -aG docker agent
npm install -g @anthropic-ai/claude-code >/dev/null
```

```bash title="On your laptop"
while read -r name port; do
  ssh -p "$port" root@HOST 'bash -s' < setup-vm.sh
done < vms.txt
```

Here `ssh` reads the script as its input, so it does not read `vms.txt`.

## Fan out the task

Put the task in a file, and a small script that starts the agent in a detached tmux session on its own branch:

```text title="task.md"
Fix the session expiry bug in src/auth (users are logged out after 5 minutes).
Add a test that reproduces it. Run the whole test suite. Commit your work.
```

```bash title="run-agent.sh"
#!/bin/bash
# Usage: run-agent.sh BRANCH. Runs as user inside the VM.
set -euo pipefail
. ~/.claude.env
cd ~/repo
git switch -q -c "$1"
tmux new -d -s agent "claude -p --dangerously-skip-permissions \"\$(cat ~/task.md)\" 2>&1 | tee ~/agent.log"
```

Start every agent:

```bash title="On your laptop"
while read -r name port; do
  scp -q -P "$port" task.md run-agent.sh user@HOST: < /dev/null
  ssh -n -p "$port" user@HOST "bash ~/run-agent.sh agent/$name"
  echo "started $name"
done < vms.txt
```

The agents keep working after the loop ends and after you close your laptop. To watch one, attach to its session: `ssh -t -p 22002 user@HOST tmux attach -t agent`. More on sessions and logs in [Long-running sessions](https://flockvm.com/docs/agents/tmux).

> **Tip: Different tasks, same loop.** For a backlog instead of N attempts at one task, give each VM its own task file: `scp task-$name.md …:task.md`.

## Check progress

```bash title="On your laptop"
while read -r name port; do
  state=$(ssh -n -p "$port" user@HOST 'tmux has-session -t agent 2>/dev/null && echo running || echo done')
  echo "$name: $state"
done < vms.txt
```

## Collect the results

Each agent works on its own branch. Push them all, then review them as pull requests or locally:

```bash title="On your laptop"
while read -r name port; do
  ssh -n -p "$port" user@HOST 'cd ~/repo && git push -q -u origin HEAD'
done < vms.txt
git fetch origin 'refs/heads/agent/*:refs/remotes/origin/agent/*'
git branch -r --list 'origin/agent/*'
```

For files outside Git, such as logs, reports or build output, copy them back:

```bash title="On your laptop"
mkdir -p results
while read -r name port; do
  scp -q -P "$port" user@HOST:agent.log "results/$name.log" < /dev/null
  rsync -az -e "ssh -p $port" "user@HOST:repo/coverage/" "results/$name-coverage/" < /dev/null
done < vms.txt
```

## Pick the best of N

When N agents try the same task, let the tests make the first pass. Run the suite in every VM and list which attempts pass, with the size of each diff:

```bash title="On your laptop"
while read -r name port; do
  result=$(ssh -n -p "$port" user@HOST '
    cd ~/repo
    if npm test >/dev/null 2>&1; then t=pass; else t=fail; fi
    echo "$t $(git diff --shortstat main...HEAD)"')
  echo "$name: $result"
done < vms.txt
```

Review the passing attempts first, smallest diff first, and merge one. How much best-of-N helps, and how to score attempts, is in [best-of-N for coding agents](https://flockvm.com/learn/best-of-n-coding-agents).

## What it costs

VMs bill per second while they start and run, and not while paused or stopped, for now.

| Batch                                                | Size                 | Time        | VM cost                                    |
| ---------------------------------------------------- | -------------------- | ----------- | ------------------------------------------ |
| 3 attempts at one task                               | `small` ($0.016/h)   | 30 min each | 3 × 0.5 h × $0.016 = $0.024                |
| 5 tasks from a backlog                               | `default` ($0.032/h) | 45 min each | 5 × 0.75 h × $0.032 = $0.12                |
| 4 attempts, then 1 kept for review for a day, paused | `default`            | 1 h each    | 4 × 1 h × $0.032 = $0.128, $0 while paused |

Model tokens are billed separately by your provider, and grow with N. Your organization's spend shows on the [Usage page](https://flockvm.com/docs/billing/usage).

## Clean up

1. **Keep the VM you are still reviewing**

   Click **Pause** on it. Its memory, processes and files stay, and billing stops.

2. **Delete the others**

   On each VM page, click **Delete**, then **Delete for good**. The disk and a paused VM's saved memory go too, along with the credentials you copied in. Push anything you want to keep first.

3. **Revoke the credentials**

   Delete the deploy key on GitHub, and revoke the Claude token or API key if the agents ran code you don't trust.

With the CLI, pause or delete the whole batch at once:

```bash title="On your laptop"
flockvm vm pause fix-auth-2
flockvm vm list --json \
  | jq -r '.vms[] | select(.name | startswith("fix-auth-") and . != "fix-auth-2") | .name' \
  | while read -r name; do flockvm vm delete "$name" --yes; done
```

Then remove the `Host fix-auth-*` entries from `~/.ssh/config`, and the old host keys from `~/.ssh/known_hosts` (`ssh-keygen -R "[HOST]:22001"`): the next VM that gets the same port has a new host key.

## Let an agent run the batch

With the [MCP server](https://flockvm.com/docs/mcp), an orchestrating agent such as Claude Code on your laptop can create the VMs itself (`create_vm`), read their SSH commands (`get_vm`), and pause or delete them when the attempts are reviewed. Give it a dedicated API key with an expiry and the **Create and use VMs** preset: it can create billed VMs, and delete only the ones it created. See [MCP security](https://flockvm.com/docs/mcp/overview#security).

## Next steps

- [Coding agent swarms](https://flockvm.com/use-cases/coding-agent-swarms): What breaks when agents share a machine, and what 5, 10 and 20 agents cost.
- [Run Claude Code in a VM](https://flockvm.com/docs/agents/claude-code): The single-VM setup, step by step.
- [Long-running sessions](https://flockvm.com/docs/agents/tmux): tmux, logging and pause/resume.
- [Pricing](https://flockvm.com/docs/billing/pricing): Prices per size, per second.
