System Prompt 设计模式
10 个可复用的 System Prompt 设计模式
本章总结从各大 AI 厂商 System Prompts 中提炼的 10 个可复用设计模式,帮助你写出专业级的 System Prompt。
模式 1: 身份锚定 (Identity Anchoring)
在开头明确 AI 的身份、能力和限制。
# 模板
You are [角色名称], a [角色类型] specialized in [专业领域].
Your capabilities:
- [能力 1]
- [能力 2]
Your limitations:
- [限制 1]
- [限制 2]
Knowledge cutoff: [日期]
Current date: [动态日期]
实际案例 (Claude Code):
You are an interactive CLI tool that helps users
with software engineering tasks.
Here is useful information about the environment:
<env>
Working directory: [working directory]
Platform: [platform]
Today's date: [date]
</env>
为什么有效:
- 让 AI 明确自己的角色定位
- 减少角色混淆导致的不当回复
- 用户也清楚 AI 能做什么
模式 2: 分层约束 (Layered Constraints)
用优先级标记区分规则的重要性。
# 优先级标记
CRITICAL: [最高优先级,必须遵守]
IMPORTANT: [重要规则]
Note: [一般性建议]
# 或使用紧急程度
NEVER: [绝对禁止]
ALWAYS: [必须执行]
PREFER: [优先选择]
AVOID: [尽量避免]
实际案例 (Claude Code):
IMPORTANT: Refuse to write code that may be used maliciously.
IMPORTANT: You should minimize output tokens as much as possible.
NEVER commit changes unless the user explicitly asks.
为什么有效:
- AI 能理解规则的相对重要性
- 当规则冲突时,知道优先遵守哪条
- 避免"一视同仁"导致的行为偏差
模式 3: Allowed/Not Allowed 列表
用清晰的二分法定义行为边界。
# 模板
## [场景名称] Policy
Allowed:
- [允许的行为 1]
- [允许的行为 2]
Not Allowed:
- [禁止的行为 1]
- [禁止的行为 2]
实际案例 (GPT-4o 图片策略):
Image safety policies:
Not Allowed:
- Giving away the identity of real people in images
- Stating that someone is a public figure
- Classifying human-like images as animals
Allowed:
- OCR transcription of sensitive PII
- Identifying animated characters
为什么有效:
- 边界清晰,不留灰色地带
- 易于维护和更新
- AI 执行起来没有歧义
模式 4: 示例驱动 (Example-Driven)
用具体的 input/output 示例展示期望行为。
# 模板
Examples of appropriate [behavior]:
user: [输入 1]
assistant: [期望输出 1]
user: [输入 2]
assistant: [期望输出 2]
# 对比示例
✅ Correct: [正确做法]
❌ Incorrect: [错误做法]
实际案例 (Claude Code):
Examples of appropriate verbosity:
user: 2 + 2
assistant: 4
user: what command should I run to list files?
assistant: ls
user: what files are in src/?
assistant: [runs ls and sees foo.c, bar.c, baz.c]
foo.c, bar.c, baz.c
为什么有效:
- Few-shot learning 效果显著
- 比抽象描述更直观
- 能精确控制输出风格
模式 5: 工具规范定义 (Tool Specification)
为每个工具提供清晰的接口定义和使用指南。
# 模板
## [工具名称]
Description: [功能描述]
When to use:
- [使用场景 1]
- [使用场景 2]
When NOT to use:
- [不适用场景]
Parameters:
- param1 (required): [描述]
- param2 (optional): [描述]
Example:
[调用示例]
实际案例 (GPT web tool):
## web
Use the `web` tool to access up-to-date information when:
- Local Information: questions about user's location
- Freshness: information that could be outdated
- Niche Information: detailed info not widely known
- Accuracy: when cost of mistake is high
Commands:
- search(): Issues a new query to search engine
- open_url(url: str): Opens the given URL
为什么有效:
- 减少工具误用
- 明确参数类型和必填项
- 帮助 AI 选择最合适的工具
模式 6: 条件分支 (Conditional Branching)
用 if-then 逻辑处理不同场景。
# 模板
When [条件], then [行动]
If [情况 A], do [行动 A]
If [情况 B], do [行动 B]
Otherwise, [默认行动]
实际案例 (Claude 引用规则):
If the search results do not contain relevant information,
politely inform the user that the answer cannot be found
in the search results, and make no use of citations.
If the documents have <document_context> tags,
consider that information when providing answers
but DO NOT cite from the document context.
为什么有效:
- 处理边缘情况
- 逻辑清晰,易于理解
- 减少 AI 的"自由发挥"
模式 7: 格式模板 (Format Templates)
定义输出的结构化格式。
# 模板
Format your response as:
<tag_name>
[内容]
</tag_name>
# 或
Return your answer in this format:
```json
{
"field1": "value",
"field2": "value"
}
```
**实际案例 (Claude Code 文件路径提取)**:
Format your response as:
If no files are read or modified, return empty tags:
Do not include any other text in your response.
**为什么有效**:
- 便于程序解析
- 输出一致性高
- 减少后处理工作
---
## 模式 8: 负面约束 (Negative Constraints)
明确说明"不要做什么"。
```markdown
# 模板
Do NOT:
- [禁止行为 1]
- [禁止行为 2]
NEVER:
- [绝对禁止 1]
- [绝对禁止 2]
AVOID:
- [尽量避免 1]
- [尽量避免 2]
实际案例 (Claude Code):
IMPORTANT: You should NOT answer with unnecessary
preamble or postamble, unless the user asks you to.
You MUST avoid text before/after your response, such as:
- "The answer is <answer>."
- "Here is the content of the file..."
- "Based on the information provided..."
- "Here is what I will do next..."
为什么有效:
- 消除常见的不良行为
- 比正面描述更直接
- "不做什么"往往比"做什么"更重要
模式 9: 上下文注入 (Context Injection)
动态注入运行时信息。
# 模板
<context>
Current user: [用户信息]
Session info: [会话信息]
Available tools: [可用工具列表]
</context>
# 或使用变量占位符
Working directory: [working_directory]
Platform: [platform]
Today's date: [current_date]
实际案例 (Claude Code):
<env>
Working directory: /Users/john/project
Is directory a git repo: Yes
Platform: darwin
Today's date: 2025-01-15
Model: claude-sonnet-4
</env>
为什么有效:
- 让 AI 感知运行环境
- 动态调整行为
- 提供必要的上下文信息
模式 10: 迭代改进指引 (Iterative Improvement)
指导 AI 如何逐步改进输出。
# 模板
If [初始尝试失败], then:
1. [调整策略 1]
2. [调整策略 2]
3. If still fails, [兜底策略]
# 或
After completing [任务], verify by:
- [验证步骤 1]
- [验证步骤 2]
If verification fails, [修正策略]
实际案例 (Claude Code 任务流程):
For software engineering tasks:
1. Use search tools to understand the codebase
2. Implement the solution using all tools available
3. Verify the solution with tests
4. VERY IMPORTANT: Run lint and typecheck commands
If unable to find the correct command,
ask the user and proactively suggest writing
it to CLAUDE.md for next time.
为什么有效:
- 提供失败恢复策略
- 鼓励验证和自检
- 形成闭环的工作流
实战练习
练习 1: 设计一个客服 Bot 的 System Prompt
要求:
- 使用至少 5 种设计模式
- 包含身份定义、行为约束、工具使用
- 处理敏感话题的边界
练习 2: 优化现有 Prompt
找一个你正在使用的 Prompt,用以上模式重构,对比效果差异。
练习 3: 逆向分析
选择一个 AI 产品(如 Perplexity、Notion AI),通过交互推测其 System Prompt 设计。
🎯 记住:好的 System Prompt 不是一次写成的,而是通过不断测试、观察 AI 行为、迭代优化得来的。把这 10 个模式当作你的工具箱,根据实际需求灵活组合。