Skip to content
Last updated

Code interpreter allows your AI assistant to execute Python code in a sandboxed environment. The model can write and run code to perform calculations, analyze data, generate visualizations, and solve problems that require computation.

Enabling Code Interpreter

{
 "organization_id": "org_your_org_id",
 "assistant_id": "asst_abc123",
 "tools": [{"type": "code_interpreter"}],
 "inputs": [{"role": "user", "content": "What is the compound interest on $10,000 at 7% for 20 years?"}]
}

The model will write and execute Python code to calculate the answer, then present the result.

Tool Modes

ModeBehavior
autoModel uses code when computation is needed (recommended)
onAlways run code before responding
offDisable code interpreter

Response Structure

When code is executed, the response includes a code_interpreter_call output item:

{
 "output": [
 {
 "type": "code_interpreter_call",
 "id": "ci_abc123",
 "status": "completed",
 "code": "principal = 10000\nrate = 0.07\nyears = 20\nresult = principal * (1 + rate) ** years\nprint(f'${result:,.2f}')",
 "outputs": [
 {"type": "text", "text": "$38,696.84"}
 ]
 },
 {
 "type": "message",
 "role": "assistant",
 "content": [{"type": "output_text", "text": "$10,000 at 7% for 20 years grows to $38,696.84."}]
 }
 ]
}

Capabilities

The code interpreter can:

  • Mathematical computations — Arithmetic, statistics, financial calculations
  • Data analysis — Process CSV/JSON data, compute aggregates, find patterns
  • String manipulation — Parse, format, transform text
  • Algorithm execution — Sort, search, graph algorithms
  • File processing — Read and analyze uploaded files (with file retrieval enabled)

Limitations

  • Execution environment is sandboxed (no network access, no file system writes)
  • Maximum execution time: 30 seconds per code block
  • No external package installation (standard library and common scientific packages are available)
  • Currently in development — availability may vary by plan

Use Cases

  • "Calculate the ROI on these sales figures"
  • "Parse this JSON and tell me the average value"
  • "Sort this list and find duplicates"
  • "Convert these temperatures from Fahrenheit to Celsius"