You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Code Interpreter tool enables models to write and execute Python code in a secure, sandboxed environment. It supports a range of advanced tasks, including:
569
+
570
+
* Processing files with varied data formats and structures
571
+
* Generating files that include data and visualizations (for example, graphs)
572
+
* Iteratively writing and running code to solve problems—models can debug and retry code until successful
573
+
* Enhancing visual reasoning in supported models (for example, o3, o4-mini) by enabling image transformations such as cropping, zooming, and rotation
574
+
* This tool is especially useful for scenarios involving data analysis, mathematical computation, and code generation.
instructions ="You are a personal math tutor. When asked a math question, write and run code using the python tool to answer the question."
605
+
606
+
response = client.responses.create(
607
+
model="gpt-4.1",
608
+
tools=[
609
+
{
610
+
"type": "code_interpreter",
611
+
"container": {"type": "auto"}
612
+
}
613
+
],
614
+
instructions=instructions,
615
+
input="I need to solve the equation 3x + 11 = 14. Can you help me?",
616
+
)
617
+
618
+
print(response.output)
619
+
```
620
+
621
+
### Containers
622
+
623
+
> [!IMPORTANT]
624
+
> Code Interpreter has [additional charges](https://azure.microsoft.com/pricing/details/cognitive-services/openai-service/) beyond the token based fees for Azure OpenAI usage. If your Responses API calls Code Interpreter simultaneously in two different threads, two code interpreter sessions are created. Each session is active by default for 1 hour with an idle timeout of 30 minutes.
625
+
626
+
The Code Interpreter tool requires a container—a fully sandboxed virtual machine where the model can execute Python code. Containers can include uploaded files or files generated during execution.
627
+
628
+
To create a container, specify `"container": { "type": "auto", "files": ["file-1", "file-2"] }` in the tool configuration when creating a new Response object. This automatically creates a new container or reuses an active one from a previous code_interpreter_call in the model’s context. The `code_interpreter_call` in the output of the APIwill contain the `container_id` that was generated. This container expires if it is not used for 20 minutes.
629
+
630
+
### File inputs and outputs
631
+
632
+
When running Code Interpreter, the model can create its own files. For example, if you ask it to construct a plot, or create a CSV, it creates these images directly on your container. It will cite these files in the annotations of its next message.
633
+
634
+
Any files in the model input get automatically uploaded to the container. You do not have to explicitly upload it to the container.
Background mode allows you to run long-running tasks asynchronously using models like o3 and o1-pro. This is especially useful forcomplex reasoning tasks that may take several minutes to complete, such as those handled by agents like Codex or Deep Research.
1170
+
Background mode allows you to run long-running tasks asynchronously using models like o3 and o1-pro. This is especially useful forcomplex reasoning tasks that can take several minutes to complete, such as those handled by agents like Codex or Deep Research.
1065
1171
1066
1172
By enabling background mode, you can avoid timeouts and maintain reliability during extended operations. When a request is sent with`"background": true`, the task is processed asynchronously, and you can poll for its status over time.
0 commit comments