# Loop Engineering: The Karpathy Method - and the workflow that just made it 5x better
**作者**: codila
**日期**: 2026-07-01T14:39:05.000Z
**来源**: [https://x.com/0xCodila/status/2072329149520232639](https://x.com/0xCodila/status/2072329149520232639)
---

Most people use AI the same way they used Google in 2005
- Type something, read what comes back, type again
The AI sits there doing nothing until you push it - you are the engine - The AI is a wrench you pick up and put down between every turn.
> That worked fine for two years. It does not work anymore.
The people getting 10x more out of AI right now are not writing better prompts and not using secret models.
They are building loops. And the person who made this idea impossible to ignore is Andrej Karpathy
This article will explain:
1 - from scratch what a loop is
2 - show you how to use them the way Andrej Karpathy does
3 - then describe a method to improve this approach by a factor of 5
4 - and provide tips for getting started.
## Part 1 · The Basics
> 1.1 - What a loop actually is ?

A prompt is one instruction. You ask, you get an answer, you decide what to do next.
A loop is a goal the AI keeps working toward until it gets there - without you sitting in the chair prompting every step
The AI discovers what needs doing, plans how to do it, does the work, checks the result, and if it is not there yet - feeds the result back in and goes again. You define the purpose once.
The loop handles the rest.
Three parts make or break it:
- A verifier is what turns repetition into progress. Without a real check on the result, you do not have a loop - you have the agent agreeing with itself on repeat. The check can be a test that passes or fails, a metric that goes up or down, a build that compiles or crashes. No gate means the agent grades its own homework.
- State is what makes the loop learn. Each pass, the AI has to know what it already tried. Without that, it repeats the same mistake every cycle. A small file on the side records what is done, what failed, what is next. Tomorrow's run resumes instead of starting from zero.
A stop condition keeps it sane. A loop with no exit runs until it succeeds, breaks, or drains your account. Every working loop has two ways to stop: the goal is met, or a hard limit says "after N tries, stop and report"
> 2.2. Do you actually need one? Run this test first.
Most articles sell you the loop before they tell you when it is a mistake.
A loop earns its cost only when all four of these are true. Miss one and it costs more than it returns.

- The task repeats - at least weekly. Less than that and the setup cost never pays itself back. A one-time job is still better served by one good prompt.
- Verification is automated - a test suite, a type checker, a linter, a build. Something that can fail the work without you in the room. No automated check means you are back reading every diff - the exact job the loop was supposed to remove.
- Your token budget can absorb the waste - loops re-read context, retry, explore. That burns tokens whether the run ships anything or not. This scales with budget, which is why it reads as obvious to people with free tokens and reckless to people on a $20 plan.
- The agent has real tools - logs, a reproduction environment, the ability to run the code it writes and see what breaks. Without that, the loop iterates blind.
The honest take: loop engineering is real, and most people do not need the heavy version yet. If you are on a consumer plan with limited tokens, a heavy loop will hit your rate limit or your wallet before the productivity gain arrives.
## Part 2 · The Karpathy Loop
How Karpathy stopped prompting and started looping
In March 2026, Andrej Karpathy released a GitHub repo called AutoResearch.
> Three files. About 630 lines of code.

Within a month it had 66,000+ stars and Fortune magazine gave it a name: The Karpathy Loop.
- karpathy autoresearch -
The setup is almost absurdly simple:
- train.py - the training script. The only file the agent is allowed to touch.
- prepare.py - the evaluator that scores the model. The agent cannot touch it. If it could, it would just make the test easier instead of making the model better.
- program.md - the instructions that tell the agent what to explore and what constraints to respect.
The agent runs in a loop:
> Read the code → propose a change → train for five minutes → check if the result improved → commit if it did, roll back if it did not → repeat.
You go to sleep. You wake up to a log of experiments and hopefully a better model.
The human never touches train.py. You write program.md. - the agent handles execution.
- The results:
Karpathy pointed it at a model he had already carefully tuned by hand over two decades of experience. He let it run for two days.
- The agent ran 700 experiments
- Found 20 improvements he missed
- Things like a missing scalar multiplier in the attention mechanism that made attention too diffuse across heads
- Not a bug a fuzzer would catch - a subtle optimization that a careful human could have found but did not
Because humans get tired after experiment number twelve. The agent does not get tired at all.
Shopify CEO Tobi Lutke tried it overnight on an internal model:
- He woke up to a 19% quality improvement
- The optimized model was half the size of his previous one
- A smaller model beating a bigger one - because the agent optimized for the hardware instead of defaulting to "bigger is better"
> The Karpathy Loop Fortune
> Karpathy's core insight: if you have an objective metric, you should not be the one running the experiments. You are the bottleneck. Remove yourself from the loop and let it run.
## Part 3 · The Building Blocks
Five pieces that make a loop work
Every working loop - whether you build it in Claude Code, Codex, or bash scripts - is assembled from five pieces.
Both Claude Code and Codex ship all five now.

- Automation - the heartbeat. Something that fires the loop on a schedule, on an event, or on a trigger.
In Claude Code: /loop for cadence, /goal for running until a condition holds. In Codex: the Automations tab. Without the heartbeat, you ran a script once and forgot about it. That is not a loop.
- A skill - stores project knowledge so the agent stops guessing every session. Your conventions, your build steps, the thing you do not do because of that one incident three months ago.
Written once in a markdown file, read by every run.
Without skills, the loop re-derives your entire project context from zero every cycle. With skills, intent compounds.
- Sub-agents - split the maker from the checker.
The model that wrote the code is too generous grading its own homework.
A second agent with different instructions catches the things the first one talked itself into. Your writer can be fast and cheap, your reviewer slow and strict. That separation is most of the quality.
- Connectors - let the loop act inside your real environment. Read your issue tracker, open a PR, ping Slack, update a Linear ticket.
The difference between an agent that says "here is the fix" and a loop that ships the fix and tells you about it in the morning.
- A verifier - the gate. The test, type check, or build that automatically rejects bad work.
Everything else is plumbing.
This is the part that makes the loop real. Without it you are paying for an agent to agree with itself all night
## Part 4 · What Comes After Karpathy
The Bilevel - a Loop on top of the Loop
- this is where it gets interesting, hehe xD
- In March 2026, two researchers published a paper on arxiv called "Bilevel Autoresearch: Meta-Autoresearching Itself."
They took Karpathy's loop and asked a simple question:
If autoresearch is itself a form of research - can you autoresearch autoresearch?
> Bilevel Autoresearch
They built a second loop on top of the first one:
- Inner loop - does what Karpathy's original does: propose a change, train, evaluate, keep or discard
- Outer loop - watches the inner loop work, reads its code and traces, identifies where the search process itself is getting stuck, and generates new Python code that changes how the inner loop searches.
Then it injects that code and lets the inner loop run again.

- The result:
On Karpathy's GPT pretraining benchmark:
- 5x improvement over the standard single loop (-0.045 vs -0.009 val_bpb)
- Not 5% better. Five times better.
- Both loops used the same LLM - you do not need a smarter model for the meta level
- The improvement comes from the architecture, not from raw intelligence
What the outer loop actually found: the inner loop kept falling into the same search patterns. The LLM has priors about what optimizations to try, and it keeps going back to those priors even when they stopped working. The outer loop broke those patterns by forcing exploration in directions the model's instincts avoided.
> The paper ends with a line worth sitting with: "If autoresearch can meta-autoresearch itself, it can in principle meta-autoresearch anything with a measurable objective."
## Part 5 · Try It Yourself
Run a loop right now - no tools needed
You do not need Claude Code or Codex to feel how this works. Paste this into any LLM and watch what happens:
```
You will work in a loop until the task meets the bar.
TASK:
[describe exactly what you want produced]
SUCCESS CRITERIA (be strict):
- [criterion 1]
- [criterion 2]
- [criterion 3]
LOOP PROTOCOL, repeat every turn:
1. PLAN - state the single next step.
2. DO - produce or improve the work.
3. VERIFY - score the result 1-10 on each criterion.
Be brutally honest. List exactly what is still weak.
4. DECIDE - if every criterion is 8+, print FINAL and stop.
Otherwise print ITERATING and go again, fixing
the weakest point first.
RULES:
- Never call it done until every criterion is 8 or higher.
- Each pass must fix the weakest score from the last VERIFY.
- Do not ask me questions. Make a sensible assumption
and keep going.
Begin.
```
The model drafts, grades its own work against your criteria, finds the weak spot, rewrites, and repeats until it clears the bar.
That is a loop. You just built one with a paragraph.
It is limited - you are still the trigger, there is no schedule, no persistent state, close the tab and it is gone. But it shows the core mechanic. The jump from this to a full autonomous loop is adding the automation, the state file, and the gate.
## Part 6 · The Honest Part
What the loop does not fix
Loops change the work. They do not delete you from it.
And two problems get sharper as the loop gets better - not easier:
- Comprehension debt. The faster the loop ships code you did not write, the larger the gap between what exists in your repo and what you actually understand. A smooth running loop charges compound interest on that gap. The day you have to debug a system nobody on the team has read will cost more than the tokens ever did.
- Cognitive surrender. When the loop runs itself, it is tempting to stop forming an opinion and just accept whatever comes back. Designing the loop is the cure when you do it with judgment - and the accelerant when you do it to avoid thinking. Same action, opposite result.
Two people can build the exact same loop and get completely opposite outcomes. One uses it to move faster on work they understand deeply. The other uses it to avoid understanding the work at all.
The loop does not know the difference. You do!
Karpathy stopped writing code. Cherny stopped prompting. Neither of them stopped thinking. If you take one thing from this, take that.
start use loop like engineer . @0xCodila
## 相关链接
- [codila](https://x.com/0xCodila)
- [@0xCodila](https://x.com/0xCodila)
- [3.9M](https://x.com/0xCodila/status/2072329149520232639/analytics)
- [karpathy autoresearch](https://github.com/karpathy/autoresearch)
- [train.py](https://train.py/)
- [prepare.py](https://prepare.py/)
- [train.py](https://train.py/)
- [The Karpathy Loop Fortune](https://fortune.com/2026/03/17/andrej-karpathy-loop-autonomous-ai-agents-future/)
- [Bilevel Autoresearch](https://arxiv.org/abs/2603.23420)
- [@0xCodila](https://x.com/@0xCodila)
- [Upgrade to Premium](https://x.com/i/premium_sign_up)
- [10:39 PM · Jul 1, 2026](https://x.com/0xCodila/status/2072329149520232639)
- [3.9M Views](https://x.com/0xCodila/status/2072329149520232639/analytics)
- [View quotes](https://x.com/0xCodila/status/2072329149520232639/quotes)
---
*导出时间: 2026/7/12 15:14:38*
---
## 中文翻译
# 循环工程:Karpathy 方法——以及让效率提升 5 倍的工作流
**作者**: codila
**日期**: 2026-07-01T14:39:05.000Z
**来源**: [https://x.com/0xCodila/status/2072329149520232639](https://x.com/0xCodila/status/2072329149520232639)
---

大多数人使用 AI 的方式,和他们在 2005 年使用 Google 的方式没什么两样。
- 输入点什么,读读返回的内容,然后再输入点什么。
在你推它一把之前,AI 就在那儿干等着——你是引擎——AI 只是一把你在每次转弯间隙拿起又放下的扳手。
> 这种方法在过去两年里运作良好。但现在行不通了。
现在那些从 AI 中获得 10 倍产出的人,并不是因为写了更好的提示词,也不是用了什么秘密模型。
他们在构建循环。而让这个想法无法被忽视的人,就是 Andrej Karpathy。
本文将解释:
1. 从零开始解释什么是循环
2. 向你展示如何像 Andrej Karpathy 那样使用它们
3. 然后描述一种将这种方法提升 5 倍的手段
4. 并提供入门建议。
## 第 1 部分 · 基础知识
> 1.1 - 循环到底是什么?

提示词是一条指令。你提问,得到回答,然后决定下一步做什么。
循环是一个目标,AI 会持续朝它努力,直到达成——而不需要你坐在椅子上一步步提示。
AI 自己发现需要做什么,规划如何做,执行工作,检查结果,如果还没达成——就把结果反馈进来再试一次。你只需要定义一次目的。
剩下的交给循环。
三个要素决定成败:
- 验证器是将重复转化为进度的关键。如果没有对结果进行实质性的检查,你就没有循环——你只是让智能体在重复中自我赞同。检查可以是通过或失败的测试,上升或下降的指标,编译成功或崩溃的构建。没有关卡意味着智能体自己在批改作业。
- 状态让循环得以学习。每一轮,AI 都得知道它已经尝试过什么。没有这个,它每个周期都在重复同样的错误。旁边的一个小文件记录了做了什么、失败了什么、下一步是什么。明天的运行会从中恢复,而不是从零开始。
停止条件让它保持理智。没有出口的循环会一直运行,直到成功、崩溃或耗尽你的账户。每个能工作的循环都有两种停止方式:目标达成,或者硬性限制说“尝试 N 次后,停止并报告”。
> 2.2. 你真的需要一个吗?先做个测试。
大多数文章会在告诉你什么时候是个错误之前,先向你推销循环。
只有在以下四点全部为真时,循环才划算。缺一点,它的成本就高于收益。

- 任务重复——至少每周一次。低于这个频率,设置成本永远无法收回。一次性任务最好还是用一个优秀的提示词来解决。
- 验证自动化——测试套件、类型检查器、Linter、构建工具。有些东西能在你不在场时让工作失败。没有自动检查意味着你又要回头去读每一个 Diff——这恰恰是循环本来要消除的工作。
- 你的 Token 预算能承受浪费——循环会重读上下文、重试、探索。无论运行是否产出成果,这都会消耗 Token。这与预算成正比,这就是为什么对于有免费 Token 的人来说这显而易见,而对于使用 20 美元套餐的人来说这显得鲁莽。
- 智能体拥有真正的工具——日志、复现环境、运行它写的代码并看到哪里出错的能力。没有这些,循环就是在盲目迭代。
坦率的说法:循环工程是真实的,但大多数人还不需要重型版本。如果你使用的是 Token 受限的消费级计划,重型循环会在生产力提升到来之前先触碰你的速率限制或钱包。
## 第 2 部分 · Karpathy 循环
Karpathy 如何停止提示词并开始循环
2026 年 3 月,Andrej Karpathy 发布了一个名为 AutoResearch 的 GitHub 仓库。
> 三个文件。大约 630 行代码。

不到一个月,它就获得了 66,000+ 的星标,《财富》杂志给它起了一个名字:Karpathy 循环。
- karpathy autoresearch -
这个设置简单得近乎荒谬:
- train.py——训练脚本。这是唯一允许智能体触碰的文件。
- prepare.py——给模型打分的评估器。智能体不能碰它。如果能碰,它就会把测试变简单,而不是把模型变好。
- program.md——告诉智能体探索什么以及遵守哪些限制的指令。
智能体在循环中运行:
> 读取代码 → 提出修改 → 训练五分钟 → 检查结果是否改进 → 改进则提交,未改进则回滚 → 重复。
你去睡觉。醒来时面对的是实验日志,希望还有一个更好的模型。
人类从不触碰 train.py。你写 program.md——智能体处理执行。
- 结果:
Karpathy 将它指向一个他凭借二十年经验精心调优过的模型。他让它运行了两天。
- 智能体运行了 700 次实验
- 发现了他错过的 20 个改进点
- 比如注意力机制中缺少一个标量乘数,导致注意力在各个头之间过于分散
- 这不是模糊测试能发现的 Bug——这是一种精细的优化,细心的人类本可以发现但没有发现
因为人类在第十二次实验后就累了。智能体根本不会累。
Shopify CEO Tobi Lutke 在内部模型上试了一整晚:
- 他醒来时质量提升了 19%
- 优化后的模型是他之前那个模型的一半大小
- 一个小模型打败了大模型——因为智能体针对硬件进行了优化,而不是默认“越大越好”
> 《财富》杂志论 Karpathy 循环
> Karpathy 的核心洞察:如果你有一个客观指标,你不应该亲自运行实验。你是瓶颈。把自己从循环中移除,让它运行。
## 第 3 部分 · 构建模块
让循环工作的五个组件
每一个能工作的循环——无论你是用 Claude Code、Codex 还是 bash 脚本构建的——都由五个组件组装而成。
Claude Code 和 Codex 现在都内置了这五项。

- 自动化——心跳。按照时间表、事件或触发器启动循环的东西。
在 Claude Code 中:/loop 用于节奏,/goal 用于运行直到满足条件。在 Codex 中:是 Automations 选项卡。没有心跳,你只运行了一次脚本然后就忘了它。那不是循环。
- 技能——存储项目知识,这样智能体就不会每次会话都瞎猜。你的约定、你的构建步骤、因为三个月前的那次事故而你不再做的事情。
一次写在 markdown 文件里,每次运行都读取。
没有技能,循环每个周期都要从零推导你的整个项目上下文。有了技能,意图会复合增长。
- 子智能体——把制造者和检查者分开。
编写代码的模型给自己批改作业太大方了。
第二个拥有不同指令的智能体能捕捉到第一个智能体自说自话的东西。你的编写者可以快速廉价,你的审查者可以缓慢严格。这种分离是质量的大部分来源。
- 连接器——让循环在你的真实环境中行动。读取你的 Issue 追踪器,打开 PR,Ping Slack,更新 Linear 票据。
这区别在于一个智能体说“这是修复方案”和一个循环修复了问题并在早上告诉你。
- 验证器——关卡。自动拒绝劣质工作的测试、类型检查或构建。
其他一切都是管道。
这是让循环变真的部分。没有它,你就是在花钱让智能体整晚自己赞同自己。
## 第 4 部分 · Karpathy 之后是什么
双层——循环之上的循环
- 这里开始变得有意思了,嘿嘿 xD
- 2026 年 3 月,两位研究人员在 arxiv 上发表了一篇论文,名为“Bilevel Autoresearch: Meta-Autoresearching Itself”(双层自动研究:对自动研究本身的元研究)。
他们拿 Karpathy 的循环提出了一个简单的问题:
如果自动研究本身也是一种研究——你能对自动研究进行自动研究吗?
> 双层自动研究
他们在第一个循环之上构建了第二个循环:
- 内层循环——做 Karpathy 原版做的事:提出修改、训练、评估、保留或丢弃。
- 外层循环——观察内层循环工作,读取其代码和追踪,识别搜索过程本身卡在哪里,并生成新的 Python 代码来改变内层循环的搜索方式。
然后它注入该代码,让内层循环再次运行。

- 结果:
在 Karpathy 的 GPT 预训练基准上:
- 比标准单层循环提升了 5 倍(-0.045 vs -0.009 val_bpb)
- 不是好 5%。是五倍好。
- 两个循环使用的是同一个 LLM——元层不需要更聪明的模型
- 改进来自架构,而非原始智力
外层循环实际发现的是:内层循环总是陷入同样的搜索模式。LLM 对尝试哪些优化有先验偏见,即使这些先验不再有效,它也会一直回头去用。外层循环通过强制在模型本能回避的方向上进行探索,打破了这些模式。
> 论文以一句值得深思的话结尾:“如果自动研究可以对自身进行元自动研究,那么原则上它可以对任何具有可测量目标的事物进行元自动研究。”
## 第 5 部分 · 亲自尝试
现在就运行一个循环——无需工具
你不需要 Claude Code 或 Codex 就能感受它是如何工作的。把这个粘贴到任何 LLM 中看看会发生什么:
```
你将在循环中工作,直到任务达到标准。
任务:
[准确描述你想要产出的内容]
成功标准(严格一点):
- [标准 1]
- [标准 2]
- [标准 3]
循环协议,每一轮重复:
1. 计划 - 说明唯一的下一步。
2. 执行 - 产出或改进工作。
3. 验证 - 针对每项标准给结果打 1-10 分。
要极其诚实。准确列出还有什么弱点。
4. 决策 - 如果每项标准都达到 8 分以上,打印 FINAL 并停止。
否则打印 ITERATING 并重来,优先
修复上次验证中得分最低的点。
规则:
- 直到每项标准都达到 8 分或以上,否则绝不叫停。
- 每一轮必须修复上一次验证中得分最低的点。
- 不要问我问题。做出合理的假设
并继续。
开始。
```
模型会起草草案,根据你的标准给自己的工作打分,找出弱点,重写,并重复直到达标。
这就是循环。你刚用一段话就构建了一个。
它很有限——你还是触发者,没有时间表,没有持久状态,关掉标签页它就没了。但它展示了核心机制。从这个跳跃到完全自主循环,只需加上自动化、状态文件和关卡。
## 第 6 部分 · 诚实的部分
循环解决不了的问题
循环改变了工作方式。它们不会把你从中抹去。
而且随着循环变得更好,有两个问题会变得更尖锐——而不是更容易:
- 理解债务。循环运送你没写过的代码的速度越快,仓库中存在的东西和你实际理解的东西之间的差距就越大。一个运行顺畅的循环会对这个差距收取复利。某天你必须调试一个团队里没人读过的系统时,其代价将远超 Token 的费用。
- 认知投降。当循环自己运行时,很容易不再形成观点,只是接受返回的任何结果。如果你带着判断力去设计循环,它是解药;如果你用它来避免思考,它是加速剂。同样的行动,相反的结果。
两个人可以构建完全相同的循环,却得到完全相反的结果。一个用它来在深刻理解的工作上移动得更快。另一个用它来完全避免理解工作。
循环不知道其中的区别。你知道!
Karpathy 停止写代码。Cherny 停止写提示词。他们都没停止思考。如果你从中只能带走一样东西,带走这个。
像工程师一样开始使用循环。@0xCodila
## 相关链接
- [codila](https://x.com/0xCodila)
- [@0xCodila](https://x.com/0xCodila)
- [3.9M](https://x.com/0xCodila/status/2072329149520232639/analytics)
- [karpathy autoresearch](https://github.com/karpathy/autoresearch)
- [train.py](https://train.py/)
- [prepare.py](https://prepare.py/)
- [train.py](https://train.py/)
- [The Karpathy Loop Fortune](https://fortune.com/2026/03/17/andrej-karpathy-loop-autonomous-ai-agents-future/)
- [Bilevel Autoresearch](https://arxiv.org/abs/2603.23420)
- [@0xCodila](https://x.com/@0xCodila)
- [Upgrade to Premium](https://x.com/i/premium_sign_up)
- [10:39 PM · Jul 1, 2026](https://x.com/0xCodila/status/2072329149520232639)
- [3.9M Views](https://x.com/0xCodila/status/2072329149520232639/analytics)
- [View quotes](https://x.com/0xCodila/status/2072329149520232639/quotes)
---
*导出时间: 2026/7/12 15:14:38*