Sub topic 10
Controlling when and which tools are used
This chapter assumes you can write a tool definition, read a tool call from a model reply, and run the basic loop from earlier chapters. By default the model decides for itself whether to call a tool. Most providers let you override that decision with a setting usually called tool_choice.
The three modes
Names differ by provider, but the behavior falls into three groups.
| Mode | Typical values | Effect |
|---|---|---|
| Automatic | `auto` | The model may answer directly or call any tool. |
| Forced | `required`, `any`, or a named tool | The model must call a tool. With a name, it must call that one. |
| Disabled | `none` | The model must answer in text, even if tools are defined. |
Check your provider's documentation for the exact strings and wrapper shape. Some providers take a plain string, and others take an object such as {"type": "tool", "name": "get_weather"}.
Choosing a mode
Use auto for general assistants. The model can skip tools for small talk and use them when the question needs outside data.
Force a tool when your program always needs a call. An example is a pipeline step that must extract fields into a known schema. A forced named tool also removes the chance that the model answers in prose and breaks your parser.
Disable tools when you want a final answer and no more actions. A common case is the last step of a loop, after you have hit your step limit and want the model to summarize what it found.
A request with a forced tool
The sketch below uses a provider-neutral request shape. Rename the keys to match your SDK.
def build_request(messages, tools, mode="auto", tool_name=None):
request = {"messages": messages, "tools": tools}
if mode == "named":
request["tool_choice"] = {"type": "tool", "name": tool_name}
else:
request["tool_choice"] = mode # "auto", "required", or "none"
return requestGotchas with forced choice
Forcing a tool inside a loop can make it run forever. If every turn must call a tool, the model can never give a final answer. Force the tool on the first turn only, then switch to auto or none.
def choice_for_step(step):
return "required" if step == 0 else "auto"A forced call also makes the model produce arguments even when the user's message lacks the needed information. It may invent values to fill required fields. Validate the arguments as you would for any call, and consider adding an explicit "unknown" option to the schema.
Limiting which tools are visible
The other control is the tools list itself. The model can only call tools you send. If a request needs only two of your twenty tools, send those two. A short list reduces wrong picks, saves input tokens, and shrinks the set of actions a bad call can trigger. Some providers also offer an allowed-tools option that restricts choices while keeping the full list in the request.
Exercise
Your loop forces a named tool on every turn, and it never ends. Why does this happen, and what is the fix?
Show answer
A forced tool means the model must emit a tool call each turn, so it can never reply with a final text answer and the loop only stops at the step limit. Force the tool on the first turn only, then switch to auto, or to none on the last step so the model must answer in text.