Tool Use
Define tools, let the model call them and feed results back, using the OpenAI function-calling shape on every tool-capable model.
Tool use (function calling) lets the model call functions you define. AnyRouter uses the OpenAI schema, so any tool-capable model in the catalog accepts the same request; change model and nothing else.
The loop: send tools, run whatever the model calls, return results as tool messages, repeat until the model returns a normal completion.
1. Send tools
const tools = [
{
type: "function",
function: {
name: "get_weather",
description: "Return the current weather for a city.",
parameters: {
type: "object",
properties: {
city: { type: "string", description: "City name" },
unit: { type: "string", enum: ["celsius", "fahrenheit"] },
},
required: ["city"],
},
},
},
]
const response = await client.chat.completions.create({
model: "openai/gpt-5.4-mini",
messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
tools,
tool_choice: "auto",
})2. Handle tool calls
When the model calls a tool, finish_reason is tool_calls and the message carries tool_calls:
{
"choices": [
{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_01ABC",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"Tokyo\",\"unit\":\"celsius\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
]
}Run the tool and send the result as a tool message, with the original user message and the assistant's tool-call message for context:
const toolCall = response.choices[0].message.tool_calls![0]
const args = JSON.parse(toolCall.function.arguments)
const weather = await getWeather(args.city, args.unit)
const followup = await client.chat.completions.create({
model: "openai/gpt-5.4-mini",
messages: [
{ role: "user", content: "What's the weather in Tokyo?" },
response.choices[0].message,
{ role: "tool", tool_call_id: toolCall.id, content: JSON.stringify(weather) },
],
tools,
})Models can emit several tool calls in one turn. Run them all (for example with Promise.all) and send exactly one tool message per tool_call_id; a missing result usually produces an error or a degraded answer.
tool_choice
"auto"(default when tools are present): the model decides."none": plain text only.{ type: "function", function: { name: "get_weather" } }: force that function.
Structured output without tools
Use response_format: { type: "json_object" }, or a schema for guaranteed conformance:
response_format: {
type: "json_schema",
json_schema: { name: "weather_report", schema: { /* JSON Schema */ }, strict: true },
}AnyRouter routes requests to third-party models. Models and outputs are provided as is, with no warranty; you use them at your own risk. Disclaimer