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.runstops 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:df.to_csv("clean.csv").
Upload data and get a chart back
Send the user’s file in withfiles.write. Code that saves an image with matplotlib writes it to the sandbox’s disk, and files.read returns the bytes.
ls -t /home/user or ask the model to print the paths.
Clean up
Delete the sandbox when the conversation ends: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.Related guides
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.