Function Calling
Let supported models request functions and return their results to the conversation.
Function calling connects the model to application code, APIs, databases, and other tools that you control.
glm-5.2, glm-5, kimi-k3, kimi-k2, deepseek-v4-pro, and
deepseek-v4-flash support tools. deepseek-r1 does not.
GLM 5 supports OpenAI-style function tools. OpenRouter server tools such as web search, web fetch, file parsing, code execution, or router plugins are not executed by this endpoint.
1. Define a function
Send one or more OpenAI-compatible tools:
{
"model": "glm-5.2",
"messages": [
{
"role": "user",
"content": "What is the weather in Shanghai?"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "City name, for example Shanghai."
}
},
"required": ["city"],
"additionalProperties": false
}
}
}
],
"tool_choice": "auto",
"max_completion_tokens": 800
}2. Read the tool call
When the model chooses a function, finish_reason is tool_calls:
{
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"Shanghai\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
]
}Parse and validate function.arguments, then execute the function in your
application. The model does not execute it for you.
3. Return the result
Append the assistant tool call and a matching tool message:
{
"model": "glm-5.2",
"messages": [
{
"role": "user",
"content": "What is the weather in Shanghai?"
},
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"Shanghai\"}"
}
}
]
},
{
"role": "tool",
"tool_call_id": "call_123",
"content": "{\"temperature_c\":24,\"condition\":\"clear\"}"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string" }
},
"required": ["city"]
}
}
}
],
"max_completion_tokens": 800
}The next response can turn the tool result into a natural-language answer or request another tool.
Tool choice
| Value | Behavior |
|---|---|
auto | The model decides whether to call a function. |
none | The model does not call a function. |
required | The model must call one or more available functions. |
| Named function object | Forces a particular function when supported. |
Reliability guidance
- Validate all arguments before executing a function.
- Apply authorization checks in your application, not in the prompt.
- Set timeouts and size limits for external tool results.
- Return structured JSON when possible.
- Keep assistant tool calls and tool results together when trimming history.
Tool definitions are part of the input
Large JSON schemas are included in the prompt and count toward input usage on every request. Send only the tools relevant to the current task.