GLM 5.2 System Prompt 指南:编程、分析与写作模板
Jul 20, 2026

GLM 5.2 System Prompt 指南:编程、分析与写作模板

System prompt 决定了 GLM 5.2 一切输出的框架。本文提供经过验证的代码生成、文档分析与结构化抽取模板,附 token 效率技巧。

GLM 5.2 产生的每一份输出,都由用户消息之前的内容塑形。含糊的 system prompt 产出含糊的输出;精准框架化的 system prompt 则锁定角色、格式与语气——而且因为 GLM 5.2 以每百万 token $0.26 的价格缓存 prompt 前缀,一条长而稳定的 system prompt 在首次调用之后几乎就免费了。本指南为 system prompt 设计杠杆最大的三类工作负载提供可直接复制粘贴的模板:代码生成、文档分析与结构化写作。

为什么 system prompt 对 GLM 5.2 更重要

GLM 5.2 是一款 753B 参数的混合专家(MoE)模型,拥有 1M token(1,048,576)上下文窗口。这个上下文规模以实际方式改变了 system prompt 的算盘:你可以把整本风格指南、代码库片段、API 参考或知识库直接加载进 system prompt,让它在每一轮对话中都可用。

把内容前置加载进 system prompt 的经济理由在于缓存命中价格。标准输入定价是每百万 token $1.40。缓存前缀命中降到每百万 $0.26——81% 的折扣。规则是:一切稳定的内容放进 system prompt;一切动态的内容放进用户消息。

Token 类型每百万价格
输入(未命中缓存)$1.40
缓存命中$0.26
输出$4.40

以每秒 158 token 的速度——据 Artificial Analysis,在前沿模型中位列第三快——GLM 5.2 的补全返回也很快,所以即使是长 system prompt 也不会带来恼人的延迟。TTFT 为 1.54 秒。

架构说明:GLM 5.2 处理什么

GLM 5.2 仅支持文本。它不接受图像、音频或视频输入。如果你的工作流涉及多模态输入,请把这些路由到能处理它们的模型;让 GLM 5.2 负责文本推理和生成步骤。

该模型使用 Transformer decoder,78 层、每层 256 个专家、每次前向传播激活 8 个(混合专家,DSA 注意力)。这个架构意味着相对计算成本而言极高的容量——这就是为什么模型能在 GPQA Diamond 拿 89%、SWE-bench Pro 拿 62.1%、HumanEval 拿 90%+,同时把输出定价保持在有竞争力的 $4.40 每百万 token。

理解架构对 system prompt 设计很重要:模型在大规模上有很强的指令遵循能力,所以 system prompt 里明确的格式约束会被可靠地遵守,而不是被当作建议。

核心原则:角色 + 格式 + 约束

每条有效的 GLM 5.2 system prompt 都包含三个要素:

  1. 角色:模型扮演谁,具体程度要足以收窄行为。
  2. 格式:输出应该长什么样——代码围栏、JSON schema、字数、标题结构。
  3. 约束:模型不该做什么——不要开场白、不要道歉、不要多余的说明。

省略任何一项都会制造歧义,既烧 token,又会在规模化时产出不一致的结果。

模板 1:代码生成

用于生成函数、模块、测试或重构。关键约束是「只输出代码」——没有它,GLM 5.2 经常会附带解释性文字,既消耗输出 token,又需要后处理来剥离。

You are a senior software engineer specializing in [language/framework].

Rules:
- Output ONLY code in markdown fences with the correct language tag.
- Do not include explanations, comments, or preamble unless the user explicitly asks.
- If the request is ambiguous, ask one clarifying question before writing code.
- Match the style of any code the user provides (indentation, naming conventions, idioms).
- When writing tests, use [pytest/jest/go test] and cover edge cases.

If the user says "explain", add a brief comment block above each logical section.

对于 GLM 5.2 要调用工具的智能体编程工作流,用显式的工具清单扩展这个模板。参见 GLM 5.2 概览

You are a senior software engineer. You have access to these tools:
- bash(command: str) — runs a shell command, returns stdout/stderr
- read_file(path: str) — returns file contents
- write_file(path: str, content: str) — writes content to disk

Always read a file before editing it. Never assume file contents.
Output tool calls as JSON: {"tool": "bash", "args": {"command": "..."}}

SWE-bench Pro 的 62.1% 反映了真实的软件工程能力——system prompt 中显式的工具声明正是支撑这一表现的因素之一。

模板 2:文档分析与结构化抽取

对抽取类任务——解析合同、摘要报告、从非结构化文本中抽出结构化数据——system prompt 最重要的要素是输出 schema。当 GLM 5.2 确切知道输出必须是什么形状时,它不会发明字段,也不会漏掉必填项。

You are a data analyst specializing in document extraction.

Rules:
- Think step by step before outputting your final answer.
- Output ONLY valid JSON matching this exact schema:
  {
    "summary": string,          // 2-3 sentence summary
    "key_entities": string[],   // named people, orgs, places
    "dates": string[],          // all dates in ISO 8601 format
    "action_items": string[],   // any explicit tasks or commitments
    "risk_flags": string[]      // any ambiguous, contradictory, or missing information
  }
- If a field has no data, output an empty array or empty string — never omit the key.
- Do not include any text outside the JSON object.

对希望最终答案之前可见推理过程的分析任务,加一条草稿区指令:

You are a senior analyst. Before answering, think through the problem inside <thinking>...</thinking> tags. After closing the thinking tags, output your final answer in the requested format. The user will only see what comes after </thinking>.

这个模式在调试「模型为什么给出某个答案」时很有用,因为推理过程保留在响应中,展示前可以剥离。

模板 3:技术写作

用于生成文档、API 参考、博客文章或结构化报告。关键变量是受众、语气和长度上限。没有长度上限,模型往往会过度生成。

You are a technical writer. Your audience is professional software developers.

Rules:
- Tone: direct, concrete, and precise. No filler phrases ("In conclusion", "It is worth noting").
- Length: stay under [word count] words unless the user specifies otherwise.
- Structure: use markdown headings (##, ###), bullet lists for items, and code blocks for all code.
- Do not use passive voice when active voice is clearer.
- If the user provides existing text to edit, preserve their terminology unless it is factually wrong.
- Never add a "Summary" section unless the user asks for one.

对本地化写作或品牌声量需求,用 1M 上下文窗口把完整风格指南嵌进去:

You are a technical writer following the style guide below.

[STYLE GUIDE — paste entire document here, up to several hundred pages]

Rules:
- Follow the style guide exactly.
- If the user's request conflicts with the style guide, follow the guide and note the conflict.
- Output in markdown.

因为风格指南是稳定的前缀,同一会话内的后续调用会以 $0.26/M 命中缓存,而不是 $1.40/M。

模板 4:函数调用与工具使用

GLM 5.2 支持函数调用。要可靠地进行工具分发,就在 system prompt 里显式列出可用函数及其签名和用途。「什么时候该调用工具、什么时候直接回答」的歧义,是智能体系统最常见的失败来源之一。

You are an assistant with access to the following tools. Use them when the user's request requires real-time data, file access, or computation.

Tools:
- search(query: str) -> list[dict]: Search the web. Returns a list of {title, url, snippet} objects. Use when the user asks about recent events or facts you may not have.
- calculator(expression: str) -> float: Evaluate a mathematical expression. Use for any numeric computation.
- get_user_data(user_id: str) -> dict: Retrieve user account data. Use only when the user explicitly asks about their account.

Rules:
- Call tools only when necessary. Do not call search for questions you can answer directly.
- Always explain your reasoning before calling a tool.
- After receiving tool output, summarize the key information before responding.
- If a tool returns an error, tell the user and ask how to proceed.

缓存策略:什么放哪里

1M 上下文窗口让人想什么都往 system prompt 里塞。正确的划分依据是稳定性,而不是大小。

内容类型放在原因
角色定义System prompt会话内从不变化
输出格式 / schemaSystem prompt所有调用中保持稳定
风格指南或参考文档System prompt静态内容,首次调用后即命中缓存
当前用户查询用户消息每轮都变
动态数据(今天日期、用户 ID)用户消息可变的,无法缓存
对话历史消息数组由 API 管理
一次性指令用户消息不值得缓存

缓存命中 $0.26/M 对比未缓存输入 $1.40/M,一条 10,000 token 的 system prompt 加载 1,000 次,缓存命中成本 $2.60,而未缓存是 $14.00——仅这一条 prompt 就省了 $11.40。规模化之后,这个数字会显著放大。

常见问题

问题可能原因修复
模型无视格式指令格式规则埋在长段文字里把格式规则移到最前面;用编号列表
JSON 输出带多余文字没有「只输出 JSON」约束加「JSON 对象之外不要有任何文字」
代码输出夹带多余解释缺少「只输出代码」规则加显式约束;加「除非要求否则不解释」
不必要地调用工具工具触发条件含糊给每个工具加显式「仅在……时使用」
输出过长没有长度约束加字数限制或带具体目标的「简明扼要」
跨轮次风格漂移风格规则太含糊加反面示例(「不要用被动语态」)

常见问题

GLM 5.2 能可靠遵循 system prompt 指令吗?

能,只要指令明确。GLM 5.2 是训练来遵循详细指令的 753B 参数模型。含糊的指令会产生多变的结果;具体、祈使式的指令——「只输出有效 JSON」「不要开场白」——会被一致地遵守。

system prompt 可以多长?

总上下文窗口是 1,048,576 token。理论上 system prompt 可以吃掉其中很大一部分,但实操上,核心指令区要紧凑(500 token 以内),窗口的大部分留给代码库、文档或风格指南这类参考资料。指令区越简洁、越靠前,模型的关注就越可靠。

缓存对部分匹配的 system prompt 生效吗?

缓存命中只针对 prompt 开头开始的精确前缀匹配。如果你在缓存部分之前改了任何内容,就按缓存未命中计费。这正是动态内容——时间戳、用户 ID、每调用变量——应该永远放在用户消息、而不是 system prompt 里的原因。

我能通过 OpenRouter 使用 GLM 5.2 的 system prompt 吗?

可以。GLM 5.2 在 OpenRouter 上以 z-ai/glm-5.2 提供。system prompt 字段的行为完全相同。直接 API 访问时,通过 Z.ai 端点使用 base_url=https://api.z.ai/v1 和模型 glm-5.2

专门针对编程任务,最好的 system prompt 是什么?

从本文的模板开始:资深工程师角色、只输出代码规则、一条澄清问题政策。要匹配 62.1% SWE-bench Pro 基准的智能体编程,加上显式工具清单和文件操作的「先读后写」指令。

system prompt 里应该放示例吗?

system prompt 中的少样本示例会被缓存并复用,在规模化时成本效益很高。对抽取或结构化输出这类格式敏感的任务,一两个精心挑选的示例往往胜过几段指令文字。对简单任务,保持 system prompt 只有指令。

怎么调试一个不工作的 system prompt?

临时加一条草稿区指令(<thinking>...</thinking> 标签)看看模型的推理。这能揭示模型是误解了任务、忽略了某条具体规则,还是推理正确但格式错了。定位之后,只修那条失败的规则,而不是重写整个 prompt。

下一步

上面的模板是起点。把其中具体的语言、schema 或风格约束换成你的工作流需要的,适配到你的技术栈。生产使用时:

  1. 把 system prompt 固化为版本控制中的稳定字符串。
  2. 通过 API usage 响应字段测量缓存命中率,验证缓存是否生效。
  3. 部署改动前先用一个小型评估集跑一遍输出质量——system prompt 的改动可能对边界情况产生不明显的下游影响。
  4. 对函数调用工作流,GLM 5.2 的 1M 上下文如何支持加载完整 API schema——见 GLM 5.2 API 指南

试用 GLM 5.2——无需 API key:glm5.app/chat

来源

Start Using GLM 5 Today

Try GLM 5 free — reasoning, coding, agents, and image generation in one platform.