SandboxAgent graphs

Agent graphs

An agent graph is a team with a fixed shape: nodes that do the work, edges that pass work between them, a budget, and a check that decides when the job is done. You can run one from a YAML file with the tangle graph commands, or build one in TypeScript with runGraph from @tangle-network/agent-runtime, where each worker node runs in its own Tangle sandbox.

Use a graph when the roles and their order are known ahead of time. For many independent agents doing the same kind of task, a fleet is simpler. For several agents sharing one machine, see Several agents in one sandbox.

Run a graph file from the CLI

haiku-review.yaml ships with the CLI. A poet writes a haiku, an independent critic counts its syllables, and the run passes only when the critic approves a five, seven, five poem. The key parts:

graph: haiku-review
params:
  topic: { type: string, default: rain at dawn }
nodes:
  - id: poet
    profile:
      prompt:
        systemPrompt: >-
          Write one original English haiku about the assigned topic. ...
  - id: critic
    profile:
      prompt:
        systemPrompt: >-
          Judge the poet's result supplied in the task evidence. ...
entry:
  - node: poet
    task: 'Write a haiku about ${params.topic}.'
edges:
  - kind: routes
    from: poet
    to: critic
    directive: { surface: delegates/worker-brief, version: 1 }
budget:
  maxIterations: 6
  maxTokens: 64000
  deadlineMs: 120000
check:
  all:
    - { node: critic, pointer: /approved, equals: true }
    - { node: critic, pointer: /syllables/0, equals: 5 }

The whole file, with its output, is in Evaluation and runtime examples. It is also in the installed package at node_modules/@tangle-network/sandbox-cli/examples/haiku-review.yaml.

tangle graph show haiku-review.yaml
tangle graph run haiku-review.yaml --param topic="first snow"
tangle graph inspect <run-id>
CommandWhat it does
tangle graph show <file>Draws the nodes, routes, loops and stops in the terminal. --open writes one offline page and opens it; --json prints the shape
tangle graph run <file>Runs the graph. --param key=value sets a parameter, --dry-run validates and prints the graph without starting agents, and --resume <run> continues a saved run
tangle graph inspect <run>Reads a live or finished run

graph run runs each node on a coding agent you are signed in to on your machine, such as Claude Code or Codex. With none signed in, it stops before running with SEAT_NONE_DETECTED, even with --dry-run. --seat harness/model@effort picks the coding agent and model for every node. --cloud runs the graph on Tangle Platform as a durable workflow instead of on your machine.

Answer a gate

A graph can declare gates: points where a person picks one of a fixed set of choices. In a terminal, graph run asks you when it reaches one. In a script, answer ahead of time with --param gate.<gate-id>=<choice>:

tangle graph run release.yaml --param gate.ship=approve

A gate with no answer and no terminal fails with GATE_ANSWER_REQUIRED, and a choice the gate does not list fails with GATE_CHOICE_INVALID. Both errors name the gate and its choices.

Build a graph in TypeScript

npm install @tangle-network/agent-runtime @tangle-network/sandbox

@tangle-network/agent-runtime needs Node 22.19 or later. Save the program as graph.mts and run it with npx tsx graph.mts.

The lead runs on the Tangle Router and delegates once. The coder runs OpenCode in a sandbox, runs a command and reports what it wrote.

import { Sandbox } from "@tangle-network/sandbox";
import { promptHandle, runGraph } from "@tangle-network/agent-runtime/kernel";
 
const apiKey = process.env.TANGLE_API_KEY!;
const sandbox = new Sandbox({ apiKey, baseUrl: "https://sandbox.tangle.tools" });
 
// Name the model's own vendor; a sandbox prefixes a bare model id with the provider.
const model = { provider: "zai", default: "zai/glm-5.3" };
 
const graph = {
  nodes: [
    {
      id: "lead",
      profile: {
        name: "lead", // must equal the node id
        harness: "cli-base", // the root runs on the Router
        model,
        tools: { agent_runtime_coordination_spawn_worker: true },
        prompt: {
          systemPrompt:
            "You coordinate. Spawn the coder once, wait for it to settle, then repeat its output.",
        },
      },
    },
    {
      id: "coder",
      profile: {
        name: "coder",
        harness: "opencode", // runs in a Tangle sandbox
        model,
        prompt: { systemPrompt: "You are a coding agent working in a Linux sandbox." },
      },
    },
  ],
  edges: [
    {
      kind: "delegates" as const,
      from: "lead",
      to: "coder",
      directive: promptHandle("delegates/worker-brief/v1"),
    },
  ],
  deliverable: {
    describe:
      "Have the coder run `echo ANSWER=$((6*7)) > out.txt && cat out.txt` in its working directory and reply with the output.",
    check: (out: unknown) => JSON.stringify(out ?? "").includes("ANSWER=42"),
  },
  budget: { maxIterations: 10, maxTokens: 400_000, deadlineMs: 8 * 60_000 },
};
 
const { result, ledger } = await runGraph(graph, {
  backend: { backend: "sandbox", sandboxClient: sandbox }, // where worker nodes run
  router: { routerBaseUrl: "https://router.tangle.tools/v1", routerKey: apiKey },
  perWorker: { maxIterations: 2, maxTokens: 200_000 },
});
 
console.log(result.kind); // "winner" when the deliverable check passed
console.log(ledger);

ledger has one row per delegation, with its outcome.

Rules the runtime enforces

RuleWhy
Exactly one root: a node that delegates and is never delegated toThe root owns the plan and the stop decision
The root’s profile sets tools.agent_runtime_coordination_spawn_worker: trueDelegation is a tool the root must be allowed to call
Only the root delegatesWorkers cannot start further workers
profile.name equals the node idThe lead starts a worker by name
Every profile has harness, model.provider and a concrete model.defaultA graph that leaves a model to a default is refused before it runs
backend places worker nodesThe root runs on the Router (router), or on a coding agent through driverBackend

Budgets

budget bounds the whole graph by iterations, tokens and a deadline; perWorker bounds each worker. Bound sandbox workers with tokens and a deadline.

Control the worker sandboxes

sandboxClient is any object with the SDK’s create and, optionally, list. Wrap the client to set create options the runtime does not, such as a lifetime cap or network rules:

import assert from "node:assert/strict";
import { Sandbox } from "@tangle-network/sandbox";
 
const sandbox = new Sandbox({
  apiKey: process.env.TANGLE_API_KEY!,
  baseUrl: "https://sandbox.tangle.tools",
});
 
const sandboxClient = {
  create: (options: Parameters<typeof sandbox.create>[0], request?: Parameters<typeof sandbox.create>[1]) =>
    sandbox.create(
      {
        ...options,
        maxLifetimeSeconds: 600,
        egressPolicy: { mode: "strict", allowDomains: ["registry.npmjs.org"] },
      },
      request,
    ),
  list: (options?: Parameters<typeof sandbox.list>[0]) => sandbox.list(options),
};
 
const box = await sandboxClient.create({
  agent: false, maxLifetimeSeconds: 60, egressPolicy: { mode: "open" },
});
try {
  const effective = await box.egress.get();
  assert.equal(effective.policy.mode, "strict");
  assert.deepEqual(effective.policy.allowDomains, ["registry.npmjs.org"]);
  assert((await sandboxClient.list()).some((item) => item.id === box.id));
  const result = await box.exec("printf 'worker defaults applied\n'");
  assert.equal(result.exitCode, 0, result.stderr);
  assert.equal(result.stdout.trim(), "worker defaults applied");
  console.log(effective.policy);
} finally {
  await box.delete();
}

The runtime deletes each worker sandbox when the worker settles.

Read why a run failed

result.kind is "winner" when the deliverable check passed. When a worker fails, the reason is in the spawn journal. Pass a journal and a blob store to keep it:

import {
  InMemoryResultBlobStore,
  InMemorySpawnJournal,
} from "@tangle-network/agent-runtime/kernel";
 
const journal = new InMemorySpawnJournal();
const blobs = new InMemoryResultBlobStore();
await runGraph(graph, {
  backend: { backend: "sandbox", sandboxClient: sandbox },
  router: { routerBaseUrl: "https://router.tangle.tools/v1", routerKey: apiKey },
  journal,
  blobs,
});

A workflow’s script.run step can also run an agent loop with the bundled agent-runtime functions; see Workflow templates.

Next