Skip to content

Connect an LLM

An LLM can write code and AgentBox can run it safely in a separate sandbox. The application defines an execute_python tool, passes it to the model, executes the returned code, and sends the result back to the model.

OpenAI

Install the OpenAI and AgentBox SDKs:

npm install openai @abox-dev/sdk

Set OPENAI_API_KEY and AGENTBOX_API_KEY. The example uses the Responses API with gpt-5.6-luna.

mjs
import OpenAI from "openai";
import { Sandbox } from "@abox-dev/sdk";

const openai = new OpenAI();
const tools = [
  {
    type: "function",
    name: "execute_python",
    description: "Run Python code in an isolated AgentBox sandbox",
    parameters: {
      type: "object",
      properties: {
        code: { type: "string", description: "Python source code" },
      },
      required: ["code"],
      additionalProperties: false,
    },
    strict: true,
  },
];

const response = await openai.responses.create({
  model: "gpt-5.6-luna",
  input: "Use Python to calculate the sum of 2, 3, and 5.",
  tools,
});
const toolCall = response.output.find(
  (item) => item.type === "function_call" && item.name === "execute_python",
);
const { code } = JSON.parse(toolCall.arguments);

const sandbox = await Sandbox.create();
let result;

try {
  const program = "/tmp/tool.py";
  await sandbox.files.write(program, code);
  result = await sandbox.commands.run(`python3 ${program}`);
} finally {
  await sandbox.kill();
}

const finalResponse = await openai.responses.create({
  model: "gpt-5.6-luna",
  previous_response_id: response.id,
  tools,
  input: [
    {
      type: "function_call_output",
      call_id: toolCall.call_id,
      output: result.stdout,
    },
  ],
});

console.log(finalResponse.output_text);

Anthropic

Install the Anthropic and AgentBox SDKs:

npm install @anthropic-ai/sdk @abox-dev/sdk

Set ANTHROPIC_API_KEY and AGENTBOX_API_KEY. The example uses the Messages API with claude-sonnet-5.

mjs
import Anthropic from "@anthropic-ai/sdk";
import { Sandbox } from "@abox-dev/sdk";

const anthropic = new Anthropic();
const tools = [
  {
    name: "execute_python",
    description: "Run Python code in an isolated AgentBox sandbox",
    input_schema: {
      type: "object",
      properties: {
        code: { type: "string", description: "Python source code" },
      },
      required: ["code"],
    },
  },
];
const messages = [
  {
    role: "user",
    content: "Use Python to calculate the sum of 2, 3, and 5.",
  },
];

const response = await anthropic.messages.create({
  model: "claude-sonnet-5",
  max_tokens: 1024,
  tools,
  tool_choice: { type: "auto", disable_parallel_tool_use: true },
  messages,
});
const toolCall = response.content.find(
  (block) => block.type === "tool_use" && block.name === "execute_python",
);

const sandbox = await Sandbox.create();
let result;

try {
  const program = "/tmp/tool.py";
  await sandbox.files.write(program, toolCall.input.code);
  result = await sandbox.commands.run(`python3 ${program}`);
} finally {
  await sandbox.kill();
}

const finalResponse = await anthropic.messages.create({
  model: "claude-sonnet-5",
  max_tokens: 1024,
  tools,
  tool_choice: { type: "auto", disable_parallel_tool_use: true },
  messages: [
    ...messages,
    { role: "assistant", content: response.content },
    {
      role: "user",
      content: [
        {
          type: "tool_result",
          tool_use_id: toolCall.id,
          content: result.stdout,
        },
      ],
    },
  ],
});
const finalText = finalResponse.content.find((block) => block.type === "text");

console.log(finalText.text);

Production considerations

Validate the tool name and arguments before execution. Do not interpolate model output into a shell command: write the code to a file first. A production application should also limit execution time and output size. Kill the sandbox in finally so it stops after errors as well.

If the model can request several tools in one response, execute each call and return all results in the next message.