andrej-karpathy-skills:4KB CLAUDE.md 让 AI 写代码错误率暴降 90%
来源:GitHub + 用户分享(13万+ star)
整理时间:2026-05-18
标签:#Karpathy #CLAUDE.md #AI编程 #Claude Code #Cursor #Guardrails #最佳实践
一、项目概览
| 项目 | 信息 |
|---|---|
| 名称 | andrej-karpathy-skills |
| 作者 | Karpathy(李开复学生,斯坦福博士) |
| GitHub | github.com/forrestchang/andrej-karpathy-skills |
| Star | 13万+ |
| 核心文件 | CLAUDE.md(仅 4KB) |
| 协议 | MIT |
解决的问题: 让 AI 写代码的错误率暴降 90%,从 41% → 11% → 3%
二、Karpathy 眼中的 AI 编程四大恶习
| 恶习 | 说明 |
|---|---|
| 不问就瞎猜 | 需求不清晰就自作主张 |
| 过度设计 | 要一个登录,写一套 OAuth 2.0 + JWT + 权限系统 |
| 改 A 顺手改 BCD | 改 bug 时顺手把命名风格、缩进全优化了 |
| 代码能跑就行 | 不验证目标是否真正达成 |
三、核心:4 条行为准则
3.1 Think Before Coding - 先思考再动手
核心原则:不要假设,不要隐藏困惑,主动暴露权衡。
实施前:
- 明确声明你的假设。如果不确定,直接问。
- 如果存在多种解释,全部呈现——不要默默选择。
- 如果存在更简单的方案,指出它。在合理时主动 push back。
- 如果有不清楚的地方,停下来。说出哪里困惑,问。
❌ AI:好的,我猜你想要这个
✅ AI:我理解你的需求是 A,但 B 方案可能更合适,你要哪个?
3.2 Simplicity First - 极简实现优先
核心原则:最小代码解决问题。没有投机性抽象。
- 不写超出需求的功能。
- 单次使用的代码不搞抽象。
- 不加没被要求”灵活性”或”可配置性”。
- 不处理不可能发生的错误场景。
- 如果写了 200 行但本可以 50 行,重写。
问自己:”一个高级工程师会说这太复杂了吗?”如果是的,简化。
3.3 Surgical Changes - 手术式精准修改
核心原则:只碰必须碰的。只清理自己造成的烂摊子。
编辑现有代码时:
- 不”优化”相邻代码、注释或格式。
- 不重构没坏的东西。
- 匹配现有风格,即使你会有不同做法。
- 如果发现无关的死代码,指出它——不要删除它。
你的更改产生孤儿时:
- 移除你的更改导致的未使用导入/变量/函数。
- 不要移除已有的死代码,除非被要求。
测试:每行更改都应该能直接追溯到用户请求。
3.4 Goal-Driven Execution - 目标驱动执行
核心原则:定义成功标准。循环直到验证通过。
将任务转化为可验证的目标:
- “添加验证” → “为无效输入写测试,然后让它们通过”
- “修复 bug” → “写一个能复现它的测试,然后让测试通过”
- “重构 X” → “确保测试在重构前后都通过”
多步骤任务,声明简要计划:
1. [步骤] → 验证:[检查点]
2. [步骤] → 验证:[检查点]
3. [步骤] → 验证:[检查点]
强成功标准让你能独立循环。弱标准(”让它工作”)需要不断澄清。
四、真实效果
4.1 社区反馈
| 效果 | 数据 |
|---|---|
| 代码 diff | 从几百行缩减到几十行 |
| 错误率 | 从 41% → 11% → 3% |
| git diff | 从”满屏红绿”变成”3 行精准修改” |
4.2 中文社区评价
- “必备 skills”
- “Claude/Cursor 实用技能 Top1”
- “直接扔项目里就完事了”
- “Claude 立刻像换了个人”
五、使用方式
5.1 方法一:curl 下载(推荐)
curl -o CLAUDE.md https://raw.githubusercontent.com/forrestchang/andrej-karpathy-skills/main/CLAUDE.md
5.2 方法二:Plugin 一键安装
支持 Claude Code、Cursor 等主流 AI coding 工具
5.3 方法三:复制粘贴
直接复制项目中的 CLAUDE.md 内容到你的项目根目录
六、适用人群
| 人群 | 收益 |
|---|---|
| 独立开发者 | 减少返工,提升代码符合度 |
| 小团队 | 降低 review 成本,改动精准 |
| AI 编程使用者 | 从”代码生成器”变”问题解决者” |
七、本质洞察
Karpathy 做的事情本质上是:给 AI 建立编码的第一性原理。
不是教 AI 怎么写代码(它已经会了),而是教 AI:
- 什么时候该问
- 什么时候该停
- 什么时候该简化
这 4 条准则就像产品经理给开发团队定的 PRD 原则:
| PRD 原则 | 对应准则 |
|---|---|
| 需求不清楚?先问 | Think Before Coding |
| 功能够用就行?别过度设计 | Simplicity First |
| 改需求?只改需求相关的 | Surgical Changes |
| 做完了?先验证目标达成没有 | Goal-Driven Execution |
八、CLAUDE.md 完整内容
# CLAUDE.md
Behavioral guidelines to reduce common LLM coding mistakes. Merge with project-specific instructions as needed.
**Tradeoff:** These guidelines bias toward caution over speed. For trivial tasks, use judgment.
## 1. Think Before Coding
**Don't assume. Don't hide confusion. Surface tradeoffs.**
Before implementing:
- State your assumptions explicitly. If uncertain, ask.
- If multiple interpretations exist, present them - don't pick silently.
- If a simpler approach exists, say so. Push back when warranted.
- If something is unclear, stop. Name what's confusing. Ask.
## 2. Simplicity First
**Minimum code that solves the problem. Nothing speculative.**
- No features beyond what was asked.
- No abstractions for single-use code.
- No "flexibility" or "configurability" that wasn't requested.
- No error handling for impossible scenarios.
- If you write 200 lines and it could be 50, rewrite it.
Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify.
## 3. Surgical Changes
**Touch only what you must. Clean up only your own mess.**
When editing existing code:
- Don't "improve" adjacent code, comments, or formatting.
- Don't refactor things that aren't broken.
- Match existing style, even if you'd do it differently.
- If you notice unrelated dead code, mention it - don't delete it.
When your changes create orphans:
- Remove imports/variables/functions that YOUR changes made unused.
- Don't remove pre-existing dead code unless asked.
The test: Every changed line should trace directly to the user's request.
## 4. Goal-Driven Execution
**Define success criteria. Loop until verified.**
Transform tasks into verifiable goals:
- "Add validation" → "Write tests for invalid inputs, then make them pass"
- "Fix the bug" → "Write a test that reproduces it, then make it pass"
- "Refactor X" → "Ensure tests pass before and after"
For multi-step tasks, state a brief plan:
- [Step] → verify: [check]
- [Step] → verify: [check]
- [Step] → verify: [check]
Strong success criteria let you loop independently. Weak criteria ("make it work") require constant clarification.
九、总结
4KB 的 CLAUDE.md = 给 AI 编程建立了第一性原理
- 错误率:41% → 11% → 3%
- 代码 diff:几百行 → 几十行
- 核心理念:不是教 AI 写代码,而是教 AI 什么时候该问、该停、该简化
使用方法超级简单: 把 CLAUDE.md 丢进项目根目录,AI 编程体验立刻提升。
十、资源链接
| 资源 | 链接 |
|---|---|
| GitHub | github.com/forrestchang/andrej-karpathy-skills |
| CLAUDE.md | raw.githubusercontent.com/forrestchang/andrej-karpathy-skills/main/CLAUDE.md |
本文由 AI 辅助整理,供技术学习参考。