Tool calling
Define tools, let the model call them, and return their results.
Which models support tools#
Models with the tools capability accept tools; the model catalog shows it. A request with tools to another model is refused with 400 unsupported_parameter before any upstream call.
The samples read the model id from PANWARD_TOOLS_MODEL: set it to a chat model with the tools capability from the model catalog. A model without it refuses the request with 400 unsupported_parameter before any upstream call, so nothing is billed.
Define tools#
tools is a list of up to 128 entries of type function, each with a name (letters, digits, _ and -, up to 64 characters), a description and the JSON Schema of its arguments in parameters. The Python sample makes the whole round trip.
cat > body.json <<EOF { "model": "$PANWARD_TOOLS_MODEL", "messages": [{"role": "user", "content": "Is bed 12 on ward 4 free?"}], "tools": [{ "type": "function", "function": { "name": "get_bed_status", "description": "The status of one hospital bed.", "parameters": { "type": "object", "properties": {"ward": {"type": "integer"}, "bed": {"type": "integer"}}, "required": ["ward", "bed"] } } }], "tool_choice": "auto" } EOF curl https://api.panward.ai/v1/chat/completions \ -H "Authorization: Bearer $PANWARD_API_KEY" \ -H "Content-Type: application/json" \ --data-binary @body.json
import json, os from openai import OpenAI client = OpenAI( base_url="https://api.panward.ai/v1", api_key=os.environ["PANWARD_API_KEY"], ) model = os.environ["PANWARD_TOOLS_MODEL"] tools = [{ "type": "function", "function": { "name": "get_bed_status", "description": "The status of one hospital bed.", "parameters": { "type": "object", "properties": {"ward": {"type": "integer"}, "bed": {"type": "integer"}}, "required": ["ward", "bed"], }, }, }] messages = [{"role": "user", "content": "Is bed 12 on ward 4 free?"}] reply = client.chat.completions.create(model=model, messages=messages, tools=tools) message = reply.choices[0].message messages.append(message.model_dump(exclude_none=True)) for call in message.tool_calls or []: arguments = json.loads(call.function.arguments) # validate before use result = {"ward": arguments["ward"], "bed": arguments["bed"], "status": "free"} messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)}) final = client.chat.completions.create(model=model, messages=messages, tools=tools) print(final.choices[0].message.content)
import OpenAI from "openai"; const client = new OpenAI({ baseURL: "https://api.panward.ai/v1", apiKey: process.env.PANWARD_API_KEY, }); const reply = await client.chat.completions.create({ model: process.env.PANWARD_TOOLS_MODEL!, messages: [{ role: "user", content: "Is bed 12 on ward 4 free?" }], tools: [ { type: "function", function: { name: "get_bed_status", description: "The status of one hospital bed.", parameters: { type: "object", properties: { ward: { type: "integer" }, bed: { type: "integer" } }, required: ["ward", "bed"], }, }, }, ], }); for (const call of reply.choices[0].message.tool_calls ?? []) { console.log(call.id, call.function.name, JSON.parse(call.function.arguments)); }
Let the model choose, or force a choice#
tool_choice is none (no tool), auto (the model decides; the default when tools are present), required (at least one tool call), or a named function the model must call.
cat > body.json <<EOF { "model": "$PANWARD_TOOLS_MODEL", "messages": [{"role": "user", "content": "Bed 12, ward 4."}], "tools": [{"type": "function", "function": {"name": "get_bed_status", "parameters": {"type": "object", "properties": {"ward": {"type": "integer"}, "bed": {"type": "integer"}}}}}], "tool_choice": {"type": "function", "function": {"name": "get_bed_status"}} } EOF curl https://api.panward.ai/v1/chat/completions \ -H "Authorization: Bearer $PANWARD_API_KEY" \ -H "Content-Type: application/json" \ --data-binary @body.json
Handle tool calls#
A response that calls tools has finish_reason: "tool_calls" and message.tool_calls, each with an id, the function.name and function.arguments. The arguments are a JSON-encoded string written by the model: parse and validate them before you act on them.
{
"id": "chatcmpl-01JC7Q4N8G5T2R9X1M3K6P7W4D",
"object": "chat.completion",
"created": 1790000000,
"model": "z-ai/glm-5.3",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_01JC7Q4P2B",
"type": "function",
"function": {
"name": "get_bed_status",
"arguments": "{\"ward\": 4, \"bed\": 12}"
}
}
]
},
"finish_reason": "tool_calls"
}
],
"usage": {
"prompt_tokens": 96,
"completion_tokens": 21,
"total_tokens": 117
}
}Return results#
Append the assistant message as you received it, then one message with role tool per call, holding the result in content and the call's id in tool_call_id. Send the conversation again for the model's answer.
Parallel tool calls#
A model may return several calls in one response; handle each and return one tool message per call. parallel_tool_calls (default true) controls whether it may.
Tool calls in streams#
In a stream, tool calls arrive in delta.tool_calls as fragments, each with the index of its call. Concatenate the function.arguments fragments of the same index, and parse them once the stream has ended.
calls = {}
stream = client.chat.completions.create(model=model, messages=messages, tools=tools, stream=True)
for chunk in stream:
for delta in (chunk.choices[0].delta.tool_calls or []) if chunk.choices else []:
call = calls.setdefault(delta.index, {"id": None, "name": "", "arguments": ""})
if delta.id:
call["id"] = delta.id
if delta.function and delta.function.name:
call["name"] += delta.function.name
if delta.function and delta.function.arguments:
call["arguments"] += delta.function.arguments
for call in calls.values():
print(call["id"], call["name"], json.loads(call["arguments"]))Receipts and tools#
Each call to the model is a request with its own receipt. A receipt binds the bytes of that model call, tool definitions and tool results included, and says nothing about what your tool did.