Sub topic 17
Choosing and combining tools
This chapter assumes you can call a responses or messages API with an API key and read JSON output. It also assumes you know the basic idea of function calling. By now you have seen each built-in tool on its own. The practical question is which one to use for a given task, and what happens when you enable several at once.
Start from the question, not the tool
Each tool answers a different kind of question. Pick by asking where the needed information or computation lives.
| Need | Best fit | Why |
|---|---|---|
| Facts that change over time, such as releases, news, prices | Web search | The model's training data is out of date. |
| Exact arithmetic, data analysis, charts, file conversion | Code execution | The model writes and runs code instead of guessing numbers. |
| Answers from your own documents | File search | Your documents are indexed in a vector store and retrieved by meaning. |
| Data in your database, internal APIs, or actions with side effects | Function tool | Only your code can reach your systems. |
| Stable general knowledge | No tool | A tool adds cost and latency for no gain. |
A useful rule: if the model could answer correctly from memory, do not give it a tool. If a wrong answer would be costly and the answer can be checked, give it the tool that provides the evidence.
Decision criteria beyond "what can it do"
Capability is only the first filter. Compare candidate tools on these dimensions:
- Freshness. Web search is current. File search is only as fresh as your last indexing run. Code execution has no knowledge of its own, only the data you pass in.
- Determinism. Code execution gives reproducible results for the same code and input. Web search and retrieval vary between calls.
- Control. A function tool runs in your environment, so you decide authentication, logging, and validation. A built-in tool runs on the provider's side, so you control it only through request parameters.
- Exposure. Web search brings untrusted text into the context. File search can expose any document in the store. Code execution runs in a sandbox, and files you upload to it are visible to model-written code.
- Cost and latency. Each search, retrieval, or code run adds time and tokens. Retrieved chunks and search results count as input tokens.
- Portability. Tool names and parameters differ between providers. A function tool you define yourself is the most portable option.
Combining tools in one request
You can list several tools in the same request. The model decides in each step which one to call, and the provider runs the loop until it produces a final answer. A common pattern is to retrieve, then compute.
from openai import OpenAI
client = OpenAI()
vector_store = client.vector_stores.create(name="company-handbook")
with open("handbook.pdf", "rb") as f:
client.vector_stores.files.upload_and_poll(
vector_store_id=vector_store.id,
file=f,
)
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "file_search",
"vector_store_ids": [vector_store.id],
"max_num_results": 3,
},
{"type": "code_interpreter", "container": {"type": "auto"}},
],
instructions=(
"Use file_search for policy details from the handbook. "
"Use code for any arithmetic. Do not do arithmetic in your head."
),
input=(
"A new employee gets the vacation allowance in our handbook. "
"How many days is that over 3 years, if unused days never carry over?"
),
)
print(response.output_text)The instructions matter here. Without them the model may skip a tool it could have used, or use the wrong one. Naming each tool and the job it owns makes the behavior more predictable.
Seeing what happened
After a combined request, inspect the output items to confirm which tools ran and in what order. This is how you audit and debug a multi-tool answer.
for item in response.output:
print(item.type)You should see items such as file_search_call, code_interpreter_call, and finally a message. If a tool you expected is missing from the list, the model decided it was not needed, and your instructions or tool choice setting is the place to adjust.
Mixing built-in and function tools
Built-in tools and function tools can share a request, but they behave differently in the loop. The provider runs built-in tools itself and keeps going. A function call stops the response and hands control back to you. Your code runs the function, sends back the result, and the model continues.
The diagram below shows the flow when both kinds are present.
Design consequences:
- A request with a function tool may take more than one round trip, while a request with only built-in tools finishes in one.
- Anything that touches private systems or has side effects belongs in a function tool, where you can check permissions before acting.
- Do not send the full result of a sensitive function call into a request that also has web search enabled unless you accept that the model could include it in a search query.
Failure modes when combining tools
- Wrong tool chosen. The model searches the web for a question your handbook already answers. Fix it with clearer instructions, or restrict the tools for that request.
- Tool loops and runaway cost. Several searches and code runs in one request multiply tokens and time. Use limits such as
max_usesfor Anthropic web search, andmax_num_resultsfor file search. - Mixed trust levels. Text from web search is untrusted, and it can contain instructions aimed at the model. If the same request also has a function tool that can send email or change records, an injected instruction could trigger it. Keep side-effecting tools out of requests that read the open web, or require confirmation in your own code before executing them.
- Stale index. File search answers from the last indexed version of a document. If a policy changed, re-upload the file or the answer will be wrong with full confidence.
- Unverified numbers. If the model states a figure without a
code_interpreter_callitem, it did not compute it. Check the output items when accuracy matters.
Steering with tool choice
When you know which tool a task needs, say so. Instructions guide the model. A tool choice setting can require a tool call, or disable tools. Use these deliberately:
- Allow automatic choice for open-ended assistants.
- Require a tool for pipelines where skipping it would produce an unsupported answer, such as a compliance question that must cite the handbook.
- Send separate requests with different tool lists when tasks are clearly different. A narrow tool list is cheaper, faster, and easier to reason about than one request with everything enabled.
A short selection procedure
- Can the model answer reliably without help? Use no tool.
- Is the data private and in a system you control? Use a function tool, or file search if it is documents.
- Is the data public and time-sensitive? Use web search.
- Is the result a calculation, transformation, or file you can generate with code? Use code execution.
- Does the task need more than one of these? Combine them, write instructions that assign each tool a job, and inspect the output items afterward.
- Does any step read untrusted content while another can cause side effects? Separate them or add a confirmation step.
Exercise
A support bot must answer questions about your refund policy (stored in PDFs), report the live status of an order (in your own database), and calculate prorated refund amounts. Which tools do you enable, and why?
Show answer
Use file_search over a vector store containing the policy PDFs, a function tool that queries your order database (only your code can reach it, and you control authentication), and code execution for the prorated arithmetic so the number is computed rather than guessed. Web search is not needed because none of the data is public or time-sensitive. Add instructions assigning each tool its job, and inspect the output items to confirm the calculation went through code.
Exercise
Your agent has web search and a function tool that sends emails. Why is this combination risky, and how can you reduce the risk?
Show answer
Web pages are untrusted and can contain text that tries to instruct the model, so a page could push the model to call the email function with attacker-chosen content. Reduce the risk by keeping the email tool out of requests that use web search, by splitting the work into separate requests, or by requiring your own code to confirm or validate the email before it is sent.