接受Karpathy的建议,给AI编程带上紧箍咒
你有没有过这种经历:
你只是想让 AI 帮你改一个按钮的颜色,结果它啪一下丢给你 200 行代码,顺便把你的整个页面布局都重构了。你运行了一下,代码报错,你问它怎么回事,它又开始疯狂道歉并陷入了死循环……

这就是当前 AI 编程最大的痛点:AI 不是不会干活,是太爱发挥了。
受特斯拉前 AI 负责人 Andrej Karpathy 的启发,这个专门给 AI 程序员立规矩的 GitHub 项目值得我们关注:
https://github.com/forrestchang/andrej-karpathy-skills

今天,我们用费曼学习法,把这份复杂的“顶级架构师准则”翻译成人话,看看为什么现在的 AI 必须戴上这副“紧箍咒”。
一、 核心痛点:别让 AI 变成“过度勤奋的实习生”
想象一下,你雇了一个天才实习生。他虽然熟读所有编程书籍,但有个坏毛病:你让他修个水龙头,他能把你家厨房拆了重新装修,还振振有词说“新的设计更科学”。
这就是现在的 LLM(大模型)。如果不加约束,它会产生“代码熵”——也就是无谓的复杂度和潜在的 Bug。
Karpathy 的这套规则,本质上就是把这个“天才实习生”调教成“资深老架构师”。
二、 费曼式拆解:这套规则到底在说什么?
这份指南可以浓缩成四个极其简单的生活常识:
1. 先问“为什么”,再动手(Think Before Coding)
-
直白解释:如果你没听懂老婆让你买什么水果,别随便买个榴莲回家。 -
AI 准则:在写代码前,AI 必须先复述需求。如果模糊不清,必须提问。它需要列出几种方案让你选,而不是自作主张选一个最复杂的。
2. 做“微创手术”,别搞“开颅手术”(Surgical Changes)
-
直白解释:灯泡坏了就换灯泡,别因为嫌吊灯样式老旧就顺便把天花板拆了。 -
AI 准则:精准修改。不许动无关的代码,不许改动别人的缩进。哪怕你觉得原来的代码写得丑,只要它没坏,AI 就不准动。
3. 极简主义:能写 10 行,绝不写 11 行(Simplicity First)
-
直白解释:如果一把瑞士军刀就能修好自行车,就别去开挖掘机。 -
AI 准则:拒绝过度工程。不要为了“以后可能用到”的功能写一堆复杂的配置。如果 AI 发现一段代码能被删减,它的第一反应应该是重写得更短,而不是加更多补丁。
4. 结果导向:自备“验货标准”(Goal-Driven Execution)
-
直白解释:去超市买菜前先列清单,买完对着清单一项项勾掉。 -
AI 准则:不要说“把它修好”,要说“写一个能复现 Bug 的测试,然后让它通过”。AI 必须先定义成功标准,然后循环尝试直到验证通过。
三、 为什么现在这件事极其重要?
在过去,程序员自己写代码,每一行都是深思熟虑的。
在现在,AI 一秒钟能生成几百行代码。
如果 AI 不遵守这套规则,后果是灾难性的:
-
维护地狱:你会发现项目里充斥着 AI 生成的、谁也看不懂的复杂逻辑。 -
信任崩塌:你不敢让 AI 动你的核心代码,生怕它引发连锁崩溃。 -
成本飙升:过度的代码意味着更多的 Token 消耗和更长的 Debug 时间。
四、 具体内容
这套准则不是给你看的,而是给 AI “洗脑”用的。你可以去GitHub上详细了解安装部署方式,页可以从这里获取SKILL.md 的具体内容,然后加到你的System rules里:
—
name: karpathy-guidelines
description: Behavioral guidelines to reduce common LLM coding mistakes. Use when writing, reviewing, or refactoring code to avoid overcomplication, make surgical changes, surface assumptions, and define verifiable success criteria.
license: MIT
—
# Karpathy Guidelines
Behavioral guidelines to reduce common LLM coding mistakes, derived from [Andrej Karpathy’s observations](https://x.com/karpathy/status/2015883857489522876) on LLM coding pitfalls.
**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:
“`
1. [Step] → verify: [check]
2. [Step] → verify: [check]
3. [Step] → verify: [check]
“`
Strong success criteria let you loop independently. Weak criteria (“make it work”) require constant clarification.
总结一句话:好的 AI 程序员,不是写代码最快的那个,而是知道什么时候“不该写代码”的那个。
今日互动:
你被 AI “帮倒忙”最惨的一次经历是什么?欢迎在评论区分享你的血泪史。
夜雨聆风