Code Interpreter
Enable LLMs to execute Python code in a secure sandbox.
Code Interpreter is a built-in tool that allows LLMs to execute Python code in a secure sandboxed environment. When enabled, the model can write and run code for computations, data analysis, plotting, file processing, and more.
Pricing
Code Interpreter is currently free to use. Each execution runs in an isolated sandbox with no additional cost beyond token usage.
How it works
Include { type: 'code_interpreter' } in your tools array to give the model
the ability to execute Python code. The model will autonomously decide when
to write and run code to answer the user's question.
import OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://api.aivene.com/v1',
apiKey: process.env.AIVENE_API_KEY
});
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'user', content: 'Calculate the first 20 Fibonacci numbers' }
],
tools: [
{ type: 'code_interpreter' }
]
});
console.log(response.choices[0].message.content);Configuration options
typestringrequiredMust be "code_interpreter".
timeout_msnumberoptionalMaximum execution time for the code in milliseconds. The execution will be terminated if it exceeds this limit.
sandbox_timeout_msnumberoptionalMaximum lifetime of the sandbox environment in milliseconds.
Example with configuration
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'user', content: 'Generate a sine wave plot' }
],
tools: [
{
type: 'code_interpreter',
timeout_ms: 30000,
sandbox_timeout_ms: 60000
}
]
});Sandbox environment
Each code execution runs in a fresh, isolated sandbox:
- Ephemeral — No state is shared between calls
- Secure — Isolated from other executions and the host system
- Python environment — Common libraries like NumPy, Pandas, Matplotlib are available
- Output capture — stdout, stderr, and results (including charts/images) are returned
Use cases
- Mathematical computations — Complex calculations, statistics, simulations
- Data analysis — Process and analyze datasets
- Visualization — Generate charts, plots, and graphs
- File processing — Parse, transform, or generate files
- Algorithm implementation — Run custom algorithms on demand
Response format
The code execution returns a JSON object with:
| Field | Description |
|---|---|
stdout | Standard output from the code |
stderr | Standard error output |
results | Array of results including charts/images |
error | Error object if execution failed |
Example: Data visualization
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{
role: 'user',
content: 'Create a bar chart showing the population of the top 5 most populous countries'
}
],
tools: [
{ type: 'code_interpreter' }
]
});
// The model will generate and execute Python code using matplotlib
// The chart image will be included in the responseCombining with other tools
Code Interpreter works well alongside Web Search for data-driven tasks:
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'user', content: 'Find the current stock prices of FAANG companies and create a comparison chart' }
],
tools: [
{ type: 'web_search' },
{ type: 'code_interpreter' }
]
});The model will search for current stock prices, then use Code Interpreter to create a visualization.