Quick answer: GLM 5.4 function calling uses the OpenAI-style tools array on Z.ai's Chat Completions endpoint with model glm-5.4. You can define up to 128 tools, each with a name matching ^[a-zA-Z0-9_-]+$ (max 64 characters) and a JSON Schema parameters object. tool_choice only accepts "auto", so you cannot force a specific tool. For streaming, set stream: true and tool_stream: true. Because GLM 5.4 always thinks and keeps prior reasoning by default, you must send each assistant message back with its tool_calls and its unmodified reasoning_content.
Z.ai's GLM-5.4 page describes tool use in one line, "powerful tool calling," with no schema or example. The details live in the API reference and the capability guides, spread over four pages. This tutorial puts the GLM 5.4 function calling rules in one place, with a working loop and the mistakes we see most often when people port GLM 5.2 or OpenAI code. Every limit below is from Z.ai's docs, checked December 3, 2026.
Why GLM 5.4 function calling breaks ported code
The pain is that GLM 5.4 function calling looks OpenAI-compatible but is not identical. Three differences cause most failures:
- No forced tool choice. Code that sends
tool_choice: {"type": "function", ...}or"required"gets rejected or ignored; only"auto"is supported. - Forced, preserved thinking. GLM 5.4 cannot turn thinking off, and
clear_thinkingdefaults tofalse. Dropreasoning_contentbetween turns and the model loses its plan. - Streaming needs a second flag. Without
tool_stream: true, streamed GLM 5.4 function calling responses do not stream the tool call the way you expect.
What this guide adds over generic function-calling tutorials is the GLM 5.4-specific loop: how reasoning, tool calls and tool results must be ordered.
GLM 5.4 function calling limits
| GLM 5.4 function calling rule | Value |
|---|---|
| Endpoint | POST https://api.z.ai/api/paas/v4/chat/completions |
| Model | glm-5.4 |
| Max tools per request | 128 |
| Tool types | function, retrieval, web_search |
| Function name | ^[a-zA-Z0-9_-]+$, max 64 characters |
| Parameters | JSON Schema object |
tool_choice | "auto" only |
tool_stream | Boolean, default false, needs stream: true |
| Tool result message | role: "tool" with tool_call_id |
max_tokens | 1 to 131,072 |
| Thinking | Always enabled |
Beyond custom functions, GLM 5.4 function calling also accepts built-in tools. web_search is a separate tool type with search_engine: "search_pro_jina" and is billed at $0.01 per use on Z.ai's pricing page.
Step 1: Define GLM 5.4 function calling tools
Keep GLM 5.4 function calling schemas small and strict. Clear description fields matter more than clever names, because the model decides when to call a tool from the description alone.
tools = [{
"type": "function",
"function": {
"name": "get_order_status",
"description": "Look up the shipping status of one order by its ID.",
"parameters": {
"type": "object",
"properties": {
"order_id": {"type": "string", "description": "Order ID like A-10293"}
},
"required": ["order_id"],
"additionalProperties": False
}
}
}]
Good GLM 5.4 function calling practice: one job per tool, required fields listed, enums for fixed choices, and no free-text "options" blobs.
Step 2: Run the GLM 5.4 function calling agent loop
This is the core GLM 5.4 function calling loop. The important line is the one that appends the full assistant message, including reasoning_content, before the tool results.
import os, json
from openai import OpenAI
client = OpenAI(base_url="https://api.z.ai/api/paas/v4",
api_key=os.environ["ZAI_API_KEY"])
messages = [{"role": "user", "content": "Where is order A-10293?"}]
while True:
resp = client.chat.completions.create(
model="glm-5.4",
messages=messages,
tools=tools,
tool_choice="auto",
max_tokens=8000,
extra_body={"reasoning_effort": "high"},
)
msg = resp.choices[0].message
turn = {"role": "assistant", "content": msg.content or ""}
reasoning = getattr(msg, "reasoning_content", None)
if reasoning:
turn["reasoning_content"] = reasoning
if msg.tool_calls:
turn["tool_calls"] = [tc.model_dump() for tc in msg.tool_calls]
messages.append(turn)
if not msg.tool_calls:
print(msg.content)
break
for tc in msg.tool_calls:
args = json.loads(tc.function.arguments)
result = get_order_status(**args) # your function
messages.append({"role": "tool", "tool_call_id": tc.id,
"content": json.dumps(result)})
Three GLM 5.4 function calling details in that loop come straight from Z.ai's docs. arguments arrives as a JSON string, so parse and validate it. Every tool message needs the matching tool_call_id. And Z.ai says thinking blocks "should be explicitly preserved and returned together with the tool results," which is why reasoning_content goes back unmodified.
Want to see how GLM 5.4 plans multi-step work before you write tools? Ask GLM 5.4 in glm5.app chat to outline the tool calls it would make for your task.
Step 3: Stream GLM 5.4 function calling with tool_stream
For a responsive UI, stream GLM 5.4 function calling output. Set both flags, then rebuild each tool call by its index, because function.arguments arrives in pieces.
stream = client.chat.completions.create(
model="glm-5.4", messages=messages, tools=tools, stream=True,
extra_body={"tool_stream": True},
)
calls = {}
for chunk in stream:
delta = chunk.choices[0].delta
for tc in (delta.tool_calls or []):
if tc.index not in calls:
calls[tc.index] = tc
elif tc.function and tc.function.arguments:
calls[tc.index].function.arguments += tc.function.arguments
Z.ai notes that streamed arguments arrive "without buffering or JSON validation." Parse only after the stream ends, and guard against empty fragments. The same stream also carries reasoning_content and content deltas; collect reasoning_content so you can return it next turn.
Step 4: Control GLM 5.4 function calling cost and latency
GLM 5.4 function calling runs on top of forced thinking, so each round trip pays for reasoning tokens at $4.40 per million output tokens. Practical settings:
| Setting | Recommendation | Why |
|---|---|---|
reasoning_effort | high for routine tool loops, max for planning | Z.ai recommends max; lower effort trims tokens |
max_tokens | Set it every call | Caps "marathon thinking" |
| Tool count | Send only tools relevant to the task | Smaller prompt, fewer wrong calls |
| Prompt caching | Keep system prompt and tools stable | Cached input is $0.26 vs $1.40 per 1M |
| Timeouts | 120 s or more | 70–80 tokens/s at peak plus thinking |
For a full parameter breakdown, see our guide to GLM 5.4 reasoning effort.
Common GLM 5.4 function calling errors
| Symptom | Likely cause | Fix |
|---|---|---|
| 400 on request | Tool name has spaces or dots | Use letters, digits, _ or - only |
| Model never calls the tool | Vague description or tool_choice forced | Sharpen the description; use "auto" |
| Model repeats a finished call | reasoning_content dropped between turns | Return the full assistant message |
json.loads fails | Parsed streamed arguments mid-stream | Parse after the stream ends |
| Answer cut off after tool result | max_tokens too low for reasoning | Raise the budget |
Testing GLM 5.4 function calling without a Z.ai account
glm5.app exposes glm-5.4 through an OpenAI-compatible endpoint with one API key and prepaid credits. Check the glm5.app API docs for the base URL and supported fields, and the pricing page for credit rates. glm5.app is independent of Z.ai and costs more per token than going direct, so use it for prototyping and move high-volume GLM 5.4 function calling traffic to Z.ai or OpenRouter later.
FAQs
Does GLM 5.4 support function calling?
Yes. GLM 5.4 function calling uses the standard tools array with up to 128 tools per request.
Can GLM 5.4 function calling force a specific tool?
No. tool_choice accepts only "auto". Steer the model with tool descriptions and the system prompt instead.
Do I need tool_stream for GLM 5.4 function calling?
Only when streaming. Z.ai recommends stream: true with tool_stream: true.
Why does GLM 5.4 function calling lose track in multi-step loops?
Usually because reasoning_content was dropped between turns, which is the most common GLM 5.4 function calling bug. GLM 5.4 preserves thinking by default and expects it back unmodified.
Is GLM 5.4 function calling available on OpenRouter?
GLM 5.4 is listed on OpenRouter as z-ai/glm-5.4; check each provider's tool support on the model page.
GLM 5.4 function calling: final take
GLM 5.4 function calling is straightforward once you respect its three rules: auto tool choice only, both streaming flags, and reasoning returned every turn. Get those right and the deeper loop model plans multi-step tool work well. Try GLM 5.4 on glm5.app to sketch your agent before writing the integration.
Sources
- Z.ai Docs: Chat Completion API:
tools,tool_choice,tool_stream, name rules, message roles - Z.ai Docs: Function Calling: tool definitions and flow
- Z.ai Docs: Tool Streaming: streamed tool call deltas
- Z.ai Docs: Thinking Mode: preserved and interleaved thinking
- Z.ai Docs: GLM-5.4: model capabilities and recommended settings
Checked December 3, 2026 against Z.ai's docs.




