Tool use and function calling
Let a model call your functions — the request loop, both formats, and the mistakes that cost tokens.
Tool use lets the model ask you to run a function and then continue with the result. It is the mechanism behind every coding agent.
The loop
- You send
messagesplustoolsdescribing what is available. - The model replies with
stop_reason: tool_useand atool_useblock naming the tool and its arguments. - You execute it — the gateway never runs your code.
- You send the whole history back plus a
tool_resultblock. - The model produces its final answer, or asks for another tool.
Defining tools (Messages format)
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"tools": [{
"name": "get_weather",
"description": "Current weather for a city. Use when the user asks about weather.",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string", "description": "City name"}},
"required": ["city"]
}
}],
"messages": [{"role": "user", "content": "Weather in Paris?"}]
}
Returning a result
{
"role": "user",
"content": [{
"type": "tool_result",
"tool_use_id": "toolu_01ABC...",
"content": "18C, light rain"
}]
}
tool_result goes in a message with role user, and tool_use_id must match the id from the model's request exactly. Mismatched ids are rejected as an invalid request.OpenAI format
The /v1/chat/completions endpoint takes the OpenAI tools / tool_calls shape instead, with results returned as messages with role tool.
What tool use costs
- Definitions are resent every turn. Twenty verbose tool schemas can dominate your input tokens before the conversation even starts.
- Results become context. A tool returning 200 rows of JSON puts all of it in the next request, and every request after that.
- Each round trip is a billed call. A five-tool task is at least six requests.
- Cap tool output length and summarise before returning. Truncating a result to what the model actually needs is the highest-leverage change in most agent loops.
Tool schemas are stable across turns, which makes them a strong prompt caching candidate alongside the system prompt.