Skip to content

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:

  1. Upload the dataset to the sandbox.
  2. Tell the model where the file is and which columns it contains.
  3. Give the model a tool that accepts Python code.
  4. Run the tool call with runCode or run_code.
  5. 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-interpreter

Set OPENAI_API_KEY and AGENTBOX_API_KEY before running the example. It uses the Responses API with gpt-5.6-luna.

mjs
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-interpreter

Set ANTHROPIC_API_KEY and AGENTBOX_API_KEY. This version uses the Messages API with claude-sonnet-5.

mjs
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.