系统提示词
系统提示词不是单个静态字符串。它由 src/constants/prompts.ts 中的 getSystemPrompt() 组装,然后由 buildEffectiveSystemPrompt() 处理。它具有最多 6 个层级的优先级层次结构和 7 个独立的内容部分,由缓存边界分隔。
优先级层次结构
层级按优先级顺序评估。更高层级替换其下所有内容。 在主动模式下,代理提示词是追加而不是替换默认值。
简单模式
如果设置了 CLAUDE_CODE_SIMPLE 环境变量,
整个系统提示词会折叠为三行:
You are Claude Code, Anthropic's official CLI for Claude.
CWD: /path/to/cwd
Date: 2026-03-31
缓存边界
标记 __SYSTEM_PROMPT_DYNAMIC_BOUNDARY__ 将提示词
分为两半,具有不同的缓存行为。动态部分使用注册表缓存计算值,
直到 /clear 或
/compact。
例外:MCP 指令使用 DANGEROUS_uncachedSystemPromptSection(),因为 MCP 服务器可以在轮次之间连接/断开。
可跨组织全局缓存。部分:
- 1 Intro
- 2 System
- 3 Doing Tasks
- 4 Executing Actions with Care
- 5 Using Tools
- 6 Tone and Style
- 7 Output Efficiency
会话特定。不可全局缓存。部分:
- · Session-specific guidance
- · Memory (MEMORY.md + memory files)
- · Environment (cwd, platform, model)
- · Language preference
- · Output Style (if configured)
- · MCP Instructions (always uncached)
- · Scratchpad directory (if enabled)
- · Function Result Clearing (if CACHED_MICROCOMPACT)
7 个静态内容部分
这些部分构成默认提示词。标记为 [ANT-ONLY] 的项目仅对内部 Anthropic 用户显示。 标记为 [EXT-ONLY] 的项目仅对外部用户显示。
Intro
Defines Claude as an interactive software engineering agent. Includes the security policy (authorized testing / CTF / defensive use OK; destructive / DoS / mass targeting refused) and the no-URL-guessing rule.
System
Output rendering rules (GitHub-flavored markdown, monospace). Permission mode mechanics. How to handle denied tool calls, <system-reminder> tags, prompt injection in tool results, hooks, and context compression.
Doing Tasks
Engineering task rules: read before proposing changes, prefer editing over creating, no time estimates, diagnose failures before switching tactics, no extras beyond what was asked, no premature abstractions, no backwards-compat hacks. Several rules are ANT-ONLY: spot adjacent bugs, faithful outcome reporting, comment hygiene, verify before claiming done.
Executing Actions with Care
Risk classification for actions. Local reversible actions (file edits, tests) are free. Hard-to-reverse or shared-state actions (force push, prod changes, sending messages, uploads to third-party tools) require confirmation. "Measure twice, cut once" principle.
Using Tools
Dedicated tool preference (Read over cat, Grep over grep, etc.). TodoWrite for task management. Parallel tool calls where independent.
Tone and Style
No emojis. Short and concise responses [EXT-ONLY]. file_path:line_number format for code citations. owner/repo#123 for GitHub references. No colon before tool calls.
Output Efficiency
External: lead with action, skip preamble, one sentence over three, no code or tool-call restrictions. ANT (internal): flowing prose, no fragments/em-dashes/symbols, inverted pyramid, explain for the reader's level, never suppress failures or oversell wins.
重要的动态部分
| Section | When present | What it does |
|---|---|---|
| 会话指导 | 始终 | 拒绝工具调用的规则、shell 命令的 ! 前缀、何时使用 Agent vs Glob/Grep、技能调用。 |
| 语言 | 已配置语言 | 始终用 {LANGUAGE} 响应。技术术语和标识符保持原始形式。 |
| 输出样式 | 已配置样式 | 内置样式:default(无)、Explanatory(添加 "Insights")、Learning(要求用户编写代码片段)。 |
| 长度锚点 | 仅 ANT 用户 | 硬限制:工具调用之间 ≤25 词,最终响应 ≤100 词,除非任务需要更多。 |
| Token 预算 | TOKEN_BUDGET 标志 | 显示每轮输出 token 计数。指示 Claude 持续工作直到接近用户指定的 token 目标。 |
| 总结工具结果 | 始终 | 从工具结果中记录重要信息,因为旧结果会从上下文中清除。 |
提示词中的关键行为规则
这些规则硬编码在默认提示词中。了解它们有助于你预测和使用 Claude 的默认行为。
| Rule | What it means for you |
|---|---|
| 无 emoji | 除非你明确要求,否则 Claude 不会使用 emoji。 |
| 无额外功能 | Claude 不会添加超出要求的重构、文档注释或改进。设计上是有意为之。 |
| 无过早抽象 | "三行相似代码优于过早抽象。" Claude 默认使用简单、直接的代码。 |
| 无时间估计 | Claude 不会预测任务需要多长时间,无论是对自己还是对你。 |
| 使用专用工具 | Claude 被明确告知使用 Read 而不是 cat,Grep 而不是 grep 等。Bash 用于没有专用工具的操作。 |
| 代码引用 | 代码引用使用 file_path:line_number 格式以便导航。 |
| GitHub 引用 | Issue 和 PR 引用为 owner/repo#123,以便渲染为可点击链接。 |
影响提示词的功能标志
| Flag | Effect on system prompt |
|---|---|
| PROACTIVE / KAIROS | 用自主模式提示词替换默认提示词。添加 tick-loop 行为和 SleepTool 要求。 |
| CACHED_MICROCOMPACT | 添加"函数结果清除"部分:保留 N 个最近的工具结果,清除更早的结果。 |
| TOKEN_BUDGET | 添加 token 预算指令。显示每轮输出计数并强制执行用户指定的最小 token 目标。 |