How tool_choice Modes Control Function Calling Behavior
In function/tool calling APIs, tool_choice controls whether and how the model selects from your declared tools on a given turn—letting you balance model autonomy against deterministic tool execution.
"auto"(model decides): The model may call one or more declared tools, emit a normal assistant message without calling tools, or call a tool and follow up with text. Use it for general assistants where only some turns require external data or actions."required"(must call at least one tool): The model is constrained to call one or more of the provided tools rather than replying with plain text alone. Use it when a step cannot proceed without grounding or state lookup from a tool.- Forced function selection (
{ type: "function", function: { name: "..." } }): The model is restricted to calling that specific tool on the turn, locking the tool name so arguments are generated against that function’s JSON Schema. Use it when the orchestrator already knows the exact action required for the current state.
Steps to Choose and Apply tool_choice Safely
- Declare your tool schemas in
tools: Provide clear function names, descriptions, and strict parameter schemas so the model understands when and how each tool applies. - Select the right
tool_choicemode for the workflow stage: Use"auto"for open-ended turns,"required"when tool output is mandatory before answering, and forced function selection for deterministic pipeline steps. - Control multi-tool fan-out when needed: Pair
"required"or"auto"withparallel_tool_calls: falsewhen downstream state or rate limits require single-tool sequential execution. - Loop until a non-tool or final response is reached: Execute the returned
tool_calls, appendrole: "tool"results with matchingtool_call_idvalues, and switchtool_choiceback to"auto"(or omit forcing) once tool data is collected so the model can synthesize the final answer.
TypeScript Example: Switching Between auto, required, and Forced Tool Selection
import OpenAI from "openai";
const openai = new OpenAI();
const tools: OpenAI.Chat.Completions.ChatCompletionTool[] = [
{
type: "function",
function: {
name: "lookup_order_status",
description: "Fetch current payment and fulfillment status for an order ID.",
parameters: {
type: "object",
additionalProperties: false,
required: ["orderId"],
properties: {
orderId: { type: "string", description: "Order identifier" },
},
},
},
},
{
type: "function",
function: {
name: "issue_refund",
description: "Refund a paid order after verifying eligibility.",
parameters: {
type: "object",
additionalProperties: false,
required: ["orderId", "reason"],
properties: {
orderId: { type: "string" },
reason: { type: "string" },
},
},
},
},
];
export async function runToolChoiceModes(orderId: string) {
const baseMessages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
{
role: "system",
content: "You are a backend order orchestration assistant.",
},
{
role: "user",
content: `Check status and refund order \${orderId} if eligible.`,
},
];
// 1. "auto": model chooses whether to call tools or respond directly
const autoRes = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: baseMessages,
tools,
tool_choice: "auto",
});
// 2. "required": model must call at least one tool from `tools`
const requiredRes = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: baseMessages,
tools,
tool_choice: "required",
parallel_tool_calls: false,
});
// 3. Forced function selection: model must call `lookup_order_status` specifically
const forcedRes = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: baseMessages,
tools,
tool_choice: {
type: "function",
function: { name: "lookup_order_status" },
},
});
return {
autoToolCalls: autoRes.choices[0]?.message.tool_calls ?? [],
requiredToolCalls: requiredRes.choices[0]?.message.tool_calls ?? [],
forcedToolCalls: forcedRes.choices[0]?.message.tool_calls ?? [],
};
}
Helpful Tips for Production Tool Routing
- Avoid leaving
tool_choice: "required"on every follow-up turn: If you keep"required"aftertoolmessages are already returned, the model is forced to keep calling tools instead of giving a final answer—switch back to"auto"once required data is gathered. - Provide a fallback or clarification tool if using
"required": When forcing a tool call on ambiguous user input, include a tool (or nullable parameter) that represents missing/invalid arguments so the model is not forced to hallucinate IDs or values. - Verify compatibility across normalized providers: When routing through an OpenAI-compatible layer or LiteLLM across multiple providers, test
"required"and named function forcing per target model, as support forparallel_tool_callsand forced tool options can vary by backend.
You must log in or register to comment.
