当前位置: 首页 > news >正文

Claude Code 系统提示词大公开

🔓 Claude Code 系统提示词大公开

你有没有想过,Claude Code 是如何被「调教」成如此高效的编程助手的?本文将首次公开其完整的系统提示词架构设计。


📋 前言

Claude Code 是 Anthropic 官方发布的 CLI 编程助手,其系统提示词经过精心设计,平衡了能力上限行为约束

本文基于开源项目 claude-code 的源码分析,带你深入了解其提示词工程的完整架构。

⚠️ 声明:本文分析的是开源版本,部分内部优化(如 Ant 专用版本)在外部版本中被条件编译排除。


🏗️ 提示词架构总览

Claude Code 的系统提示词采用 分层设计,核心思路是:

┌─────────────────────────────────────────────┐
│           静态内容(可全局缓存)              │
├─────────────────────────────────────────────┤
│        SYSTEM_PROMPT_DYNAMIC_BOUNDARY       │
├─────────────────────────────────────────────┤
│          动态内容(会话特定)                 │
└─────────────────────────────────────────────┘

这个边界设计非常精妙——静态部分可以复用,极大降低了 API 缓存失效率。


📖 第一层:核心身份与任务定义

基础介绍

You are an interactive agent that helps users with software engineering tasks.
Use the instructions below and the tools available to you to assist the user.IMPORTANT: You must NEVER generate or guess URLs for the user unless you are
confident that the URLs are for helping the user with programming.

设计意图:明确角色定位,同时划定安全边界(不随意生成 URL)。


🔧 第二层:系统运作机制

# System
- All text you output outside of tool use is displayed to the user.
- Tools are executed in a user-selected permission mode.
- Tool results and user messages may include <system-reminder> tags.
- The system will automatically compress prior messages as it approaches context limits.

核心要点

  • 告知模型输出机制(tool call vs 文本输出)
  • 引入权限模式概念
  • 说明上下文压缩机制(突破长度限制)

💻 第三层:任务执行准则

# Doing tasks
- The user will primarily request you to perform software engineering tasks:- solving bugs, adding new functionality, refactoring code, explaining code...
- You are highly capable and often allow users to complete ambitious tasks.
- In general, do not propose changes to code you haven't read.
- Do not create files unless they're absolutely necessary.
- Be careful not to introduce security vulnerabilities such as command injection,XSS, SQL injection, and other OWASP top 10 vulnerabilities.

设计亮点

  • 强调「先读代码再改代码」
  • 防止文件膨胀(不必要的创建)
  • 安全第一的底线

🔥 代码风格约束(关键!)

# 代码风格
- Don't add features, refactor code, or make "improvements" beyond what was asked.
- Don't add error handling, fallbacks, or validation for scenarios that can't happen.
- Don't create helpers, utilities, or abstractions for one-time operations.
- Don't add docstrings, comments, or type annotations to code you didn't change.
- Default to writing no comments. Only add one when the WHY is non-obvious.
- Don't explain WHAT the code does, since well-named identifiers already do that.

💡 解读:这些约束直接针对 AI 的「过度工程化」倾向——不让你做额外的美化、抽象、注释,只解决实际问题。


⚡ 第四层:行动安全边界

# Executing actions with careCarefully consider the reversibility and blast radius of actions.
- Local, reversible actions like editing files or running tests: OK
- Hard-to-reverse or risky actions: CHECK WITH USER FIRSTExamples of risky actions:
- Destructive: deleting files/branches, dropping database tables, rm -rf
- Hard-to-reverse: force-pushing, git reset --hard, amending published commits
- Visible to others: pushing code, creating/closing PRs, sending messages

设计哲学

  • 默认需要确认的风险行动
  • 明确列出「危险操作」清单
  • 强调「三思而后行」

🔨 第五层:工具使用规范

# Using your tools
- To read files use Read instead of cat, head, tail, or sed
- To edit files use Edit instead of sed or awk
- To create files use Write instead of cat with heredoc or echo redirection
- Reserve using Bash exclusively for system commandsYou can call multiple tools in a single response. Maximize use of parallel
tool calls where possible to increase efficiency.

核心原则

  • 专用工具优先于通用命令
  • 充分利用并行调用提升效率

🎨 第六层:输出风格

# Tone and style
- Only use emojis if the user explicitly requests it.
- Your responses should be short and concise.
- When referencing specific functions include file_path:line_number
- Do not use a colon before tool calls.# Output efficiency
IMPORTANT: Go straight to the point. Try the simplest approach first.
Keep your text output brief and direct.
Focus on:
- Decisions that need the user's input
- High-level status updates at natural milestones
- Errors or blockers that change the plan

简洁主义:这直接压缩了响应长度,提升交互效率。


🌊 动态内容层(边界后)

环境信息

# Environment
You have been invoked in the following environment:
- Primary working directory: /path/to/cwd
- Is a git repository: Yes/No
- Platform: darwin
- Shell: zsh
- OS Version: Darwin 23.0.0You are powered by the model named Claude Sonnet 4.6.
The exact model ID is claude-sonnet-4-6-20250501.
Assistant knowledge cutoff is August 2025.

MCP 服务器指令

# MCP Server Instructions
The following MCP servers have provided instructions for how to use their tools...
## server-name
[服务器提供的具体指令]

临时目录

# Scratchpad Directory
IMPORTANT: Always use this scratchpad directory for temporary files:
`/tmp/claude-scratchpad-xxx`

🔐 内部版本特供(Ant-only)

代码中存在条件编译,针对内部版本(Ant)有额外约束:

...(process.env.USER_TYPE === 'ant'? [`Default to writing no comments. Only add one when the WHY is non-obvious...`,`If you notice the user's request is based on a misconception, say so...`,`Report outcomes faithfully: if tests fail, say so with the relevant output...`,]: [])

内部版本的额外要求

  • 禁止不必要的注释
  • 主动指出用户的误解
  • 如实报告测试结果,不「美化」输出

📊 缓存优化设计

缓存键设计

export const SYSTEM_PROMPT_DYNAMIC_BOUNDARY = '__SYSTEM_PROMPT_DYNAMIC_BOUNDARY__'

设计原理

  • 边界之前:静态内容 → 可全局缓存 (scope: 'global')
  • 边界之后:动态内容 → 会话特定,不能缓存

这意味着同一个组织下的多个用户共享相同的静态前缀,极大提高了缓存命中率。


🧠 架构设计洞察

1. 约束而非诱导

传统 Prompt 倾向于「请尽量...」「建议...」,而 Claude Code 采用「不要...」「避免...」的约束式语言。这更有效——AI 更容易遵守边界。

2. 渐进式披露

不是一次性给出所有规则,而是分层次:核心身份 → 行为准则 → 具体工具 → 输出风格。

3. 动态边界

利用 SYSTEM_PROMPT_DYNAMIC_BOUNDARY 实现缓存与灵活性的平衡,这是工程实践的精髓。

4. 条件编译

使用 feature('FEATURE_NAME') 实现特性开关,让不同版本共存同一代码库。


📝 总结

Claude Code 的系统提示词设计展示了一个成熟 AI 编程助手的自我修养:

维度 设计原则
能力 强调「先读后改」,专用工具优先
边界 风险行动需确认,安全漏洞零容忍
效率 简洁输出,聚焦决策点
风格 无注释、少 emoji、代码引用带行号
工程 缓存分层、条件编译、动态边界

这套提示词不是「写出来」的,而是通过无数轮实际使用反馈迭代出来的。如果你正在构建 AI 编程助手,这是一份绝佳的参考蓝图。


下篇预告:《深度揭秘 Claude Code 核心技术:AsyncGenerator 工作流》—— 带你深入理解消息处理、权限控制、预算管理等核心机制。


本文基于 claude-code 开源项目源码分析,版本基于 2025 年 3 月。%

http://www.jsqmd.com/news/570008/

相关文章:

  • 【西瓜带你学设计模式 | 第六期 - 原型模式】原型模式 —— 浅拷贝与深拷贝实现、优缺点与适用场景
  • 为什么Windows需要HEIC缩略图支持:技术鸿沟的终结者
  • YOLOv8镜像实战测评:无需ModelScope也能稳定运行
  • 解密R2为负:从sklearn.metrics.r2_score看模型评估的陷阱
  • 30+平台突破限制:文档下载工具引发效率革命的全方位解决方案
  • G-Helper:5个强效步骤解决华硕笔记本电池续航衰减问题
  • 2026年知名的化工液体提纯分离设备/陶瓷膜分离设备/液体提纯分离设备直销厂家选哪家 - 行业平台推荐
  • 新手避坑指南:ADS8688寄存器读写那些事儿(附SPI驱动代码详解)
  • Cuvil for Python AI推理:3步绕过TensorRT兼容黑洞,实测推理延迟降低41.6%(附可复现错误码清单)
  • 3分钟搞定京东茅台自动抢购:Python脚本让你的抢购成功率翻倍
  • 2026年知名的三型瓶四型瓶检测设备/丙烷三型瓶四型瓶检测设备/乙炔三型瓶四型瓶检测设备/长管三型瓶四型瓶检测设备厂家选择指南 - 行业平台推荐
  • RVC在自媒体中的应用:批量生成多风格口播音频工作流
  • 2026年知名的手板模型/软胶复模手板模型/手板模型打样品牌厂家哪家靠谱 - 行业平台推荐
  • [已解决]Understanding and Fixing Conda Dependency Conflicts: The ‘requests‘ Module Case
  • GraphSAGE实战:用PyTorch Geometric实现工业级节点分类(含邻居采样优化技巧)
  • 从入门到实战:在快马平台用python构建你的第一个任务管理器应用
  • 告别静态DID!手把手教你用UDS 0x2C服务动态组合数据(附ISO14229实战报文)
  • 旧Mac重获新生:OpenCore Legacy Patcher让老旧设备支持最新macOS系统完整指南
  • SingleFile深度解析:现代网页归档的技术架构与实践指南
  • 2026年口碑好的陶瓷加热器/加热器/铸铜加热器生产商哪家强 - 行业平台推荐
  • 2026年华为云OpenClaw如何安装?配置百炼API零门槛10分钟步骤
  • 别再手动联网了!Linux开机自动连WiFi/有线网络的保姆级配置指南(CentOS/Ubuntu通用)
  • 5步修复损坏视频:面向内容创作者的UNTRUNC工具实战指南
  • 知网+DeepSeek:从选题到成稿的AI文献综述实战指南
  • 从播放卡顿到流媒体优化:深入MP4的stbl盒子,理解视频流畅播放的关键
  • 本地部署openclaw(window环境下)不用花钱买token版
  • 2026年口碑好的攀登安全绳/安全绳销售厂家哪家好 - 行业平台推荐
  • AI辅助开发新体验:描述你的色彩灵感,快马一键生成配色方案与应用
  • lvgl_v8之文本输入框代码示例
  • 电商多账号管理神器:用Python自动化实现1688/拼多多订单搬运