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

Claude Code 系统提示词

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

前言:

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

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

提示词架构总览

┌─────────────────────────────────────────────┐
│           静态内容(可全局缓存)              │
├─────────────────────────────────────────────┤
│        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.

 第二层:系统运作机制

# 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)

...(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 编程助手的自我修养:

1d5fc079-8f5d-49bb-b4b3-8cb1a12e8e17

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

出自原文: https://www.cnblogs.com/lingli-meng/p/19804968  ,仅供学习

 

 

  

 

  

  

  

  

  

  

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

相关文章:

  • Cortex-M能否运行Linux?架构与系统需求解析
  • Pixel Couplet Gen惊艳效果:像素气球爆炸后浮现隐藏彩蛋(如马年生肖动画)
  • 谁懂啊!闲置大润发购物卡变现,居然能这么省心 - 团团收购物卡回收
  • 终极指南:使用QMCDecode免费解锁QQ音乐加密格式的完整解决方案
  • IMX6ULL开发板DDR初始化参数修改实战:从uboot源码到烧写验证
  • 跨平台运行新范式:APK Installer实现Windows直接运行安卓应用的性能优化方案
  • 愤怒的小鸟下载
  • 别再为PT100测温头疼了!手把手教你用ADS1220搞定高精度温度采集(附STM32代码)
  • 2026年国内优质的工业厂房搭建源头厂家选哪家,做工业厂房/工业厂房搭建/搭建工业厂房,工业厂房搭建实力厂家找哪家 - 品牌推荐师
  • Kafka 怎么保证消息的顺序性
  • 2026年4月AI教育品牌测评:中高考冲刺提效十款高性价比综合选购指南 - 十大品牌推荐
  • Fay-UE5数字人终极指南:5分钟打造专业虚拟主播的完整解决方案
  • 「码动四季·开源同行」go语言:OpenTracing 规范介绍与分布式链路追踪组件选型
  • 2026年苏州地区竹木纤维板价格多少钱,推荐品牌有哪些 - myqiye
  • VU13P加速卡在数据中心的应用:双路100G光纤与DDR4高速缓存技术解析
  • 从零开始玩转FMC+子卡:基于PCIe-403模块,手把手教你搭建自己的高速ADC/DAC数据采集与回放系统
  • 效率翻倍:基于快马平台为狼蛛f87pro键盘一键生成工作流配置
  • 智谱 Coding Plan
  • 跨设备同步方案:OpenClaw+Qwen3-32B实现多终端状态共享
  • 靠谱的竹木纤维板厂家推荐,无锡地区哪家值得选 - mypinpai
  • 别再手动配环境了!用Docker Compose一键部署Selenium自动化测试环境(含noVNC远程调试)
  • OpenMS:革新性质谱数据分析的全流程开源解决方案
  • 避坑指南:淘晶驰串口屏上实现真正的‘单选’和‘多选’功能,别再被控件名字骗了
  • 2026年优质高尔夫球车服务商权威推荐 - 深度智识库
  • 2026云南钢材批发+ 钢结构加工找哪家?钢神贸易10 年行业经验一站式服务 - 深度智识库
  • 2026年靠谱的竹木纤维板制造商推荐 - 工业设备
  • 【2026年阿里巴巴春招- 4月1日-开发岗-第一题- 数组对齐】(题目+思路+JavaC++Python解析+在线测试)
  • QMCFLAC2MP3终极指南:快速免费破解QQ音乐格式限制的完整解决方案
  • Qt跨平台开发避坑:Windows/macOS/Linux下无边框窗口的差异与QWindowKit实战
  • JavaScript PowerPoint操作终极指南:js-pptx完整教程