Run several agents in one sandbox
box.agents runs several agents in one sandbox, each with its own
AgentProfile and coding agent. They share one
machine, its files and its network. Each agent is a durable session with an id
you choose.
| Need | Use |
|---|---|
| One conversation | box.prompt() or box.session(id) |
| Several trusted agents on one machine | box.agents |
| Separate machines or hard CPU and memory limits | Fleets |
| Supervisors and budgets across many agents | agent-runtime |
Install
npm install @tangle-network/sandbox @tangle-network/agent-interfacebox.agents exists after you import @tangle-network/sandbox/agent, which
also exports Sandbox.
Start two agents side by side
import type { AgentProfile } from "@tangle-network/agent-interface";
import { Sandbox } from "@tangle-network/sandbox/agent";
const client = new Sandbox({
apiKey: process.env.TANGLE_API_KEY!,
baseUrl: "https://sandbox.tangle.tools",
});
const plannerProfile = {
name: "planner",
harness: "codex",
prompt: { appendSystemPrompt: "Plan the work before changing files." },
} satisfies AgentProfile;
const builderProfile = {
name: "builder",
harness: "claude-code",
prompt: { appendSystemPrompt: "Implement the request and run the tests." },
} satisfies AgentProfile;
const box = await client.create({
git: { url: "https://github.com/tangle-network/sandbox-demo" },
});
try {
const [planner, builder] = await box.agents.startMany([
{ id: "planner-v1", name: "Planner", backend: { type: "codex", profile: plannerProfile } },
{ id: "builder-v1", name: "Builder", backend: { type: "claude-code", profile: builderProfile } },
]);
const [plan, implementation] = await Promise.all([
planner.prompt("Inspect the repository and propose a precise plan."),
builder.prompt("Inspect the repository and fix the failing test."),
]);
console.log(plan.response, implementation.response);
} finally {
await box.delete();
}box.agents requires an agent-enabled sandbox. Omit agent or set it to
true when creating the sandbox; agent: false disables agent sessions.
Do not use bare: true, which removes the runtime agents need.
Turns of one agent run one at a time. Different agents run side by side.
startMany() starts every agent in parallel and is not a transaction: if one
start fails, the others may already exist. Duplicate ids in one call throw
before anything starts.
Agent ids
The id you pass is the agent’s session id.
box.agents.get(id)reattaches from any process. It returnsnullwhen the agent does not exist.box.agents.start({ id })refuses an id that already exists and tells you to useget()instead. Retry a failed start by callingget()first.box.agents.list()lists the sandbox’s agents.
An id is 1 to 128 characters of letters, digits, ., _, : and -.
Shared workspace
Agents share the sandbox’s files by default. One agent sees another’s edits at once. Nothing merges simultaneous edits to the same file, so coordinate which agent writes where.
const reviewerProfile = {
name: "reviewer",
harness: "codex",
prompt: { appendSystemPrompt: "Review the diff. Report bugs with file:line." },
} satisfies AgentProfile;
const reviewer = await box.agents.start({
id: "reviewer-shared",
backend: { type: "codex", profile: reviewerProfile },
});Isolated git worktrees
An isolated agent works in its own git worktree and branch, so its changes stay
apart until you commit them. The sandbox needs a git repository, so create it
with git.
const isolated = await box.agents.start({
id: "reviewer-v1",
backend: { type: "codex", profile: reviewerProfile },
workspace: { mode: "isolated", parentSessionId: "builder-v1" },
});
await isolated.prompt("Fix every bug you find in the builder's change.");
const changes = await isolated.changes();
console.log(changes.files, changes.diff);
const merged = await isolated.commit({ message: "Apply reviewer fixes" });
console.log(merged.success, merged.commitSha, merged.conflicts);| Call | Effect |
|---|---|
changes() | Returns { files, diff, commits, worktreeInfo }. |
commit({ message, files }) | Applies the changes, or the listed files, to the parent. Returns { success, commitSha, conflicts, appliedFiles }. |
box.agents.stop(id) | Deletes the agent. For an isolated agent it removes the worktree and branch first, so uncommitted work is discarded. Returns { agentId, outcome: "stopped" } or outcome: "not-found". |
Reattach after a restart
const reloaded = await client.get(box.id);
if (!reloaded) throw new Error("The sandbox was deleted.");
const planner = await reloaded.agents.get("planner-v1");
console.log(await planner?.status());
const isolatedAgain = reloaded.agents.task("reviewer-v1");
console.log(await isolatedAgain.changes());task(id) returns a handle for an isolated agent without a network call; its
first operation checks the session.
Upgrade a profile
Do not change an active agent’s profile. Start a new agent with a new id, try it, then retire the old one:
const plannerV2Profile = {
...plannerProfile,
name: "planner",
prompt: { appendSystemPrompt: "Plan the work, then list the tests that prove it." },
} satisfies AgentProfile;
const candidate = await box.agents.start({
id: "planner-v2",
backend: { type: "codex", profile: plannerV2Profile },
workspace: { mode: "isolated", parentSessionId: "planner-v1" },
});
await candidate.prompt("Plan the next feature against this repository.");
await box.agents.stop("planner-v1");A new id lets observers tell versions apart, and a turn in progress never sees its profile change.
Copy a conversation, files or a whole sandbox
| What to copy | API |
|---|---|
| Conversation history | box.session(id).fork() |
| File changes inside one sandbox | An isolated agent |
| The whole filesystem | box.snapshot(), then create from the snapshot |
Trust between agents
Agents in one sandbox trust each other. They share:
- project files and the local network;
- processes and terminals;
- CPU, memory, disk and accelerators;
- every secret the sandbox can see.
Use separate sandboxes for different customers, agents that must not trust each other, separate sets of secrets, hard resource limits or separate failure domains.
Keep your account key and box.agents calls on your server. A browser gets a
short-lived token scoped to one session, and that token cannot list or drive
sibling sessions. See API keys and tokens.
Next
- AgentProfiles
- Fleets: agents on separate sandboxes.
- Coding agent examples