Tool call
The call is made from the model to the server for verification and execution, and then the tool results are returned.
Execution order
- Request declaration tool name, description, and JSON parameter schema.
- The model returns the tool name, call ID, and parameter text.
- The application parses and verifies parameters and executes them within its own authority.
- Associate the result with the original call ID and return it to continue generating the final answer.
Chat tool declaration example
{
"type": "function",
"function": {
"name": "lookup_order",
"description": "查询当前用户自己的订单状态",
"parameters": {
"type": "object",
"properties": {
"order_id": {
"type": "string"
}
},
"required": [
"order_id"
],
"additionalProperties": false
}
}
}
Place this object into the support tool’s Chat request tools array. After receiving message.tool_calls, first add the assistant call message to the history, and then add role: "tool" with the result message corresponding to tool_call_id. Responses and Messages use different tool result items, do not mix them.
Server-side verification
Model output is not authorized. Tools allowed by the whitelist verify schema, user ownership and business status; operations such as deletion and payment continue to comply with the application’s own confirmation process. Set the maximum number of tool calls, single timeout, and output length to prevent loop execution.
Streaming tool parameters
Parameters for the same call may be split into multiple fragments. Accumulate by call ID or index, parse and execute after receiving the complete call, do not trigger the tool for each JSON fragment.
Complete first round request
{
"model": "MODEL_ID",
"messages": [
{
"role": "user",
"content": "查询我自己的订单 A123"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "lookup_order",
"description": "查询当前用户自己的订单状态",
"parameters": {
"type": "object",
"properties": {
"order_id": {
"type": "string"
}
},
"required": [
"order_id"
],
"additionalProperties": false
}
}
}
]
}
How to return tool results
The following is a diagram of the historical structure of the second round. The actual call ID and parameters must come from the model response, and the results come from the business system:
{
"model": "MODEL_ID",
"messages": [
{
"role": "user",
"content": "查询我自己的订单 A123"
},
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "EXAMPLE_CALL_ID",
"type": "function",
"function": {
"name": "lookup_order",
"arguments": "{\"order_id\": \"A123\"}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "EXAMPLE_CALL_ID",
"content": "{\"status\": \"shipped\"}"
}
]
}
When there are multiple calls, pair them one by one and return all necessary results. Tools will continue to be provided based on model capabilities, and the number of consecutive rounds of calls will be limited. Tool exceptions return controlled error summaries and do not hand over the stack, credentials, or other user data to the model.
Verification list
First use side-effect-free testing tools to verify selections, JSON parameters, call IDs, and final text; then test for illegal parameters, unauthorized resources, tool timeouts, and multi-tool conditions. The model refuses to execute when forging a non-existent tool name, and its contents cannot be eval dynamically.