SandboxOpenAI

Give an OpenAI agent a sandbox

openaiTools(box) turns a sandbox into Chat Completions function tools and runs each tool call the model sends in the sandbox. It works with OpenAI and with any OpenAI-compatible endpoint, including the Tangle Router. To run Codex itself in the sandbox instead, see Run a coding agent.

Install

npm install openai @tangle-network/sandbox

Set OPENAI_API_KEY for OpenAI and TANGLE_API_KEY for the sandbox.

Run a tool loop

import OpenAI from "openai";
import { Sandbox } from "@tangle-network/sandbox";
import { openaiTools } from "@tangle-network/sandbox/agent";
 
const openai = new OpenAI();
const client = new Sandbox({
  apiKey: process.env.TANGLE_API_KEY!,
  baseUrl: "https://sandbox.tangle.tools",
});
 
const box = await client.create();
try {
  const { tools, handleAssistantMessage } = openaiTools(box, { sessionId: "chat-1" });
 
  const messages: OpenAI.ChatCompletionMessageParam[] = [
    { role: "user", content: "Plot sin(x) for x in [0, 2π] and describe the plot." },
  ];
  for (let step = 0; step < 6; step++) {
    const reply = await openai.chat.completions.create({ model: "gpt-5.4", tools, messages });
    const message = reply.choices[0].message;
    messages.push(message);
    if (!message.tool_calls?.length) break;
    messages.push(...(await handleAssistantMessage(message)));
  }
} finally {
  await box.delete();
}

Use any model id from OpenAI’s model list.

handleAssistantMessage runs every tool call on the message in parallel and returns one role: "tool" message per call. handleToolCall(call) runs one.

Use a Router model

The same tools work against the Tangle Router with your Tangle key, so one key pays for the model and the sandbox. Send the tools with each Chat Completions request and run the tool calls it returns:

const reply = await fetch("https://router.tangle.tools/v1/chat/completions", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.TANGLE_API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ model: "<model-id>", messages, tools }),
});
const { message } = (await reply.json()).choices[0];
messages.push(message, ...(await handleAssistantMessage(message)));

GET /v1/router-models with your Sandbox key lists the models you can use, as does the model list. The complete loop is in product examples.

Tool format

Each tool is a Chat Completions function:

{
  type: "function",
  function: {
    name: "sandbox_run_code",
    description: "Execute code in a persistent language kernel inside the sandbox. ...",
    parameters: { type: "object", properties: { /* ... */ }, required: ["language", "source"] },
  },
}

The default set is sandbox_run_code, sandbox_exec, sandbox_read, sandbox_write, sandbox_list and sandbox_search. Restrict it with allow:

openaiTools(box, { allow: ["sandbox_run_code"] });

Arguments and errors

The model sends function.arguments as a JSON string. The pack accepts a JSON object, including {} for a tool with no arguments, and returns a tool error for blank, malformed, scalar, array or null input before running anything. An unknown tool or a failed call comes back as a tool message whose content is {"error": "..."}.

The pack reads Chat Completions tool calls, { id, function: { name, arguments } }.

Types

import type {
  OpenAITool,
  OpenAIToolCall,
  OpenAIToolPack,
  OpenAIToolPackOptions,
  OpenAIToolResultMessage,
} from "@tangle-network/sandbox/agent";

openaiLifecycleTools(client) and openaiFleetTools(client) let the model create sandboxes and fleets itself; see Integrations. For retries and duplicate tool calls, see Retries and duplicate work.

Next