Analyze data with an LLM
An LLM can turn a question about a dataset into Python code. Run that code in a sandbox instead of the application process, then use the structured execution result to show tables or charts to the user.
The basic flow is:
- Upload the dataset to the sandbox.
- Tell the model where the file is and which columns it contains.
- Give the model a tool that accepts Python code.
- Run the tool call with
runCodeorrun_code. - Read text, tables, charts, or errors from the execution result.
The examples below use a small CSV file and save the generated chart as sales.png.
OpenAI
npm install openai @abox-dev/code-interpreterSet OPENAI_API_KEY and AGENTBOX_API_KEY before running the example. It uses the Responses API with gpt-5.6-luna.
const openai = new OpenAI();
const sandbox = await Sandbox.create("code-interpreter");
try {
await sandbox.files.write(
"/tmp/sales.csv",
`month,revenue
January,120
February,165
March,210
April,195
`,
);
const response = await openai.responses.create({
model: "gpt-5.6-luna",
input:
"The file /tmp/sales.csv has month and revenue columns. Create a bar chart of revenue by month and finish with plt.show().",
tools: [
{
type: "function",
name: "run_python",
description: "Run Python code in an AgentBox sandbox",
parameters: {
type: "object",
properties: { code: { type: "string" } },
required: ["code"],
additionalProperties: false,
},
strict: true,
},
],
});
const toolCall = response.output.find(
(item) => item.type === "function_call" && item.name === "run_python",
);
const { code } = JSON.parse(toolCall.arguments);
const execution = await sandbox.runCode(code);
const chart = execution.results.find((result) => result.png);
await writeFile("sales.png", Buffer.from(chart.png, "base64"));
console.log("Saved sales.png");
} finally {
await sandbox.kill();
}Anthropic
npm install @anthropic-ai/sdk @abox-dev/code-interpreterSet ANTHROPIC_API_KEY and AGENTBOX_API_KEY. This version uses the Messages API with claude-sonnet-5.
const anthropic = new Anthropic();
const sandbox = await Sandbox.create("code-interpreter");
try {
await sandbox.files.write(
"/tmp/sales.csv",
`month,revenue
January,120
February,165
March,210
April,195
`,
);
const response = await anthropic.messages.create({
model: "claude-sonnet-5",
max_tokens: 1024,
messages: [
{
role: "user",
content:
"The file /tmp/sales.csv has month and revenue columns. Create a bar chart of revenue by month and finish with plt.show().",
},
],
tools: [
{
name: "run_python",
description: "Run Python code in an AgentBox sandbox",
input_schema: {
type: "object",
properties: { code: { type: "string" } },
required: ["code"],
},
},
],
});
const toolCall = response.content.find(
(block) => block.type === "tool_use" && block.name === "run_python",
);
const execution = await sandbox.runCode(toolCall.input.code);
const chart = execution.results.find((result) => result.png);
await writeFile("sales.png", Buffer.from(chart.png, "base64"));
console.log("Saved sales.png");
} finally {
await sandbox.kill();
}Working with generated code
Treat model-generated code as untrusted input even though it runs in a sandbox. Limit execution time and output size, restrict which files the code can access, and show execution errors to the model only when a retry is useful. Always stop the sandbox after the analysis finishes.
For result formats, see charts and pre-installed libraries.