Sub topic 2
How the provider runs the tool loop for you
This chapter assumes you can call a chat or responses API with an API key, read JSON responses, and have a basic idea of function calling. The previous chapter covered what built-in tools are. This one covers what happens between your request and the response.
The loop you normally write
With a function tool, your code runs the tool. The model returns a tool call, you execute it, you send the result back, and you call the API again. You repeat this until the model returns a final answer. Each round trip is a separate API request that your code controls.
The loop the provider writes
With a built-in tool, the provider runs that loop inside one request. The model decides to search, run code, or retrieve documents. The provider executes the tool on its own infrastructure and feeds the result back to the model. The model may do this several times before it writes the final answer. Your code makes one call and gets one response.
What you see from your side
The code looks like an ordinary request. The only difference is the tool entry.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "web_search"}],
input="What are the most recent stable releases of Python?",
)
print([item.type for item in response.output])
print(response.output_text)The first line prints the item types in the output. A typical result is ['web_search_call', 'message']. The search call is a record of work the provider already did. You do not answer it. The next chapter shows how to read these items.
Function tools compared with built-in tools
| Question | Function tool | Built-in tool |
|---|---|---|
| Who runs the tool? | Your code | The provider |
| API requests per answer | Two or more | One |
| Who writes the loop? | You | The provider |
| What do you control? | Everything the tool does | Only the options the provider exposes |
| Where does the data go? | Stays in your system | Passes through the provider |
Common errors
- Waiting for a tool call to answer. Hosted tool calls arrive already completed. If your code expects to send a result back, it will wait for nothing.
- Assuming one call means one search. The model can use a tool several times in one request. Limits such as
max_useson Anthropic's web search cap this. - Ignoring the cost. Every internal round adds tokens and time to your single request, so one call can be slower and costlier than it looks.
- Expecting to intervene. You cannot approve or change a hosted tool call in the middle of the loop. Control comes from instructions, tool options, and tool choice, which later chapters cover.
Exercise
A function tool needs two API requests to produce an answer. A built-in web search tool needs one. Where does the extra work of the built-in tool go, and what do you lose?
Show answer
The provider performs the tool call and the follow-up model turn inside your single request. You lose the ability to inspect, approve, or change a tool call before it runs. You can only audit it afterward through the output items.