Sub topic 10
Controlling when and which tools are used
This chapter assumes you can write the basic tool-calling loop from earlier chapters. You should know what a tool definition is and how results go back to the model tagged with a call id. Here you will learn how to steer the model's decision to call a tool.
Why the model's default is not always enough
By default the model decides for itself whether to call a tool. It reads the user message and the tool descriptions, then either answers in text or proposes one or more calls. That default is usually right. It fails in a few predictable ways:
- The model answers from memory when you needed fresh data from a tool.
- The model calls a tool when you wanted plain conversation.
- You need structured output from a specific tool and cannot accept free text.
- A step in your workflow must use one particular tool, such as a lookup before a write.
Providers expose a request setting for this, usually called tool_choice. It never makes the model execute anything. It only constrains what the model is allowed to propose. Your code still runs the call.
The four modes
Almost every provider supports the same four behaviors, though the names differ.
| Behavior | Meaning | OpenAI-style value | Anthropic-style value | Gemini-style value |
|---|---|---|---|---|
| Automatic | Model may answer in text or call any tool | `"auto"` | `{"type": "auto"}` | mode `AUTO` |
| Disabled | Model must answer in text, no calls | `"none"` | `{"type": "none"}` | mode `NONE` |
| Required | Model must call some tool, its choice | `"required"` | `{"type": "any"}` | mode `ANY` |
| Forced | Model must call one named tool | `{"type": "function", "function": {"name": "x"}}` | `{"type": "tool", "name": "x"}` | mode `ANY` plus an allowed-names list |
Treat this table as a guide, not a contract. Parameter names and accepted values change between API versions, so check your provider's current documentation before copying a value.
Automatic
Automatic is the default whenever you pass tools. The model weighs the tool descriptions against the request. If it skips a tool you wanted, improve the description first. Forcing the tool should be your second step, because a forced call hides a weak description instead of fixing it.
Disabled
With none, the model still sees the tool definitions but cannot call them. This is useful in two places. One is the final turn of a loop that hit its step limit, where you want a text summary of what was gathered. The other is a conversational turn where you already know no tool is needed. Keep the tool list in the request. Some providers reject a conversation that contains earlier tool calls and results if no tools are declared.
Required
With required (or any), the model must emit at least one call but picks which tool. This suits routers and classifiers, where every input should map to some action.
Forced
Forcing a named tool guarantees a call to that tool with arguments that match its schema. This makes it a common way to get structured output, covered in a later chapter. The model still generates the arguments, so you must still validate them.
A decision flow
Choosing per step, not per conversation
The setting applies to one request, not to the whole conversation. In a loop you call the model many times, so you can pick a different mode at each step. A forced call on every step would never end: the model would be required to call a tool each time and could never produce a final answer.
The pattern is to force or require at the start, then relax:
- Step 0: force
search_ordersso the model always looks something up. - Later steps:
auto, so the model can keep calling tools or answer. - Last allowed step:
none, so the loop ends with text.
The following program runs as shown. It keeps the policy in one function and translates it to two provider shapes. It does not call a real model.
def choose_tool_choice(step, max_steps, first_tool=None):
"""Return a provider-neutral tool-choice dict for this step."""
if step == max_steps - 1:
return {"mode": "none"}
if step == 0 and first_tool:
return {"mode": "tool", "name": first_tool}
return {"mode": "auto"}
def to_openai(choice):
if choice["mode"] == "tool":
return {"type": "function", "function": {"name": choice["name"]}}
if choice["mode"] == "required":
return "required"
return choice["mode"] # "auto" or "none"
def to_anthropic(choice):
if choice["mode"] == "tool":
return {"type": "tool", "name": choice["name"]}
if choice["mode"] == "required":
return {"type": "any"}
return {"type": choice["mode"]} # auto or none
if __name__ == "__main__":
for step in range(3):
choice = choose_tool_choice(step, max_steps=3, first_tool="search_orders")
print(step, to_openai(choice), to_anthropic(choice))Running it prints a forced call at step 0, auto at step 1, and none at step 2. In your real loop, pass the translated value as the tool_choice argument of your SDK call.
Restricting which tools are visible
A forced choice picks one tool. Often you want something between that and everything. There are two ways to narrow the set.
- Send fewer tool definitions. Only the tools you include can be called. This is the most portable method and also saves input tokens. It works well when the available tools depend on the user's role or the current workflow stage.
- Use an allow-list setting. Some providers accept a list of permitted tool names alongside the full definitions. Gemini's function-calling config does this with allowed function names. Sending the same full list on every request keeps the prompt prefix identical, which can help with prompt caching on providers that support it.
Narrowing tools is also a security control. A tool the model cannot see in a request cannot be proposed in that request. Still, check every call in your dispatcher against the same list, because the model can sometimes name a tool you did not send.
Limiting parallel calls
By default, many providers let the model emit several calls in one turn. When calls depend on each other or touch shared state, that can cause trouble: for example, two writes issued at once where the second needs the first one's result. Providers offer a switch to limit it, such as parallel_tool_calls: false on OpenAI-style APIs or a disable_parallel_tool_use flag inside the Anthropic tool-choice object. With it set, the model emits at most one call per turn. Your loop must already handle a list of calls, because other settings and other models will still return several.
The cost of disabling parallel calls is latency. Three independent lookups then take three model round trips instead of one. Disable it only where ordering matters.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Loop never produces a final answer | `required` or a forced tool on every step | Relax to `auto` after the first step and use `none` on the last |
| Request rejected, "tool not found" | Forced a name that is missing from the `tools` list, or misspelled | Build the choice from the same registry that builds the definitions |
| Request rejected after the first tool result | Tools omitted from a later request while history contains calls | Keep the definitions in the request and use `none` to disable |
| Model ignores a tool under `auto` | Vague description, or an overlapping tool | Rewrite the description, then consider forcing |
| Forced tool returns odd arguments | Model had to fill the schema with no good value | Make fields optional where appropriate and validate |
| Setting is silently ignored | Provider or model does not support that mode, or combines poorly with another feature such as extended reasoning | Read the provider notes for your model and test it |
Forcing has one more cost. The model is compelled to call the tool even when the question does not fit it, so it may invent plausible arguments. A forced get_weather call on the message "thanks!" will still produce a city. Gate forcing behind your own check, such as a keyword rule, a classifier, or a workflow state, instead of forcing blindly.
Checklist
- Default to
autoand fix descriptions before forcing. - Use
noneto end loops and to run turns that need no tools. - Use
requiredfor routers, and a named tool for structured output or fixed workflow steps. - Pick the mode per request, and never force on every step of a loop.
- Keep an allow-list in your dispatcher no matter what you sent to the model.
- Treat provider value names as version-specific and verify them.
Exercise
Your loop forces the tool lookup_customer on every request. The model calls it, you return the result, and the loop keeps running until it hits max_steps and raises an error. What is wrong, and how do you fix it with tool choice?
Show answer
Forcing a named tool on every step requires the model to call a tool each time, so it can never produce a final text answer and the loop cannot end normally. Force the tool only on the first step (step 0), then switch to auto so the model may answer or call more tools. Optionally use none on the last allowed step so the loop always ends with text.
Exercise
A request sends three tool definitions and uses tool choice auto. The model returns a call to a fourth tool, delete_account, which you never sent. What should your dispatcher do, and why is tool choice not enough to prevent this?
Show answer
The dispatcher should reject the call because the name is not in the allow-list for this request, and return an error result such as "Unknown tool: delete_account" so the model can recover. Tool choice only influences what the model proposes. It is not an enforcement mechanism, so the model can still output an unlisted name, and your code is the only reliable gate.