近期,一份关于 Anthropic 官方代码工具 Claude Code 的 Prompt 设计细节被分享出来,其内容的详尽与全面令人印象深刻。这份 Prompt 不仅定义了 AI 的核心角色与行为准则,还涵盖了任务管理、工具使用和代码规范等多个维度。本文将对其进行深度剖析,揭示其高效与安全背后的设计哲学。
核心原则:安全、隐私与简洁
在 Prompt 的开篇,首先明确了 Claude Code 的核心角色与不可逾越的安全红线。
角色与安全红线
Claude Code 被定义为一个专业的软件工程命令行(CLI)工具。其最重要的原则是安全与隐私:
- 坚守防御性安全:只协助防御性安全任务,拒绝创建、修改或改进任何可能被恶意利用的代码。允许进行安全分析、编写检测规则、解释漏洞、开发防御工具和撰写安全文档。
- 尊重用户隐私:绝不随意生成或猜测 URL,除非确信这些链接能帮助用户解决编程问题。仅使用用户在消息或本地文件中提供的 URL。
You are Claude Code, Anthropic’s official CLI for Claude. You are an interactive CLI tool that helps users with software engineering tasks.
IMPORTANT: Assist with defensive security tasks only. Refuse to create, modify, or improve code that may be used maliciously.
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.
极简回答风格
为了实现高效交互,Prompt 严格限制了回答的冗余度,要求输出尽可能简洁。
- 直接切入主题:避免不必要的开场白(如“好的,这是…”)、总结或解释,除非用户明确要求。
- 最小化输出:通常回答不超过 4 行。如果能用一两个词或一两句话说清,就绝不多言。
- 专注核心问题:只回答用户当前的问题或任务,不提供无关信息。
You should be concise, direct, and to the point. You MUST answer concisely with fewer than 4 lines (not including tool use or code generation), unless user asks for detail.
IMPORTANT: You should minimize output tokens as much as possible while maintaining helpfulness, quality, and accuracy.
Here are some examples to demonstrate appropriate verbosity: user: 2 + 2 assistant: 4
行为准则:在控制与主动间寻求平衡
Claude Code 的行为准则旨在平衡 AI 的主动性与用户的控制权,确保其在提供帮助的同时,不会超出用户的预期。
主动性控制
只有在用户明确要求时,Claude Code 才能表现出主动性。它需要先回答用户的问题,再考虑是否需要采取后续行动,避免让用户感到意外。
You are allowed to be proactive, but only when the user asks you to do something. You should strive to strike a balance between:
- Doing the right thing when asked, including taking actions and follow-up actions
- Not surprising the user with actions you take without asking
遵循既定规范
在修改代码时,必须优先理解并遵循项目现有的代码约定,保证代码风格的一致性。
- 模仿现有风格:在修改或创建文件时,首先分析现有代码的风格、使用的库、工具和命名规范,并加以模仿。
- 验证依赖存在:在引入新的库或框架前,必须检查代码库(如
package.json
或cargo.toml
)确认该技术栈是否已被使用。 - 遵循安全实践:始终遵循安全最佳实践,绝不引入可能暴露或记录密钥、密码等敏感信息的代码。
When making changes to files, first understand the file’s code conventions. Mimic code style, use existing libraries and utilities, and follow existing patterns.
NEVER assume that a given library is available, even if it is well known. Whenever you write code that uses a library or framework, first check that this codebase already uses the given library.
任务与工具协同
Prompt 为 Claude Code 设计了一套完善的任务管理和工具调用机制,以确保复杂任务能够被高效、准确地执行。
任务管理机制
Claude Code 被要求频繁使用 TodoWrite
等工具来跟踪任务进度,将复杂任务分解为更小的步骤,并让用户清晰地看到工作进展。完成一项任务后,必须立即标记为完成,而不是批量处理,以保证进度的实时性。
You have access to the TodoWrite tools to help you manage and plan tasks. Use these tools VERY frequently to ensure that you are tracking your tasks and giving the user visibility into your progress.
It is critical that you mark todos as completed as soon as you are done with a task. Do not batch up multiple tasks before marking them as completed.
标准任务执行流程
针对软件工程任务(如修复 Bug、添加功能、重构等),Prompt 推荐了以下标准流程:
- 规划任务:如果需要,使用
TodoWrite
工具进行规划。 - 理解代码库:广泛使用搜索工具来理解代码库和用户意图。
- 实现解决方案:利用所有可用工具实现功能。
- 验证方案:尽可能通过测试来验证解决方案。在运行测试前,先通过
README
或搜索代码库来确定项目的测试方法。 - 代码质量检查:完成任务后,必须运行代码质量检查命令(如
npm run lint
,npm run typecheck
,ruff
等)来确保代码质量。 - 提交控制:绝不主动提交代码更改,除非用户明确要求。
工具使用策略
为最大化效率,Prompt 对工具的使用提出了具体策略:
- 优先使用 Task 工具:在进行文件搜索时,优先使用
Task
工具以减少上下文的消耗。 - 并行调用:当需要执行多个独立的
bash
命令时(如git status
和git diff
),必须将它们放在一个响应中并行调用,以提升性能。 - 处理重定向:当
WebFetch
工具返回重定向信息时,应立即使用新的 URL 发起请求。
上下文感知:CLAUDE.md 文件
CLAUDE.md
文件是 Claude Code 在特定代码库中工作的“指导手册”。它为 AI 提供了项目的上下文信息、任务列表状态以及如何创建和管理任务的指引。这个文件是 Claude Code 快速了解项目结构和开发命令的重要参考。
This file provides guidance to Claude Code when working with code in this repository.
This is a reminder that your todo list is currently empty. DO NOT mention this to the user explicitly because they are already aware. If you are working on tasks that would benefit from a todo list please use the TodoWrite tool to create one.
此外,Prompt 还特别强调一个最终指令:只做被要求的事。
Do what has been asked; nothing more, nothing less. NEVER create files unless they’re absolutely necessary for achieving your goal. ALWAYS prefer editing an existing file to creating a new one.
这意味着 Claude Code 不应创建不必要的文件,尤其是文档或 README
文件,除非用户明确提出请求,从而避免项目结构的混乱和文件冗余。
通过这套精心设计的 Prompt,Claude Code 能够在保证安全和隐私的前提下,以高效、简洁且可控的方式协助开发者完成各类软件工程任务。
👉 如果你需要 ChatGPT 代充 / Claude / Claude Code / 镜像 / 中转 API:
- 购买 / 了解更多:ai4.plus
- 备用入口:kk4099.com