Sub topic 8
Code execution: letting the model compute
This chapter assumes you can call the Responses API with an API key, read JSON response bodies, and already know the basic idea of tool calling. You should also know how to declare a built-in tool, which an earlier chapter covered.
Why models need to run code
A language model predicts text. It does not calculate. Ask it for the sum of the squares of 1 to 1000 and it may produce a number that looks right but is wrong. The code execution tool fixes this. The model writes Python, the provider runs it in a sandbox (an isolated environment with limited access), and the model reads the output before it answers.
This is a built-in tool, so the provider runs the whole loop. Your code makes one request and gets one response.
Enabling the tool
Add a code_interpreter tool to the tools list. The container field tells the provider which sandbox to use. Setting "type": "auto" makes the provider create a container for you.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "code_interpreter", "container": {"type": "auto"}}],
instructions="When a question involves arithmetic, write and run code to answer it.",
input="What is the sum of the squares of the integers from 1 to 1000?",
)
print(response.output_text)The instructions line matters. Without it, the model may decide it can answer from memory and skip the tool. The expected answer is 333833500.
What happens inside the request
If the code fails, the model can see the error, rewrite the code, and run it again. All of this happens before the response reaches you.
Inspecting what ran
The final text hides the work. To audit it, loop over response.output. Each tool use appears as an item of type code_interpreter_call, and the code the model wrote is in its code field.
for item in response.output:
if item.type == "code_interpreter_call":
print("status:", item.status)
print(item.code)Check this when an answer looks wrong. You can see whether the model ran code at all, and whether the code matches your question.
Containers and files
A container holds the sandbox's files and state. With "type": "auto", a new container is created for the request. Files the code writes, such as charts or CSV files, live in that container. The final message can reference them through annotations of type container_file_citation, and you can then download them with the container files endpoints. Containers expire after a short idle period, so download what you need soon.
Common errors
- The model skips the tool. Add instructions that name the situations where it must run code. A later chapter covers
tool_choicefor forcing this. - Trusting output without checking. The model can write buggy code. Read the
codefield for important results. - Expecting network or package access. The sandbox is restricted, so code that needs the internet or unusual libraries can fail.
- Costs. Containers are billed separately from tokens, and generated code and its output add tokens.
Exercise
A user reports that your code-execution app gave a wrong total. Which part of the response do you inspect first, and why?
Show answer
Inspect the code_interpreter_call items in response.output. Their code field shows what the model actually ran, so you can tell whether it skipped the tool, wrote faulty code, or misread the output.