Подключение LLM
LLM может написать код, а AgentBox — безопасно выполнить его в отдельной песочнице. Приложение описывает инструмент execute_python, передаёт его модели, выполняет полученный код и возвращает результат модели.
OpenAI
Установите SDK OpenAI и AgentBox:
npm install openai @abox-dev/sdkЗадайте OPENAI_API_KEY и AGENTBOX_API_KEY. Пример использует Responses API и модель gpt-5.6-luna.
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
Установите SDK Anthropic и AgentBox:
npm install @anthropic-ai/sdk @abox-dev/sdkЗадайте ANTHROPIC_API_KEY и AGENTBOX_API_KEY. Пример использует Messages API и модель claude-sonnet-5.
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);Что учесть
Проверяйте имя инструмента и аргументы до выполнения. Не подставляйте код модели в shell-команду: сначала запишите его в файл. Для рабочего приложения также ограничьте время выполнения и размер вывода. Песочницу удаляйте в finally, чтобы она остановилась и после ошибки.
Если модель может вызвать несколько инструментов за один ответ, обработайте каждый вызов и верните все результаты одним следующим сообщением.