SandboxFleets

Run fleets of sandboxes

A fleet is a named set of sandboxes for one job. Give it agents, each with its own task, and it creates the sandboxes, runs every agent at once, and reports each result. Give it a budget, and the platform refuses any charge past it.

Run agents under a budget

The snippets on this page use a client created with new Sandbox({ apiKey, baseUrl: "https://sandbox.tangle.tools" }). The complete programs are in fleet examples.

const fleet = await client.fleets.create({
  agents: [
    { agentId: "tests", task: "List the three slowest tests and why." },
    { agentId: "todos", task: "Find every TODO and group them by file." },
    { agentId: "deps", task: "Say what each dependency is used for." },
  ],
  placement: { agentsPerSandbox: 3 },
  defaults: {
    git: { url: "https://github.com/tangle-network/sandbox-demo" },
    backend: { type: "claude-code" },
    maxLifetimeSeconds: 900,
  },
  budgetUsd: 1,
});
try {
  const results = await fleet.runAll();
  for (const r of results) console.log(r.agentId, r.status, r.result?.response);
  console.log(await fleet.spend());
} finally {
  await fleet.delete();
}

tangle fleet run creates the fleet, runs one turn on every agent, prints one line per agent (id, coding agent and model, status, duration, cost and response), then deletes the fleet unless you pass --keep. --agents takes a count or a JSON file of agent specs with their own tasks. --model sets one model for every agent and runs them all on OpenCode; to keep Claude Code or Codex, set harness and model per agent in the file. The CLI sets no repository; give agents a repository from the SDK with defaults.git.

Agents

Each entry in agents is an agent spec, or pass a number for that many identical agents.

FieldMeaning
agentIdThe agent’s id, also its session id. Defaults to agent-<index>.
taskThe agent’s own task. run() sends it when called without a message.
harnessThe coding agent: Claude Code ("claude-code"), Codex ("codex"), OpenCode ("opencode"), Pi ("pi") and others. Defaults to the profile’s, then defaults.backend.type.
modelA model id or a model config. Defaults to the profile’s, then defaults.backend.model.
profileAn inline AgentProfile, or { profileId, revision } from your profile catalog. A revision refuses the fleet unless the catalog still holds exactly that revision.
placement"shared" (default) or "dedicated".

defaults holds the create options every sandbox gets.

Placement

placementEffect
"auto" (default)Packs as many agents onto each sandbox as its memory holds.
"dedicated"One sandbox per agent.
{ agentsPerSandbox: n }Exactly n agents per sandbox.

Agents on one sandbox share its files, CPU and memory. Give an agent its own sandbox when it writes to the repository or runs untrusted code.

Run and watch

fleet.run() starts one turn on each agent at once and yields events as they happen. fleet.runAll() waits and returns every result.

for await (const event of fleet.run()) {
  if (event.type === "agent.started") console.log("started", event.agentId);
  if (event.type === "agent.finished") console.log(event.result.agentId, event.result.status);
  if (event.type === "fleet.finished") console.log(event.durationMs, event.spend);
}

Pass a message to send the same prompt to every agent, or a function of the agent to build one per agent. { agents: [...] } limits the run to some agents, and signal aborts every turn the run started.

Each result has status (completed, failed, cancelled or budget_exhausted), result (the turn’s response, usage and trace id), durationMs and error. costUsd is what the platform charged the agent’s sandbox, set only when the agent is alone on its sandbox; result.costUsd is the turn’s model charges alone.

Budgets

With budgetUsd, the fleet creates a platform key with that budget and uses it to create its sandboxes and drive their turns. Every model call and compute charge made through the fleet counts against the budget, from any process, and a charge past it is refused. A turn that hits the budget ends budget_exhausted; turns already running are refused at their next charge.

const spend = await fleet.spend();
// { capUsd, spentUsd, inFlightUsd, reservedForChildrenUsd, remainingUsd }

Only the client that created the fleet holds its key. Other processes can read, cancel and delete the fleet, but not run its agents. A turn you drive on a fleet sandbox with another key is billed to that key, outside the budget.

To stop everyone before the budget runs out, watch spend() and call cancel():

const { interrupted, children, failures } = await fleet.cancel();

cancel() aborts the runs this handle started, interrupts every agent’s session, and cancels child fleets. The sandboxes keep running until delete().

Collect files from the agents

const files = await fleet.collectArtifacts(
  fleet.agents.map((a) => ({
    machineId: a.machineId,
    path: `/home/agent/sandbox-demo/README.${a.agentId}.md`,
    label: a.agentId,
  })),
);
for (const file of files) console.log(file.label, file.content.length);

Run commands across machines

Without agents, a fleet is a set of machines you run commands on:

const files = ["cli", "dates", "format"];
const fleet = await client.fleets.create({
  machines: files.map((name) => ({ machineId: name })),
  defaults: { git: { url: "https://github.com/tangle-network/sandbox-demo" } },
});
try {
  const results = await Promise.all(
    files.map((name) => fleet.exec(name, `node --test test/${name}.test.js`)),
  );
  results.forEach((r, i) => console.log(files[i], r.exitCode === 0 ? "pass" : "fail"));
} finally {
  await fleet.delete();
}

fleet.dispatchExec(command, { machines }) runs one command on many machines and returns a result per machine. client.fleets.createWithCoordinator({ coordinator, workers }) creates a coordinator machine plus workers.

Limit what one create can request

policy caps a create call before any sandbox is created:

FieldCaps
maxMachinesSandboxes in this create.
maxTotalCpu, maxTotalMemoryMb, maxTotalStorageMbTotal resources across machines with explicit resources.
maxTotalAccelerators, allowAcceleratorsGPUs.
maxLifetimeSecondsLifetime of any machine.
maxSpendUsdA create-time spend estimate check. Use budgetUsd for a cap the platform enforces.
allowedEnvironments, allowedTemplateIds, allowedDriversWhere machines may run.

maxConcurrentCreates sets how many sandboxes are created at once. Your plan’s quotas still apply; see Limits. With cleanupOnFailure (on by default), a failed create deletes the machines already created.

Child fleets

An agent can spawn its own fleet. Pass parent: { fleetId, agentId }: the child’s cap is reserved from the parent’s remaining budget when the child is created, and cancelling or deleting the parent reaches the child.

Find a fleet later

const fleet = await client.fleets.list({ fleetId: "release-review" });
await fleet.delete();

list({ fleetId }) returns that one fleet. Set fleetId at create to choose the id.

Use fleets from Python

Python fleets run commands across machines. Agent fleets with run() and budgets are TypeScript and CLI only.

fleet = client.fleets.create_with_coordinator(
    CreateSandboxFleetWithCoordinatorOptions(
        defaults=CreateSandboxOptions(max_lifetime_seconds=30 * 60),
        policy=SandboxFleetPolicy(max_machines=3, max_concurrent_creates=2),
        workers=[SandboxFleetMachineSpec(machine_id="worker-1"), SandboxFleetMachineSpec(machine_id="worker-2")],
    )
)
try:
    results = fleet.dispatch_exec("hostname", FleetExecDispatchOptions(machines=fleet.ids))
    for result in results:
        print(result["machineId"], result.get("result", {}).get("stdout"))
finally:
    fleet.delete()

The Python SDK refuses shared fleet workspaces. The complete Python program is in fleet examples.

Agents can manage fleets themselves through the fleet tool packs; see Integrations.

Next