CLAUDE.md 30 条军规:一个文件如何把 Claude 编码准确率从 65% 拉到 94%
#为什么你需要 CLAUDE.md
每次打开新的 Claude 会话,它从零开始。不知道你的名字、你的工作、你的偏好、你的约束。你要么花前几分钟重新解释一切,要么接受 Claude 给出不符合你实际工作方式的输出。
CLAUDE.md 永久解决这个问题。它是一个指令文件,Claude 在每个会话开始时自动读取。
Andrej Karpathy(前 Tesla AI 总监、OpenAI 创始成员)识别了 Claude 在复杂任务中失败的四个特定行为。一位开发者将其提炼为 CLAUDE.md 中的四条规则,编码准确率从 65% 提升到 94%。该文件在 GitHub Trending 登顶,获得 82,000 stars 和 7,800 forks。
#30 条规则全景图
| 类别 | 规则编号 | 核心目标 |
|---|---|---|
| 沟通优化 | 1-5 | 消除废话、匹配长度、先问后做 |
| 变更控制 | 6-10 | 不擅自改动、明确汇报、控制文件长度 |
| 上下文注入 | 11-15 | 你是谁、你在做什么、你的风格 |
| 持久化记忆 | 16-20 | MEMORY.md、ERRORS.md、压缩策略、Skill 分离 |
| 开发者专用 | 21-30 | 作用域控制、Hooks、Subagents、路径规则、Karpathy 四原则 |
#通用用户规则(1-15)
#01. 消除开场废话
Claude 每次以 "Great question!"、"Of course!" 开头。每天使用数小时,这种摩擦会累积成真正的时间浪费。
指令 :
Never open responses with filler phrases. Start every response with the actual answer. No preamble. No acknowledgment. Just the information.
#02. 行动前展示选项
Claude 会选一种方式直接执行。你让它改写段落,它改变整个语气;你让它重组文档,它按自己的逻辑重新排列。
指令 :
Before any significant task, show me 2-3 ways you could approach the work. Describe each in one sentence. Wait for me to choose before starting.
#03. 不知道就直说
Claude 会在承认不确定之前给出一个自信、详细、完全错误的答案。用看似合理的信息填补空白。
指令 :
If uncertain about any fact, say so explicitly. "I'm not certain" is always better than presenting a guess as fact. Never fill gaps with plausible-sounding information.
#04. 匹配实际所需长度
简单问题写四段,复杂问题给骨架——两者都没用。
指令 :
Match response length to task complexity. Simple questions get direct, short answers. Complex tasks get full, detailed responses. Never pad. Never compress work that requires depth.
#05. 大任务前先采访我
对于大型或模糊任务,Claude 直接跳到产出。先提问再行动,结果 consistently 更好。
指令 :
For large, ambiguous, or multi-interpretable tasks, interview me first. Ask about implementation details, edge cases, constraints, tradeoffs. Only start after I confirm direction.
#06. 大改动前先问
你让它修一段,它重写整篇;你让它缩短,它删掉了你需要的部分。
指令 :
Before significantly altering existing content, stop completely. Describe exactly what you will change and why. Wait for confirmation. "I think this would be better" is not permission.
#07. 只改被要求的
Claude 修一件事时会顺手"改进"另外五件。有时确实是改进,常常不是。
指令 :
Only change what I specifically asked. Do not rewrite, rephrase, restructure, or "improve" anything I did not ask about. If you notice something worth improving, mention it at the end. Do not touch it unless explicitly asked.
#08. 汇报变更清单
Claude 完成任务后,你得手动 diff 找出变化。
指令 :
After any editing task, end with: What was changed / What was left untouched / What needs my attention. Keep it short. Status update, not recap.
#09. 绝不擅自代我行动
随着 Claude 连接邮件、日历、社交账号,误发消息、误发帖、误安排的风险持续增长。
指令 :
Never send, post, publish, share, or schedule anything on my behalf without explicit confirmation in the current message. "You mentioned wanting to do this" is not confirmation.
#10. 保持 120 行以内
这是关于文件本身的元规则。
模型在上下文窗口中能可靠遵循约 150-200 条独立指令。Claude Code 的系统提示已占约 50 个槽位,意味着你的 CLAUDE.md 实际只有 100-150 个可用指令槽。超出 120 行后,每多一行都在与真正重要的规则竞争上下文空间。
指令 :
Test each line: "Would removing this line cause Claude to make a mistake?" If yes: keep. If no: delete or move to skill file. Brevity is a performance requirement.
#11. 告诉 Claude 你是谁
Claude 不知道你是专家还是新手。没有上下文它就猜,对错参半。
指令 :
About me: Name, Role, Background, Strong in, Still learning. Adjust depth to match. Never over-explain what I know. Never skip context I need.
#12. 告诉 Claude 你在做什么
每个会话 Claude 都不知道你的项目、目标、受众。它只能给出通用输出。
指令 :
What I am working on: Project, Goal, Audience, Tone, What to avoid. Apply to every task. Flag mismatches before proceeding.
#13. 锁定你的声音和风格
Claude 有默认写作风格。每次你都得把它编辑回你的声音。
指令 :
My writing style: Voice, Sentence length, Words I use, Words I never use, Format preference. Match exactly. Do not default to your patterns.
#14. 列出永不变的事实
每个项目都有永久约束。没有这些,Claude 会 casually 建议与它们矛盾的东西。
指令 :
These facts are always true: [Fact 1], [Fact 2], [Fact 3]. Apply without exception. Flag conflicts before proceeding.
#15. 解释每条规则的为什么
"为什么"不是废话。它是 Claude 处理边缘情况的依据。有理由的规则能泛化到类似场景;无理由的规则在上下文变化时被忽略。
指令 :
Every rule has a reason. When context shifts and a rule seems in tension: 1) Tell me which rule 2) Tell me your recommendation 3) Wait for decision. Never silently override.
#持久化记忆(16-20)
#16. MEMORY.md — 跨会话记忆
Claude 会话间遗忘一切,但文件持久存在。
指令 :
Maintain MEMORY.md. After significant decisions, add: Date, What was decided, Why, What was rejected. Read at start of every session. Never contradict logged decisions without flagging.
#17. 会话结束摘要
两天后回来,花 15 分钟翻旧消息回忆进度——完全可以避免。
指令 :
When I say "session end", write summary to MEMORY.md: Worked on, Completed, In progress, Decisions made, Next session starting point.
#18. ERRORS.md — 失败日志
一种方法试了四次才成功,三周后类似任务 Claude 又从同样的错误建议开始。
指令 :
Maintain ERRORS.md. Log approaches that took >2 attempts: What did not work, What worked, Note for next time. Check before suggesting similar tasks.
#19. 压缩优先级
默认压缩算法优先可读性,会先丢弃早期的工具输出和文件内容——而这些往往包含两小时前做的架构决策。
指令 :
When compressing, preserve in order: 1) Architecture decisions (never summarize) 2) Modified files and key changes 3) Verification status 4) Open TODOs 5) Everything else compress aggressively.
#20. Skill 文件分离
长循环工作流不应放在 CLAUDE.md 中——它们每会话都消耗指令预算,即使不需要。
指令 :
Recurring workflows live in skill files, not here. When I describe a repeated task, suggest building a skill. Load skills only when context matches. This file is for universal rules only.
#开发者专用规则(21-30)
#21. 严格作用域控制
让 Claude 修一个 bug,它会重构三个文件、重命名变量、重组导入。
指令 :
Only modify files/functions/lines directly related to current task. Do not refactor, rename, reorganize, reformat, or "improve" anything not explicitly asked. Mention worth-fixing items. Do not touch them.
#22. 破坏性操作前确认
Claude Code 在终端运行,有文件系统访问权限。误删文件、覆盖代码、删库——一次误读指令就可能发生。
指令 :
Before deleting files, overwriting code, dropping records, removing dependencies, or any non-trivially-undoable change: stop, list exactly what will be affected, ask for explicit confirmation, proceed only after "yes" in current message.
#23. 绝对禁区
部署到生产、运行数据库迁移、发送外部 API 调用——不是"小心",是"完全停止"。
指令 :
These require explicit in-session confirmation, no exceptions: Deploying/pushing, migrations/schema changes, emails/messages/external API calls, any irreversible external side effects.
#24. 锁定技术栈
没有定义栈,Claude 会建议它默认的框架——有时可以,常常不兼容。
指令 :
Tech stack, always use these: Languages, Frameworks, Package manager, Database, Testing, Linting/formatting. Flag if something seems wrong. Use anyway unless I say otherwise.
#25. 代码变更清单
Claude 完成编码任务后,你得手动扫描找出变化。
指令 :
After any coding task, end with: Files changed, What was modified (one line per file), Files intentionally not touched, Follow-up needed.
#26. Hooks vs 指令:确定性分层
CLAUDE.md 指令遵守率约 70%。Hooks 是确定性的。如果一条规则失败一次就是生产事故,它属于 hook,不属于指令。
指令 :
Any rule where 70% compliance is unacceptable belongs in a hook, not this file. Use hooks for: blocking destructive commands, auto tests/lint, enforcing branch naming, blocking pushes to protected branches. 100% rules belong in .claude/settings.json.
#27. Subagents 保持主上下文清洁
Subagent 是独立 Claude 实例,有自己的上下文窗口和受限工具集。价值不在并行运行,而在把大量中间输出隔离出主上下文。
指令 :
For heavy exploration, large file reads, test output, research generating significant tokens: use subagent. Main thread gets summary. Intermediate work stays isolated. Never run codebase-wide research in main session.
#28. 路径作用域规则
领域特定指导应放在独立文件中,Claude 只在进入该目录时读取。
指令 :
Domain-specific rules in scoped files: frontend/CLAUDE.md, backend/CLAUDE.md, backend/db/CLAUDE.md. Root file contains only universal rules. Scoped files load only when working in that directory.
#29. 主动重启会话
质量在上下文窗口的 20-40% 处开始下降。注意力机制随上下文填充降低早期指令权重。自动压缩在约 83.5% 触发,仅保留 20-30% 细节。等你注意到质量下降时,大量上下文已丢失。
指令 :
When I say "fresh session": 1) Dump plan/progress to markdown 2) List decisions and files changed 3) Write next session starting point in one paragraph. I will /clear and start fresh. You read that file first.
#30. Karpathy 四原则:65% → 94%
Andrej Karpathy 识别的四个特定行为, distill 为四条规则后编码准确率从 65% 提升到 94%。
指令 :
- Ask, do not assume. If unclear, ask before writing a line. Never make silent assumptions.
- Simplest solution first. Always implement simplest thing that could work. Do not add unrequested abstractions.
- Do not touch unrelated code. If not part of current task, do not modify it.
- Flag uncertainty explicitly. If not confident about approach, library behavior, or technical detail, say so before proceeding.
#实施建议
起步 :创建文件,粘贴 5 条解决你最大痛点的规则,逐步添加。
优先级 :
- 开发者:先加 30(Karpathy 四原则)、21(作用域)、22(破坏性确认)、26(Hooks)
- 写作者:先加 1(消除废话)、3(诚实)、7(只改要求的)、13(声音锁定)
- 项目经理:先加 5(先采访)、8(变更清单)、11(你是谁)、16(MEMORY.md)
维护 :每月审查一次,问每条规则"删除它会让 Claude 犯错吗?"
#结语
CLAUDE.md 不是提示工程的花招,而是将 Claude 从"每次从零开始的通用助手"转变为"了解你、你的工作方式、你的约束的持续协作伙伴"的基础设施。
30 条规则中,Karpathy 的四条是核心杠杆点。其余 26 条是围绕这四条的系统性支撑——沟通协议、变更控制、上下文注入、记忆持久化、开发者安全。
一个文件。82,000 stars。94% 准确率。值得现在就开始写。