Code execution runs generated source code through a code interpreter and returns the result to a language model. As a special case of tool use, it lets models obtain precise answers to complex logic and mathematics problems rather than relying only on model weights. section 13.0 section 13.2
A typical execution loop interleaves generation with external computation:
- The model generates tokens until it emits a code-execution request. section 13.2
- An orchestrator runs the requested code and appends the output to the conversation. section 13.0 section 13.2
- The model incorporates the returned value and continues generating, potentially making multiple tool calls. section 13.2
Computing Fibonacci numbers
For “What is the 50th Fibonacci number?”, the model generates a short Python loop, executes fib(50), receives 12586269025, and uses that output in its response. section 13.2
messages = [...]
while True:
response = model(messages, tools=tools)
if not response.tool_calls:
return response.text
for call in response.tool_calls:
result = execute_tool(call.name, call.args)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": result,
})
This orchestration sketch shows how model messages, tool calls, execution results, and continued generation fit together. section 13.2


