Tool calling
Define function tools, assemble streamed tool-call arguments, and return the result to the model.
Tool calling works on chat completions, Responses and Messages. Pass your functions in tools. The model answers with the call it wants to make. You run it, then send the result back.
Not every model supports tools. GET /v1/model/info lists the capability flags for each model.
Example#
This example forces one tool call, assembles streamed arguments, returns the local result, then streams the final answer. The last chunk carries exact token counts.
How it works#
- The model sends the call in
delta.tool_calls. Theidand the functionnamearrive first. Theargumentsstring arrives in pieces. Join the pieces, then parse the JSON once the stream ends. - Send the result back as a message with
roleset totooland the sametool_call_id. Keep the assistant message that made the call in the history. tool_choiceforces a call to one named function. Leave it unset to let the model decide.- A reasoning model spends
max_tokenson thinking too. See Reasoning.
Other wire formats#
POST /v1/responses takes function tools and function_call_output. POST /v1/messages uses Anthropic tool_use blocks. See API Compatibility.
Gate tool calls with a fast model#
A coding agent can ask Laya before it runs a tool. System One has a Claude Code hook that asks before a destructive command runs.