Skip to main content
Outcome: a data in -> model writes analysis -> isolated execution -> answer and chart out pipeline. Level: intermediate · Time: ~15 minutes · Pattern: the box is a tool the model calls.

When to use this

You want a conversational data assistant: a user drops in a CSV, asks “what is revenue by region, and which product sells best?”, and gets a number and a chart. The tension is structural. To produce a real answer you must actually run code, and no one reviews the pandas script the model wrote. It might read other files on your machine, install something unexpected, or exhaust memory inside your process. CodeBox turns that into a closed loop: copy the data in, let the model write the code, run it in the sandbox, copy the results out. Both the data copy and the code live inside a disposable microVM. Your process only orchestrates. Fits: conversational analytics, automated reporting, a natural-language front end over a data catalog, and batch processing of user-uploaded datasets.

Architecture

Three things are worth noticing:
  • Only a schema hint goes to the model. You send the header and a few sample rows, not the dataset. That saves tokens and keeps sensitive records out of the prompt.
  • Data enters explicitly. The sandbox sees exactly what you copy_in and nothing else.
  • Artifacts come back explicitly. Text results ride out on the return value of run(); files come back through copy_out().

Prerequisites

  • BoxLite installed and a working virtualization host — see Installation.
  • An OpenAI-compatible LLM endpoint (OPENAI_API_KEY, plus OPENAI_BASE_URL for other providers).
  • Give the box a bigger disk. pandas and numpy do not fit in the default CodeBox disk. Pass disk_size_gb=4 — see Trust and limits for what happens if you forget.

Build it

Step 1: data in, analysis out

Step 2: render a chart and bring the PNG back

The other half of analysis is a picture. Let the sandbox render it with matplotlib and pull the file out — the rendering happens inside the isolated environment too.
Parameter tables for copy_in / copy_out: Moving files without a mount. For run / install_packages / exec: Run Python code in a box.

Run it

Check the arithmetic against the sample data — North is 2400 + 2000 + 3000 + 2750 = 10150 — and note that the sum was computed inside the microVM, not by your process.

Trust and limits

  • What the boundary covers. The data copy and the generated code share one microVM with its own kernel, filesystem, and disk quota. The sandbox sees only what you copied in; open("/etc/passwd") in the generated code reads the sandbox’s file, not yours. Nothing survives the scope exit.
  • Disk is a real constraint. The default CodeBox disk cannot hold pandas and numpy. Without disk_size_gb=4 the install fails with OSError: [Errno 28] No space left on device — and because run() returns stdout only, you see an empty string rather than the error. This is the most common way this guide goes wrong.
  • run() hides errors. A wrong column name gives you an empty string, not a traceback. Debug with exec("python3", "-c", code) and read stderr.
  • Data movement is explicit, network access is not. Nothing leaves the box unless you copy_out it — but the generated code can still open its own outbound connections. If that matters, add an egress allowlist: Run untrusted tools safely.

Troubleshooting

Next steps

  • Multiple tables. copy_in several CSVs under /data/, list every header in the schema hint, and let the model write the join.
  • Export instead of plot. Write .csv / .xlsx / .json in the box and copy_out it the same way.
  • Start simplerBuild a code interpreter is the same loop without the data transfer.
  • Serve it over HTTPPreview a sandboxed web app.