了解 Claude Code 的提示词工程
虽然同样是调用 Opus 4.7 模型,但是不同的编程工具展现的表现确不一样。就拿 Claude Code, Cursor, OpenCode,Windsurf 这几个产品来说,显然 Claude Code 在程序员中的口碑更好。
那 Claude Code 与其它编程工具的差异性在哪儿呢?其中有一个重要的环节就是:提示词工程(prompt engine)。
正好我今天看到了 ccglass 这个开源项目,它可以把 Claude Code 的提示词通过网络请求截取出来,于是我试了一下,给大家分享一下我看到的有趣的地方。
整个提示词分为:系统层、消息层、工具层。这三层的提示词是拼接在一起发给服务器的。
系统层
我们先看系统层,这一层规定了 Claude Code 的人设,工作原则和方法,以及记忆。
著名的 cch header
在提示词的最顶部,你能看到这样一行:
1 |
x-anthropic-billing-header: cc_version=2.1.148.90a; cc_entrypoint=cli; cch=06d7a; |
这行最后的 cch 参数,就是造成 claude code 配置别的大语言模型缓存失效的元凶。因为模型调用的缓存是基于“前缀”的,如果你的提示词前面都没变,只是在最后加了一句新的对话。那么前面的缓存都会命中。但是 claude code 在今年 2 月的升级中加入了这个参数,在每轮对话中 cch 参数都会变,如果别的模型不特殊处理这个参数,就会造成所有缓存失效。
人设
告诉它是官方的,并且主要是做软件工程相关的任务。
提示词如下:
1 |
You are Claude Code, Anthropic's official CLI for Claude. |
禁止干坏事
提示词中明确禁止了做破坏性(比如 DoS 攻击)的事情,一些高风险的事情,也会要求用户澄清用处。
提示词如下:
1 |
Refuse requests for destructive techniques, DoS attacks, mass targeting, supply chain compromise, or detection evasion for malicious purposes. Dual-use security tools (C2 frameworks, credential testing, exploit development) require clear authorization context: pentesting engagements, CTF competitions, security research, or defensive use cases. |
记忆
提示词中为每个目录都创建了记忆,让 Agent 需要的时候把记忆保存下来。
像我的这个实验项目,记忆文件就在 /Users/tangqiao/.claude/projects/-Users-tangqiao-ccglass/memory/。
提示词如下:
1 |
You have a persistent, file-based memory system at `/Users/tangqiao/.claude/projects/-Users-tangqiao-ccglass/memory/`. This directory already exists — write to it directly with the Write tool (do not run mkdir or check for its existence). |
关于记忆,提示词中还规定了什么时候保存,怎么保存。
提示词如下:
1 |
## How to save memories |
只有 200 行的记忆限额
提示词如下:
1 |
`MEMORY.md` is always loaded into your conversation context — lines after 200 will be truncated, so keep the index concise |
消息层
这一层定义了它能使用的各种工具。
skill
所有的 skill 在这一层被提示词引入,你能看到这样的提示词开头:
1 |
The following skills are available for use with the Skill tool: |
我因为之前装过 hyperframes,所以我看到了大量 hyperframes 的 skill。
我这才知道 hyperframes 这个框架一共有 16 个 skill,每次我用 claude code,这些 skill 都被注入到了提示词。
hyperframes 引入了一共有多少提示词呢?给大家看一下:
1 |
- hyperframes: Create video compositions, animations, title cards, overlays, captions, voiceovers, audio-reactive visuals, and scene transitions in HyperFrames HTML. Use when asked to build any HTML-based video content, add captions or subtitles synced to audio, generate text-to-speech narration, create audio-reactive animation (beat sync, glow, pulse driven by music), add animated text highlighting (marker sweeps, hand-drawn circles, burst lines, scribble, sketchout), or add transitions between scenes (crossfades, wipes, reveals, shader transitions). Covers composition authoring, timing, media, and the full video production workflow. For CLI commands (init, lint, preview, render, transcribe, tts) see the hyperframes-cli skill. |
怎么说呢?反正我立马把它卸载了。我给 claude code 的提示词如下:
1 |
我曾经用 npx skills add heygen-com/hyperframes 命令增加了 hyperframes skill, 现在我希望你帮我把这个 skill 删除掉。 |
用户人设和时间
其实 claude code 知道你的信息,相关提示词如下:
1 |
<system-reminder> |
用户的问题
用户输入的问题被放在了这里。
注意,每轮对话,它会把之前所有的对话都带上,直到上下文超限,才会进行压缩。
有意思的是,你能看到它不但带上了我的输入,还带上了工具执行的结果。所以对话的上下文增长的速度也非常快。
就比如我让它删除 hyperframes 的输入,它因为调用了 10 来次工具,使得我已经无法在这儿把所有的上下文贴给大家了。
工具层
有意思的是,工具层虽然也是可以缓存的部分,但是 claude code 把它放在了消息层后面。我猜这也是一个对抗其它模型缓存的办法,其它模型需要主动地把工具层部分的提示词也单独计算,否则它一定会因为夹在中间的用户输入而失效(心机重的 Anthropic 啊!)。
鼓励使用子 Agent
1 |
Launch a new agent to handle complex, multi-step tasks. Each agent type has specific capabilities and tools available to it. |
如何提问
教大模型如何向用户提问,Claude code 有时候会弹出单选/复选框,就是这个提示词在起作用。
1 |
Use this tool when you need to ask the user questions during execution. This allows you to: |
bash
所有的 bash 命令,也会在这一层被介绍给 Claude code 具体的使用方法。
比如 Read 命令的提示词如下:
1 |
Reads a file from the local filesystem. You can access any file directly by using this tool. |
MCP
所有的 MCP 在这一层被一一介绍。
比如这个 mcp__claude-in-chrome__tabs_create_mcp:
1 |
Creates a new empty tab in the MCP tab group. CRITICAL: You must get the context using tabs_context_mcp at least once before using other browser automation tools so you know what tabs exist. |
调用 skill
虽然 skill 是在消息层注入的,但是工具层教大模型什么时候调用 skill。
1 |
When users reference a "slash command" or "/<something>", they are referring to a skill. Use this tool to invoke it. |
结束语
最后给大家讲个题外话,ccglass 这个开源项目是百姓网CEO王建硕的作品,感谢王建硕先生!