Skip to main content
The Code Execution Tool allows models to write and execute code in a sandboxed environment. This is useful for mathematical calculations, data analysis, code generation, and other computational tasks. Supported in OpenAI, Anthropic, and Gemini models. The container parameter is available only for OpenAI, and ignored for others.

Basic Usage

To enable code execution, include the CodeExecutionTool in your request’s Tools array:

Container Configuration (OpenAI Only)

For OpenAI models, you can configure the execution container with memory limits and file attachments:
Alternatively, you can reuse an existing container by providing its ID:
Note: The container parameter is only supported by OpenAI models. For Anthropic and Gemini, this parameter is ignored.

Code Execution Responses

When the model executes code, it returns a CodeInterpreterCallMessage in the response output. This message contains the executed code and its outputs.

Streaming Code Execution

When streaming, code execution progress is reported through different chunk types:
  • response.code_interpreter_call.in_progress: Code execution has started
  • response.code_interpreter_call_code.delta: Incremental code updates as the model writes code
  • response.code_interpreter_call_code.done: Code writing is complete
  • response.code_interpreter_call.interpreting: Code is being executed
  • response.code_interpreter_call.completed: Code execution is complete

Code Interpreter Call Message Structure

The CodeInterpreterCallMessage contains the following fields:

CodeInterpreterCallOutputParam

Each output in the Outputs array has the following structure:

Including Outputs

To receive code execution outputs in the response, include IncludableCodeInterpreterCallOutputs in the Parameters.Include array:
Without this parameter, the Outputs field may be empty in the response.