Sub topic 17
Choosing and combining tools
This chapter assumes you can call a responses-style API, read its JSON output, and have seen function calling. It also assumes you know the three built-in tools: web search, code execution, and file search.
Start from the data
Pick the tool by where the answer lives.
| Question needs... | Use | Why |
|---|---|---|
| Current public information | Web search | The model's training data is out of date. |
| Exact arithmetic, data analysis, or file generation | Code execution | Running code is more reliable than predicting numbers. |
| Your own documents | File search | Retrieval from a vector store grounds answers in your content. |
| Private systems, writes, or side effects | Function tool | Your code runs it, so you control credentials and validation. |
Built-in tools cost less to build because the provider runs the loop. They cost more in control. If the action must be audited, authorized per user, or change state, write a function tool.
Combining tools
You can declare several tools in one request. The model decides which to call and in what order, and the provider runs the built-in ones. A common pairing is file search to fetch figures, then code execution to compute with them.
import os
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "file_search",
"vector_store_ids": [os.environ["VECTOR_STORE_ID"]],
"max_num_results": 3,
},
{"type": "code_interpreter", "container": {"type": "auto"}},
],
instructions=(
"Find figures in the documents first. "
"Use code for any calculation. Do not search the web."
),
input="What was the average quarterly revenue in the 2024 report?",
)
for item in response.output:
print(item.type)
print(response.output_text)The printed item types show which tools ran and in what order. Check them whenever the answer looks wrong.
Rules of thumb
- Declare only the tools the task needs. Each extra tool adds tokens to the request and more ways to go wrong.
- Use
instructionsto set the order and the limits, as in the example above. If a tool must run, settool_choiceaccordingly. Otherwise leave it onauto. - Treat web and document content as untrusted when it sits beside a function tool that can act. A web page could try to steer the model toward a call that changes data. Keep write-capable function tools out of requests that also browse, or require confirmation in your own code.
- Cap cost with limits such as
max_num_resultsand, for Anthropic web search,max_uses. - Provider support differs, so check that each tool exists on the provider and model you target. Wrap tool setup in one function so you can swap it.
Common mistakes
- Using web search for facts that sit in your own documents. The answer will cite public pages instead of your policy.
- Asking the model to calculate without code execution. The result may look right and be wrong.
- Putting every tool in every request "just in case".
- Skipping the output items, then being unable to explain where an answer came from.
Exercise
A support bot must answer "What is our refund window?" from the company handbook, then email the customer a confirmation. Which tools do you declare, and why is each one built-in or a function tool?
Show answer
Declare file_search over the handbook's vector store, because the answer lives in your documents. Declare a function tool for sending email, because it has side effects and your code should authorize and run it. Web search and code execution are unnecessary here. Because the email tool can act, avoid adding web search to the same request, so untrusted pages cannot steer it.