Skip to content
docs

Tool calling

A model does not run functions itself. It receives their descriptions and returns an intent: a function name and arguments in JSON. You run the code and send the result in the next request. The model then either asks for another call or answers with text.

To check whether a model supports tool calling, look in GET /v1/models: its capabilities.features must include tools. See Models.

  1. Send a request with the tools field.
  2. Get a response with finish_reason: "tool_calls" and a tool_calls array. Run the functions on your side.
  3. Send a request with the same history plus the assistant message and the results in role: "tool" messages.

The router keeps no history: each request is sent in full, including earlier calls and their results.

step-1.sh
curl https://api.waibee.com/v1/chat/completions \
-H "Authorization: Bearer $WAIBEE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemini-3.6-flash",
"messages": [{ "role": "user", "content": "What is the weather in Berlin?" }],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Current weather in a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "City" },
"unit": { "type": "string", "enum": ["c", "f"] }
},
"required": ["city"]
}
}
}]
}'
response.json
{
"choices": [{
"index": 0,
"finish_reason": "tool_calls",
"message": {
"role": "assistant",
"tool_calls": [{
"id": "29veklzD",
"type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\":\"Berlin\"}" }
}]
}
}],
"usage": { "prompt_tokens": 37, "completion_tokens": 86, "total_tokens": 123 }
}

Two things trip people up most often:

  • arguments is a string of JSON, not an object. You have to parse it, and it may fail validation: handle a parse error as an ordinary failure, not as an exceptional case.
  • The call id is required in step 3. The model uses it to match the result to the call.
step-3.sh
curl https://api.waibee.com/v1/chat/completions \
-H "Authorization: Bearer $WAIBEE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemini-3.6-flash",
"messages": [
{ "role": "user", "content": "What is the weather in Berlin?" },
{ "role": "assistant", "tool_calls": [{
"id": "29veklzD", "type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\":\"Berlin\"}" } }] },
{ "role": "tool", "tool_call_id": "29veklzD",
"content": "{\"temp_c\": 19, \"cond\": \"cloudy\"}" }
],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Current weather in a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "City" },
"unit": { "type": "string", "enum": ["c", "f"] }
},
"required": ["city"]
}
}
}]
}'

The response comes back as ordinary text with finish_reason: "stop". Pass the tools definitions in this request too: without them, the model cannot call the tool again if it needs more data.

The content of a role: "tool" message is an arbitrary string. JSON works better than free text: it is less ambiguous and saves the tokens that explanations would take.

A model can return several items in tool_calls at once. Run all of them and add a separate role: "tool" message for each tool_call_id. The order of the result messages does not matter, but every call needs an answer: a missing result usually causes a validation error on the model’s side.

To turn off parallel calls, pass parallel_tool_calls: false. Models with parallel_tool_calls in capabilities.features understand it.

ValueBehavior
"auto"The model decides whether to call a tool or answer with text. This is the default
"none"The model sees the tools but cannot call them
"required"The model must call at least one tool
{"type": "function", "function": {"name": "get_weather"}}The model must call this function

The values are passed to the model as is, so the supported options depend on the model family. "auto" and "none" work everywhere.

On POST /v1/messages, tools are described differently: the name, description, and input_schema fields sit at the top level, with no function wrapper.

Terminal window
curl https://api.waibee.com/v1/messages \
-H "x-api-key: $WAIBEE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-haiku-4.5",
"max_tokens": 512,
"tools": [{
"name": "get_weather",
"description": "Current weather in a city",
"input_schema": {
"type": "object",
"properties": { "city": { "type": "string", "description": "City" } },
"required": ["city"]
}
}],
"messages": [{ "role": "user", "content": "What is the weather in Berlin?" }]
}'

The call arrives as a tool_use block with stop_reason: "tool_use", and the arguments are in input as an already parsed object:

{
"type": "message",
"stop_reason": "tool_use",
"content": [{
"type": "tool_use",
"id": "toolu_011Q2fbw78h7hDJT2mBJjEfx",
"name": "get_weather",
"input": { "city": "Berlin" }
}]
}

Return the result as a tool_result block inside a user message:

{
"role": "user",
"content": [{
"type": "tool_result",
"tool_use_id": "toolu_011Q2fbw78h7hDJT2mBJjEfx",
"content": "{\"temp_c\": 19, \"cond\": \"cloudy\"}"
}]
}

Built-in web search is also declared in tools, but the three-step loop does not apply to it: the router runs the search inside your single request. How to turn it on and what the response contains: Web search.

With "stream": true, a call arrives in parts. In delta.tool_calls, the first fragment carries id and name, and the following ones carry only pieces of arguments. Assemble them by the index field until you have valid JSON.

data: {"choices":[{"index":0,"delta":{"role":"assistant","tool_calls":[
{"index":0,"id":"zmahjJWU","type":"function",
"function":{"name":"get_weather","arguments":""}}]}}]}
data: {"choices":[{"index":0,"delta":{"tool_calls":[
{"index":0,"function":{"arguments":"{\"city\":\"Berlin\"}"}}]}}]}
data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}],"usage":{...}}
data: [DONE]

The call is complete when a chunk with finish_reason: "tool_calls" arrives. For the stream format, see Streaming.

  • The number of tools in one request is limited; see Limits.
  • Web search is the only server-side tool available, on all three endpoints (see above). Server-side web_fetch_* and code_execution_* are rejected with feature_not_supported (400). Implement the equivalent as your own function on the client side.
  • MCP is not supported: a tool of type mcp* and the mcp_servers field on /v1/messages are rejected with feature_not_supported (400).
  • Argument schemas are passed to the provider unchanged. How strictly the model follows a schema depends on the model. If you need a guaranteed structure for the response rather than for arguments, see Structured outputs.
  • Write descriptions for a human reader. The name, the function description, and the field descriptions are the only instructions the model has for deciding when to call a tool and with what. One precise sentence works better than a list of implementation details.
  • Narrow the schema. Use enum instead of a free-form string, put required fields in required, and leave out parameters that are optional “just in case”.
  • Cap the number of iterations. A “call, result, call” loop may never converge: limit the number of rounds and end the conversation with a clear error.
  • Check finish_reason. Run tools only when it is tool_calls; otherwise the response is ordinary text.
  • Keep the tool count low. Dozens of similar functions in one request raise the chance that the model picks the wrong one, and they bloat the input.