官方说明 GLM-5.2 思考默认开启(与 GLM-5.1/5/4.7 一致),提供四种思考形态——默认思考、交错思考(工具调用之间思考)、保留思考(跨轮保留推理内容,clear_thinking: false)、回合级思考(每回合独立开关),并给出"必须把历史 reasoning_content 原样回传"的 Agent 集成关键约束。
适合的任务:构建工具调用型 Agent(需要在每次工具结果后继续推理);追求长会话一致性与缓存命中率的编码/Agent 产品(保留思考);需要按回合精细控制成本与延迟的多轮应用(回合级思考)。
不适合的任务:无法或不愿回传完整 reasoning_content 的会话系统(会破坏保留思考并降低性能与缓存命中);对思考内容有隐私/合规顾虑的转发层(思考默认开启且不可在不改变行为的前提下全局关闭)。
适用的模型版本:GLM-5.2、GLM-5.1、GLM-5、GLM-4.7(思考默认开启);GLM-4.6 为混合思考默认,行为不同。
适用的客户端、Agent 或 API:Z.ai Chat Completions API;GLM Coding Plan 端点(保留思考默认开启);标准 API 端点(保留思考默认关闭,需显式 "clear_thinking": false 开启)。
推荐的推理档位和参数:思考默认开启;thinking.type 支持 enabled / disabled;Agent 场景建议 "clear_thinking": false(保留思考)并回传完整未修改的 reasoning_content;轻量回合可用 thinking.type: disabled 换取更快响应。
"thinking": {
"type": "disabled"
}在编码/Agent 场景下推荐开启;Coding Plan 端点默认开启,标准 API 端点默认关闭。
开启方式(API 端点):"clear_thinking": false。
必须把完整的、未经修改的 reasoning_content 回传给 API;所有连续的 reasoning 块必须与模型最初生成时的顺序完全一致,不能重排或编辑,否则性能下降、缓存命中率受影响。
作用:保留上一轮 assistant 的推理内容到上下文中,维持推理连续性、提升性能、提高缓存命中率从而节省 token。
"""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}")同一会话内每次请求可独立选择开启/关闭思考。
轻量回合(查事实、改措辞)关思考换取更快响应;重任务(复杂规划、多约束推理、代码调试)开思考提升准确性与稳定性。
Agent/工具场景:需要快速执行工具的回合降低推理开销,需要基于工具结果决策的回合加深思考。
多轮中模型保持连贯与一致的输出风格。
官方文档示例代码中的 model="glm-4.7" 为文档通用示例写法,同一页明确说明思考默认开启行为适用于 GLM-5.2 系列;接入 GLM-5.2 时应改为 model="glm-5.2"。
保留思考的"必须原样回传 reasoning_content"约束对转发层/中间缓存是硬性要求,若不满足会直接损害效果。
GLM-5.2