Sub topic 13
Steering when the model uses a tool
This chapter assumes you can call a Responses-style or Messages-style API and that you have already declared a built-in tool in a request. Here you learn how to influence whether the model uses that tool, which one it picks, and how often it uses it. You also learn how to check that it did what you asked.
Why the model sometimes ignores a tool
When you list a built-in tool, you only make it available. The model decides on each request whether to call it. That decision comes from the user's wording, your instructions, and the tool's description.
Two failures are common:
- Under-use. The model answers from memory when you wanted fresh data. Asked for the sum of squares up to 1000, it may guess instead of running code.
- Over-use. The model searches the web for a question it could answer directly, which adds latency and cost.
You have two levers. Instructions guide the decision in natural language. A tool choice setting constrains it in the API.
Lever 1: instructions
Instructions go in the system-level field. In the Responses API that is instructions. In the Messages API it is system. Be specific about the trigger condition and the tool name.
Weak instruction:
Use your tools when helpful.Stronger instruction:
For any question about events, prices, or software versions after your
training data, use web search. For any arithmetic with more than two
operations, write and run code. If the answer is in the handbook, use
file search first and do not search the web.The stronger version names a condition for each tool and sets a priority order. The priority order matters when you declare several tools, because the model otherwise picks by its own judgment.
Instructions are guidance, not a guarantee. The model can still skip a tool. If skipping is unacceptable, use tool choice.
Lever 2: tool choice
Tool choice is a request parameter that restricts the model's options. Both major APIs offer the same four ideas, with different spellings.
| Behavior | OpenAI Responses API | Anthropic Messages API |
|---|---|---|
| Model decides | `"auto"` (default) | `{"type": "auto"}` (default) |
| Must call some tool | `"required"` | `{"type": "any"}` |
| Must call one named tool | `{"type": "file_search"}` or a function by name | `{"type": "tool", "name": "web_search"}` |
| No tools this turn | `"none"` | `{"type": "none"}` |
Forcing a built-in tool in OpenAI
This request forces file search, so the model must retrieve from your documents before answering:
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "file_search",
"vector_store_ids": ["vs_REPLACE_WITH_YOUR_ID"],
"max_num_results": 3,
}
],
tool_choice={"type": "file_search"},
input="How many vacation days do new employees get?",
)
print(response.output_text)Replace the vector store ID with one you created earlier. Forcing is useful for retrieval over private documents, where you never want an answer from the model's memory.
Forcing a tool in Anthropic
import anthropic
client = anthropic.Anthropic()
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=[
{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 2,
}
],
tool_choice={"type": "tool", "name": "web_search"},
messages=[
{"role": "user", "content": "What is the latest stable release of Python?"}
],
)
for block in message.content:
if block.type == "text":
print(block.text)Here max_uses caps searches at two, and tool_choice makes sure at least one happens.
Edge cases with forcing
- Forcing applies to the first decision. The model can still call the tool several times in one turn unless you cap it.
- Forced calls skip the lead-in text. With a forced tool, expect little or no explanation before the call. Rely on the final answer for text.
- Naming a tool you did not declare is an error. The named tool must appear in
tools. - A forced tool does not guarantee a good query. The model still writes the search query or code. Garbage input gives garbage output, only more confidently.
- Tool-type names change. Hosted tool types are versioned or renamed over time, for example
web_search_20250305on Anthropic. Check the current provider documentation before copying a name.
Capping how much the model uses a tool
Choosing whether to call a tool is one decision. Choosing how many calls to allow is another.
- OpenAI's Responses API accepts
max_tool_calls, which caps the total number of built-in tool calls in a request. - Anthropic's web search tool takes
max_usesin the tool definition. - File search has
max_num_results, which limits retrieved chunks per call, not the number of calls.
Caps are your guard against runaway loops. Because the provider runs the loop for you, you cannot step in between calls. A cap is the only brake you have.
Verifying what the model did
Never assume steering worked. Inspect the output items, as you did in the earlier chapter on reading tool-call items. This helper counts calls by type:
from collections import Counter
def tool_usage(response):
counts = Counter()
for item in response.output:
if item.type != "message":
counts[item.type] += 1
return counts
print(tool_usage(response))Typical item types you will see are web_search_call, file_search_call, and code_interpreter_call. An empty counter means the model answered without any tool. In a test suite, assert on this counter when a test case requires a tool call.
A decision flow
The diagram shows how to pick a steering approach.
Common mistakes
- Using
"required"with many tools. The model must call something, but not necessarily the right one. Name the tool when only one is correct. - Forcing a tool on every turn of a conversation. A follow-up like "thanks" would trigger a pointless search. Force only the turns that need it, or send
autofor later turns. - Contradictory instructions. Telling the model to "always search" while also saying "avoid unnecessary searches" gives unpredictable behavior. Write one rule per tool.
- Relying on instructions for security. An instruction such as "never browse unknown sites" is not a control. Use domain filters and
tool_choice: nonewhere it matters. - Skipping verification. Without checking output items, a model that never calls the tool looks identical to one that does, until a stale answer reaches a user.
Exercise
Your support bot must answer only from your handbook, using file search, and must never answer from model memory. Which steering settings would you use, and how would you verify them in a test?
Show answer
Declare the filesearch tool with your vector store and set toolchoice to the named filesearch tool, so retrieval always happens. Add instructions saying to answer only from retrieved passages and to say so when nothing relevant is found. Optionally cap results with maxnumresults. In a test, check that the response output contains a filesearchcall item for every question, for example by asserting that toolusage(response)["filesearchcall"] is at least 1.
Exercise
A request declares both websearch and codeinterpreter with tool_choice "auto". The user asks for the sum of squares from 1 to 1000, but the model answers without running code. What are two fixes?
Show answer
First, add an instruction that names the trigger, such as "for arithmetic with more than two operations, write and run code". Second, set toolchoice to the codeinterpreter tool for this request. Then confirm with the output items that a codeinterpretercall appears.