> ## Documentation Index
> Fetch the complete documentation index at: /llms.txt
> Use this file to discover all available pages before exploring further.

# Agent swarm intelligence

> Fan a problem out to many agents, each on its own machine, and synthesize one answer.

A swarm splits a problem across many worker agents and merges their findings into one answer. On boxd, each worker gets a full machine of its own, with Claude Code, Codex, and OpenCode pre-installed, so a worker is one `exec` away from doing real work. Claude Code and Codex are signed in on your private machines once you sign in for the organization, and OpenCode needs a login on each machine. Machines boot in under 10ms and cost near zero when idle, which makes spawning a machine per worker feel like calling a function.

<video autoPlay muted loop playsInline src="/videos/agent-swarm.mp4" />

Three scouts research in parallel, one machine each, then a lead agent reads all three reports and writes the recommendation.

## How it works

1. The coordinator (your script, or an agent on a boxd machine) defines the sub-tasks.
2. It creates one machine per worker, fresh or [forked](/guides/fork.md) from a warm source machine.
3. Each worker runs a coding agent headless inside its machine, and the output comes back through `exec`.
4. A lead step reads every report and writes the synthesis.
5. Workers are deleted, and the fleet goes back to zero.

## Full example

Three scouts research the same question through different lenses, then a lead agent reads all three reports and produces one recommendation:

> [tab] TypeScript
>
> ```typescript
> import { Boxd } from "@boxd-sh/sdk";
>
> const LENSES: Record<string, string> = {
>   market: "Size the market for on-device speech models. Write a compact report.",
>   tech:   "Survey the strongest open-source on-device speech models. Write a compact report.",
>   risks:  "List the main risks of betting on on-device speech models. Write a compact report.",
> };
>
> const boxd = new Boxd();   // reads BOXD_API_KEY; authenticates automatically inside a machine
>
> async function scout(name: string, prompt: string): Promise<string> {
>   const machine = await boxd.machines.create({ name });
>   await boxd.machines.waitUntilReady(machine.id);
>   const result = await boxd.machines.exec(machine.id, {
>     command: `claude -p "${prompt}" --dangerously-skip-permissions`,
>     timeout: 600_000,
>   });
>   await boxd.machines.delete(machine.id);
>   return result.stdout;
> }
>
> const reports = await Promise.all(
>   Object.entries(LENSES).map(([lens, prompt]) => scout(`scout-${lens}`, prompt))
> );
>
> // Lead agent: one more machine reads every report and merges them
> const lead = await boxd.machines.create({ name: "lead" });
> await boxd.machines.waitUntilReady(lead.id);
> await boxd.machines.files.upload(lead.id, "/home/boxd/reports.md", reports.join("\n\n---\n\n"));
> const final = await boxd.machines.exec(lead.id, {
>   command: `claude -p "Read /home/boxd/reports.md and write one recommendation." --dangerously-skip-permissions`,
>   timeout: 600_000,
> });
> console.log(final.stdout);
> await boxd.machines.delete(lead.id);
> await boxd.close();
> ```

> [tab] Python
>
> ```python
> from concurrent.futures import ThreadPoolExecutor
> from boxd import Boxd
>
> LENSES = {
>     "market": "Size the market for on-device speech models. Write a compact report.",
>     "tech":   "Survey the strongest open-source on-device speech models. Write a compact report.",
>     "risks":  "List the main risks of betting on on-device speech models. Write a compact report.",
> }
>
> boxd = Boxd()   # reads BOXD_API_KEY; authenticates automatically inside a machine
>
> def scout(name: str, prompt: str) -> str:
>     machine = boxd.machines.create(name)
>     boxd.machines.wait_until_ready(machine.id)
>     result = boxd.machines.exec(
>         machine.id,
>         f'claude -p "{prompt}" --dangerously-skip-permissions',
>         timeout=600,
>     )
>     boxd.machines.delete(machine.id)
>     return result.stdout
>
> with ThreadPoolExecutor() as pool:
>     reports = list(pool.map(
>         lambda kv: scout(f"scout-{kv[0]}", kv[1]), LENSES.items()
>     ))
>
> # Lead agent: one more machine reads every report and merges them
> lead = boxd.machines.create("lead")
> boxd.machines.wait_until_ready(lead.id)
> boxd.machines.files.upload(lead.id, "/home/boxd/reports.md", "\n\n---\n\n".join(reports))
> final = boxd.machines.exec(
>     lead.id,
>     'claude -p "Read /home/boxd/reports.md and write one recommendation." '
>     "--dangerously-skip-permissions",
>     timeout=600,
> )
> print(final.stdout)
> boxd.machines.delete(lead.id)
> ```

The workers need nothing installed first, because the agents are pre-installed on every machine. Once you sign in to Claude Code for the org, the login is served to every private machine you have in it, new machines included. See [Coding agents](/use-cases/coding-agents.md).

| Stage | What happens |
| - | - |
| Fan out | The coordinator creates one machine per lens |
| Work | A coding agent runs headless inside each worker |
| Collect | Each report comes back as the `exec` result |
| Synthesize | A lead machine reads all reports and writes one answer |
| Teardown | Workers are deleted, and the fleet is gone |

## Share state over the private network

Workers can do more than report back at the end. Inside any of your machines, `<machine>.boxd` resolves to that machine's private IP, so a swarm can share a queue, a database, or a scoreboard by running it on one machine and letting the others connect by name:

```bash
curl http://coordinator.boxd:8000/next-task
psql -h shared-db.boxd -U postgres
```

Name resolution is private to you, and only machines you own resolve. See [Machine to machine](/guides/vm-to-vm.md).

## Let the swarm run itself

The coordinator can be a machine too. Every machine except an isolated one carries the in-machine `boxd` CLI and the SDKs. Inside a private machine they act as the machine's owner, so an agent inside one machine can create siblings, hand them work, and clean up. The example above runs unchanged on a boxd machine, where `Boxd()` needs neither a key nor configuration.

## Good to know

### Warm-start workers with a fork

A fresh machine is generic, and a worker that needs your repo, dependencies, or a loaded model should start from a prepared source machine instead. Set up a source machine named `baseline` once, then fork it for each worker with `machines.fork`. Each fork lands in under 200ms with the source machine's disk and memory intact. For a starting state that outlives the machine, [snapshot it](/guides/snapshot-your-app.md), and for identical starting states at scale see [Reproducible RL environments](/use-cases/reproducible-rl-environments.md).

### Keep workers apart

Independent attempts should stay independent, and workers running generated code should not reach their siblings. [Networks](/guides/vm-to-vm.md#networks) partition the swarm, and each worker then reaches the coordinator and nothing else:

```bash
boxd machine new coordinator --networks=job-1,job-2,job-3
boxd machine new worker-1 --networks=job-1
boxd machine new worker-2 --networks=job-2
boxd machine new worker-3 --networks=job-3
```

For untrusted work, create workers with `--isolated` and they reach nothing of yours beyond networks you grant explicitly. See [Sandboxes](/use-cases/sandboxes.md).

### Mind the cost and the cap

Delete workers when their task ends, since a deleted machine costs nothing. Workers you keep for the next batch [hibernate](/guides/suspend-resume.md) on their own and wake on the next request. The cap is 50 machines per organization by default, extendable on request, and the coordinator counts too. See [Resources and limits](/guides/resources.md).
