The official documentation states that thinking is enabled by default for GLM-5.2 (as with GLM-5.1/5/4.7), and provides four thinking modes: default thinking, interleaved thinking (thinking between tool calls), preserved thinking (retaining reasoning content across turns with clear_thinking: false), and turn-level thinking (an independent switch for each turn). It also highlights a key constraint for Agent integrations: historical reasoning_content must be returned unchanged.
Suitable tasks: building tool-calling Agents (which need to continue reasoning after each tool result); coding/Agent products that prioritize consistency in long sessions and cache hit rates (preserved thinking); multi-turn applications that need fine-grained per-turn control over cost and latency (turn-level thinking).
Unsuitable tasks: session systems that cannot or do not want to return complete reasoning_content (this will break preserved thinking and reduce performance and cache hits); forwarding layers with privacy or compliance concerns about thinking content (thinking is enabled by default and cannot be globally disabled without changing behavior).
Supported model versions: GLM-5.2, GLM-5.1, GLM-5, and GLM-4.7 (thinking enabled by default); GLM-4.6 uses hybrid thinking by default and behaves differently.
Supported clients, Agents, or APIs: the Z.ai Chat Completions API; the GLM Coding Plan endpoint (preserved thinking enabled by default); standard API endpoints (preserved thinking disabled by default and must be explicitly enabled with "clear_thinking": false).
Recommended reasoning mode and parameters: thinking enabled by default; thinking.type supports enabled / disabled; for Agent scenarios, use "clear_thinking": false (preserved thinking) and return the complete, unmodified reasoning_content; for lightweight turns, use thinking.type: disabled for faster responses.
"thinking": {
"type": "disabled"
}Recommended for coding/Agent scenarios; enabled by default for Coding Plan endpoints and disabled by default for standard API endpoints.
How to enable it (API endpoints): "clear_thinking": false.
The complete, unmodified reasoning_content must be returned to the API; all consecutive reasoning blocks must remain in exactly the same order in which the model originally generated them and must not be reordered or edited. Otherwise, performance will decline and cache hits will be affected.
Effect: retains the previous assistant turn's reasoning content in the context, maintains reasoning continuity, improves performance, and increases cache hits to save tokens.
"""Interleaved Thinking + Tool Calling Example"""
import json
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.z.ai/api/paas/v4/",
)
tools = [{"type": "function", "function": {
"name": "get_weather",
"description": "Get weather information",
"parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
}}]
messages = [
{"role": "system", "content": "You are an assistant"},
{"role": "user", "content": "What's the weather like in Beijing?"},
]
# Round 1: the model reasons and then calls a tool
response = client.chat.completions.create(model="glm-5.2", messages=messages, tools=tools, stream=True, extra_body={
"thinking":{
"type":"enabled",
"clear_thinking": False # False for Preserved Thinking
}})
reasoning, content, tool_calls = "", "", []
for chunk in response:
delta = chunk.choices[0].delta
if hasattr(delta, "reasoning_content") and delta.reasoning_content:
reasoning += delta.reasoning_content
if hasattr(delta, "content") and delta.content:
content += delta.content
if hasattr(delta, "tool_calls") and delta.tool_calls:
for tc in delta.tool_calls:
if tc.index >= len(tool_calls):
tool_calls.append({"id": tc.id, "function": {"name": "", "arguments": ""}})
if tc.function.name:
tool_calls[tc.index]["function"]["name"] = tc.function.name
if tc.function.arguments:
tool_calls[tc.index]["function"]["arguments"] += tc.function.arguments
print(f"Reasoning: {reasoning}\nTool calls: {tool_calls}")
# Key: return reasoning_content to keep the reasoning coherent
messages.append({"role": "assistant", "content": content, "reasoning_content": reasoning,
"tool_calls": [{"id": tc["id"], "type": "function", "function": tc["function"]} for tc in tool_calls]})
messages.append({"role": "tool", "tool_call_id": tool_calls[0]["id"],
"content": json.dumps({"weather": "Sunny", "temp": "25°C"})})
# Round 2: the model continues reasoning based on the tool result and responds
response = client.chat.completions.create(model="glm-5.2", messages=messages, tools=tools, stream=True, extra_body={
"thinking":{
"type":"enabled",
"clear_thinking": False # False for Preserved Thinking
}})
reasoning, content = "", ""
for chunk in response:
delta = chunk.choices[0].delta
if hasattr(delta, "reasoning_content") and delta.reasoning_content:
reasoning += delta.reasoning_content
if hasattr(delta, "content") and delta.content:
content += delta.content
print(f"Reasoning: {reasoning}\nReply: {content}")Within the same session, each request can independently choose whether to enable or disable thinking.
For lightweight turns (fact lookup, wording edits), disable thinking for faster responses; for demanding tasks (complex planning, multi-constraint reasoning, code debugging), enable thinking for greater accuracy and stability.
Agent/tool scenarios: reduce reasoning overhead for turns that need to execute tools quickly, and deepen thinking for turns that need to make decisions based on tool results.
Across multiple turns, the model maintains coherent, consistent output style.
The example code in the official documentation uses model="glm-4.7" as a generic documentation example; the same page clearly states that the default-enabled thinking behavior applies to the GLM-5.2 series. When integrating GLM-5.2, change it to model="glm-5.2".
The requirement to return reasoning_content verbatim for preserved thinking is a hard requirement for forwarding layers and intermediate caches. Failure to meet it will directly harm performance.
GLM-5.2