Skip to main content
A code interpreter lets a model answer with code instead of a guess: it writes Python, your app runs it, and the model reads the output. Run that code in a sandbox, not in your app’s process. This guide uses the superserve/code-interpreter template, which has pandas, numpy, matplotlib, seaborn, plotly, scikit-learn, and openpyxl installed.

Why use a sandbox

  • Isolation: each sandbox is its own microVM. Code that deletes files, starts processes, or fills the disk can’t touch your app or other users’ sandboxes.
  • No credentials inside: your API keys stay in your app. Only the code and the files you send go into the sandbox.
  • Network control: a network rule can block all outbound traffic, so code can’t send data out.
  • Files persist: one sandbox per conversation keeps uploaded data and results between runs, and you can pause it between turns.

How it works

1

Create a sandbox

Create one sandbox per conversation or user, from superserve/code-interpreter.
2

Send the model's code

Write the code to a file with files.write, then run it with commands.run and a timeout.
3

Return the result to the model

Give the model the exit code, stdout, and stderr. Read files that the code wrote, such as charts, with files.read.
4

Delete the sandbox

Delete the sandbox when the conversation ends, or let auto-delete remove it.

Run code and read the output

This example blocks all outbound traffic and runs one piece of code. Commands in this template run as root in /home/user.
  • Write the code to a file instead of passing it to python -c. A file needs no shell quoting, and it has no limit on the length of one command argument.
  • An exception in the code gives a non-zero exit code and a traceback in stderr. Return both to the model, so it can fix the code and try again.
  • A run that takes longer than the timeout stops with exit code 124. Without a timeout, commands.run stops a command after 30 seconds. See Timeouts.

Give the model a tool

Most model APIs take tools as a name, a description, and a JSON schema. Wrap the steps above in one function, and call it when the model asks for the tool:
Each run starts a new Python process, so variables don’t carry over to the next run. Files do. Tell the model to save data that it needs later to disk, for example with df.to_csv("clean.csv").

Upload data and get a chart back

Send the user’s file in with files.write. Code that saves an image with matplotlib writes it to the sandbox’s disk, and files.read returns the bytes.
To send the image to a model, base64-encode the bytes and attach them as an image in the next message. To find the files that a run made, run ls -t /home/user or ask the model to print the paths.

Clean up

Delete the sandbox when the conversation ends:
If your app can stop before it deletes the sandbox, set idleTimeoutSeconds / idle_timeout_seconds and autoDeleteSeconds / auto_delete_seconds on create. The sandbox then pauses when it has no activity and is deleted after it stays paused. See Lifecycle.

Integrations

To give the code interpreter to an agent that you build with a framework, see OpenAI Agents SDK, LangChain, Vercel AI SDK, or the other agent frameworks.

Run commands

Exit codes, working directory, env vars, and timeouts.

Read and write files

Upload and download files and folders.

Network rules

Allow or block outbound traffic.

Evaluations and RL

Run and score model outputs at scale.