ループエンジニアリングガイド:「autoresearch」と「Bilevel Autoresearch」がAIエージェントを自律的な機械学習研究ループへ変える方法
アンドレイ・カルパティ氏が公開した「autoresearch」および「Bilevel Autoresearch」を例に、AI エージェントが目標達成まで自律的に反復学習を行うループ工学の概念と実装手法が解説されている。
キーポイント
ループ工学の定義と構成要素
従来のプロンプトベースの対話ではなく、モデルが計画・実行・検証・反復を行う自律的な「ゴール指向」のパラダイムへ移行する。信頼できるループには、試行を評価する「検証者」、状態を記録する「ステート管理」、コストを制御する「停止条件」の 3 つが不可欠である。
Karpathy の autoresearch 実装
2026 年 3 月に公開された MIT ライセンスのオープンソースリポジトリで、学習コード(train.py)のみを編集し評価コードには干渉しない厳格な設計が特徴。約 700 回の実験を経て GPT-2 のトレーニング時間を 11% 短縮する実証結果を示した。
自律的改善の具体的な成果
人間の限界を超えて継続的に実験を行うことで、QK-Norm のスカラー乗算子欠落など人間が見落としがちな微細なバグや最適化余地を発見。Shopify CEO による内部モデルでのテストでも 19% の改善が報告されるなど、定量的指標がある限り高い実効性を示す。
二重ループによる性能向上
外側ループが内側の探索プロセス自体を分析・改善することで、単一ループの5倍の検証損失低下(val_bpb)を実現し、モデルの知能ではなくアーキテクチャの革新で成果を出した。
自律的ML研究ループの5大構成要素
自動化トリガー、スキル保存、役割分担されたサブエージェント、外部ツール連携、そして品質を担保する検証ゲートという5つの再利用可能なブロックで構成される。
人間の限界とAIループの優位性
人間は約12回の実験で疲弊するが、ループは疲れず、客観的指標さえあればボトルネックは人間側にあるため、自動化による継続的な改善が可能となる。
自律ループの構成要素
効果的な自動研究ループには、検証器(verifier)、永続的な状態管理(persistent state)、および明確な停止条件(stop condition)の3つの要素が必要です。
影響分析・編集コメントを表示
影響分析
この記事は、LLM を利用した開発プロセスの根本的な転換点を示しており、人間が手動でプロンプトを調整する時代から、AI エージェントが自律的にコードを改善・最適化する「ループ工学」への移行を加速させる。特に、特定のアルゴリズムやトレーニング設定に対する微細な改善を大量の実験で見つける能力は、機械学習研究の生産性を劇的に高める可能性を秘めている。
編集コメント
「ループ工学」という概念は、AI エージェントの自律性を高める上で極めて重要なパラダイムシフトです。単なる自動化ではなく、検証と学習が閉じたループを形成することで、人間を超える探索能力を発揮する実例として注目すべき記事です。
多くの人がまだ AI を、2015 年頃の検索ボックスのように使っています。入力して読み、また入力する。しかし最近の新しいパターンは、こうした手動での往復をループで置き換えています。本ガイドでは、検証済みの 2 つの実装例を通じて「ループエンジニアリング」について解説します。参照するのは Andrej Karpathy 氏の autoresearch リポジトリと、Bilevel Autoresearch の論文です。なお、この枠組みは @0xCodila 氏の書き出しに基づいています。
ループエンジニアリングとは何か?
まずは 2 つのモードを比較してみましょう。プロンプトは「1 つの指示」に過ぎず、その後に次のステップを決めるのは人間です。一方、ループはモデルが到達するまで追求し続ける「目標」です。モデルは計画を立て、行動を起こし、結果を検証して、また繰り返します。目的は一度定義すればよく、ループが反復処理を担います。重要なのは、ループのコストが発生するのは作業の成果が測定可能な場合だけだということです。
ループを機能させる 3 つの要素
では、単なるチャットボットの繰り返しと、真のループの違いは何でしょうか?信頼できるループには必ず 3 つの構成要素が必要です。
- 検証者(Verifier)
各試行に対して評価を行います。これは合格するテスト、推移する指標、あるいはビルド処理などです。検証者がいなければ、エージェントは自分自身との対話に終始してしまいます。
- 状態記録(State)
何が試され、どこで失敗し、何が残っているかを記録します。小さなサイドファイルを用意しておけば、次の実行時に最初からやり直すのではなく、続きから再開できます。
- 停止条件(Stop Condition)
コストの暴走を防ぎます。目標が達成された場合、あるいは N 回の試行に達した場合にループを停止させます。
Karpathy のループ:「autoresearch」の中身
必ず JSON 形式で返してください。translation フィールドのみ。他のフィールドは一切追加しないこと — 余計なフィールドを書こうとして本文がトークン上限で打ち切られる事故を防ぐため:
{"translation": "リライト全文"}
これら3 つの要素は単なる理論ではありません。2026 年 3 月 7 日、Karpathy が MIT ライセンスの下でオープンソースリポジトリ「autoresearch」を公開しました。このプロジェクトには核心となるファイルが 3 つ含まれ、コード量は約 630 行です。公開から数日で話題となり、現在では GitHub で約 9 万スターを獲得しています。このパターンは後に「Karpathy Loop」として知られるようになりました。
設計思想はあえてシンプルでありながら厳格です。エージェントが編集できるのは、GPT モデルとオプティマイザ(Muon と AdamW)、そしてトレーニングループを記述した train.py だけです。評価ユーティリティが含まれる prepare.py には触れられません。この分離により、モデルの性能向上ではなくテスト自体を容易にしてしまうような改変を防いでいます。一方、エージェントが遵守すべき指示は人間が program.md に記述します。
各サイクルで 1 つの実験が実行されます。エージェントはコードを読み込み、変更案を提案し、5 分間トレーニングを実行して結果を評価。改善が見られれば採用し、そうでなければ元に戻します。評価指標は「val_bpb(検証時のビット毎バイト数)」で、値が低いほど優秀です。この予算設定により、1 時間に約 12 回の実験が可能となり、一晩中では約 100 回の試行が行われます。
報告されている成果は具体的です。Karpathy はすでに最適化済みの nanochat GPT-2 トレーニングコードに対してこの手法を適用しました。2 日間の実行で約 700 回の実験が完了し、そのうち 20 回の真の改善が採用されました。これらを積み重ねることで、GPT-2 相当品質のトレーニング時間を 11% 短縮することに成功しています(2.02 時間から 1.80 時間に)。発見された具体的な問題の一つは、QK-Norm の実装にスカラー乗算子が欠落しており、これがアテンションがヘッド全体に分散しすぎていた原因だったという点です。
特筆すべきは、人間はおよそ 12 回の実験をこなすと疲弊してしまうのに対し、このループシステムにはそのような限界がない点です。実際、Shopify の CEO トビー・リトケ氏は、社内モデルに対して「autoresearch」を一夜中実行しました。その結果、37 回の実験を経て性能が 19% 向上したと報告しています。カルパティ氏の結論は明快です。「目的とする指標さえあれば、ボトルネックになるのは人間自身だ」と。
プロンプト・ループ・バイレベルループの違い
これらを並べて比較すると、違いがより明確になります。
| 項目 | シングルショットプロンプト | カルパティ式ループ(autoresearch) | バイレベル・オートリサーチ |
|---|---|---|---|
| 定義する対象 | 各ステップごと | 目標(一度だけ設定) | 目標(一度だけ設定) |
| 反復を行う主体 | あなた自身 | 内部エージェント | 内部エージェント+外部エージェント |
| 検証者 | 手動であなたが行う | prepare.py (val_bpb) | 同じ指標を二段階で適用 |
| 状態の管理 | チャットのみ | 実験ログ | ログ+注入されたコード |
| 人間の役割 | エンジニア | program.md の作成者 | program.md の作成者 |
| 報告される結果 | ばらつきがある | 700 回実行→20 箇所の修正、11% の高速化 | val_bpb が 5 倍大きく低下(改善) |
五つの構成要素
このように、AI エンジニアリングチームは現在、以下の 5 つの再利用可能な部品を組み合わせて動作するループを構築しています。
- 自動化: ループをスケジュール、イベント、またはトリガーに基づいて起動します。
- スキル: プロジェクトの知識を Markdown ファイルに保存し、実行ごとに読み込みます。
- サブエージェント: 作成者と検証者を分離します。単一のモデルが自身を評価すると甘く見てしまうためです。
- コネクタ: ループを実際のツール(Issue Tracker や Slack など)内で動作させるための接続機能です。
- 検証者: 最終的なゲートとして、品質の低い成果物を却下する役割を果たします。
Claude Code と Codex は現在、これら五つの機能をすべて備えています。
バイレベル・オートリサーチ:ループの上にループを構築する
次に、研究者たちはより鋭い問いを投げかけました。「自動研究(autoresearch)自体もまた研究であるなら、その『自動研究』をさらに自動化できるのか?」という問いです。これに対する答えは、『Bilevel Autoresearch: Meta-Autoresearching Itself』という論文で「可能だ」と示されています。
内側のループは、カーパティが提唱した当初の構成と同じく、「提案・学習・評価・採用または破棄」というサイクルを回します。一方、外側のループはこの内側を監視し、そのコードや実行トレースを読み解きます。そして、探索プロセス自体がどこで停滞しているかを特定します。次に、新しい Python の仕組み(メカニズム)を記述してランタイム時に注入し、内側のループを再実行させます。
この手法は、カーパティの GPT 事前学習ベンチマークでも有効性を示しました。外側のループを導入したことで、検証時のビットあたりのパープレキシティ(val_bpb)の改善幅が単一ループの約 5 倍に達し(-0.045 vs -0.009)、大幅な向上が見られました。特筆すべきは、両方のループで同じ大規模言語モデル(LLM)を使用している点です。つまり、性能向上はより賢いモデルによるものではなく、アーキテクチャの工夫によるものです。
実際の設計では、この仕組みは 3 つのレベルに分割されます。レベル 1 が基本ループを実行します。レベル 1.5 は 5 回の反復ごとに探索パラメータを調整し、レベル 2 では 4 ラウンドのセッションを通じて新しいメカニズムを生成します。報告された実験では、RTX 5090(32GB)と 300 秒という計算リソース予算が使用されました。
この結果には重要な理由があります。内側のループは、手法が機能しなくなっても同じ前提条件(プリアス)に固執し続けていたのです。一方、外側のループは意図的に未知の領域への探索を強制することで、その固定観念を打破しました。
使用例と具体例
これらのアイデアは事前学習の領域を超えて、さまざまな分野に適用できます。モデル開発では検証データの損失(val_bpb)が下がるまでハイパーパラメータを探索し、ソフトウェア開発ではテストや型チェック、ビルドが通るまでリファクタリングを行います。コンテンツ作成では評価基準のスコアが閾値を超えるまで再執筆し、データ処理ではスキーマチェックが成立するまでパイプラインを調整します。これらに共通するのは、「作業を不合格とする自動ゲート」が存在することです。
実際に試してみましょう:1 つのプロンプトでループを実現する
理論はさておき、Claude Code や Codex を使わなくても、このメカニズムを実感できます。以下のプロンプトを任意の高性能なモデルに貼り付けて、自己修正の様子を観察してください。
Copy CodeCopiedUse a different Browser
タスクが基準を満たすまでループを回します。
TASK:
[作成したいものを具体的に記述]
SUCCESS CRITERIA (厳格に設定):
- [基準 1]
- [基準 2]
- [基準 3]
LOOP PROTOCOL(各ターンで繰り返し実行):
- PLAN - 次の単一のステップを明言する。
- DO - 成果物を作成するか、改善を行う。
- VERIFY - 各基準に対して結果を 1〜10 で評価する。正直にスコアをつけること。
- DECIDE - すべての基準が 8 点以上なら「FINAL」と出力して終了。そうでなければ「ITERATING」と出力し、最も低いスコアの項目から修正を開始する。
RULES:
- すべての基準が 8 点以上になるまで完了と判断しないこと。
- 各パスでは、直前の VERIFY で最も低かったスコアを改善すること。
- 質問は投げないこと。合理的な仮定を立てて継続すること。
開始。
背後にある制御フローは非常にシンプルです。以下のスニペットは、その骨格を Python で示したものです。検証器(verifier)、判断ロジック(decision)、そして 2 つの停止条件が含まれています。
Copy CodeCopiedUse a different Browser
current = baseline
best = evaluate(current) # verifier: lower val_bpb is better
for step in range(MAX_STEPS): # stop condition 1: experiment budget
candidate = propose_change(current) # agent edits train.py
score = train_and_eval(candidate) # train 5 min, then verify
if score < best: # keep only real improvements
current, best = candidate, score # commit
# else: discard candidate, restore baseline
if best <= TARGET: # stop condition 2: goal met
break
両方のバージョンには限界があります。依然としてトリガーとなるのはあなた自身であり、タブを閉じれば状態はすべて消えてしまいます。自動化機能や状態保存ファイル、そして厳格なゲート(検証プロセス)を追加することで、初めて自律的なループが完成します。
実際に動かす
以下のインタラクティブデモでは、提案・学習・検証、そして採用または巻き戻しという一連のサイクルをアニメーションで示しています。目標値やステップ制限を変更すると、停止条件が発火するまで val_bpb が低下していく様子を確認できます。
(function(){
var f=document.getElementById('mtp-loop-demo');
window.addEventListener('message',function(e){
if(e&&e.data&&e.data.loopDemoHeight){ f.style.height=e.data.loopDemoHeight+"px"; }
});
})();
重要なポイント
ループを機能させるには、検証者(verifier)、永続的な状態保存、そして停止条件の 3 つが不可欠です。
autoresearch では、エージェントは train.py のみを変更し、評価器には一切手を加えません。
Karpathy 氏の一夜限りの実験では、700 回の試行から 20 件の改善が見つかっており、速度向上率は約 11% でした。
Bilevel Autoresearch では外側のループを追加することで、val_bpb の値を 5 倍に改善する成果を得ています。
ループ工学は、思考そのものを排除するものではなく、作業の重心を「設計」と「レビュー」へとシフトさせるものです。
本記事「Guide to Loop Engineering: How 'autoresearch' and 'Bilevel Autoresearch' Turn AI Agents Into Autonomous Machine Learning ML Research Loops (続き 7/7)」は、MarkTechPost に掲載されたものです。
原文を表示
Most people still use AI like a 2015 search box. You type, you read, you type again. A newer pattern replaces that manual back-and-forth with a loop. This guide explains loop engineering using two verified artifacts. The sources are Andrej Karpathy’s autoresearch repository and the Bilevel Autoresearch paper. The framing follows a write-up by @0xCodila.
What is Loop Engineering?
To start, compare two modes. A prompt is one instruction, after which you decide the next step. A loop, by contrast, is a goal the model pursues until it arrives. The model plans, acts, checks its own result, then repeats. You define the objective once, and the loop handles iteration. Crucially, a loop only earns its cost when the work is measurable.
The Three Parts That Make A Loop Work
So what separates a real loop from a chatbot on repeat? Every reliable loop has three components.
A verifier grades each attempt. That check can be a passing test, a moving metric, or a build. Without a verifier, the agent simply agrees with itself on repeat.
State records what was tried, what failed, and what remains. A small side file lets the next run resume instead of restarting.
A stop condition prevents runaway cost. The loop halts when the goal is met, or after N attempts.
The Karpathy Loop: Inside ‘autoresearch’
These three parts are not theoretical. On March 7, 2026, Karpathy released autoresearch, an open-source repository under the MIT license. It ships three core files and about 630 lines of code. The project went viral within days and now sits near 90,000 GitHub stars. It was latter presented as the pattern “the Karpathy Loop.”
The design is deliberately small, yet strict. The agent edits only train.py, which holds the GPT model, optimizer (Muon and AdamW), and training loop. It cannot touch the evaluation utilities in prepare.py. That separation stops the agent from making the test easier instead of the model better. Meanwhile, a human writes program.md, the instructions the agent must respect.
Each cycle runs one experiment. The agent reads the code, proposes a change, trains five minutes, then keeps or rolls back. The scoring metric is val_bpb, validation bits per byte, where lower is better. That budget yields roughly 12 experiments per hour, so about 100 run overnight.
The reported outcomes are concrete. Karpathy pointed it at his already-optimized nanochat GPT-2 training code. It ran for two days and completed about 700 experiments, keeping 20 genuine improvements. Stacked together, those fixes cut GPT-2-quality training time by 11%, from 2.02 to 1.80 hours. One finding was a QK-Norm implementation missing a scalar multiplier, which had left attention too diffuse across heads.
Notably, humans tire after roughly a dozen experiments, whereas the loop does not. Separately, Shopify CEO Tobi Lütke ran autoresearch overnight on an internal model. He reported a 19% improvement after 37 experiments. Karpathy’s takeaway: if you have an objective metric, you are the bottleneck.
Prompt vs Loop vs Bilevel Loop
The differences become clearer side by side.
AspectOne-shot promptKarpathy loop (autoresearch)Bilevel Autoresearch
You defineEach stepThe goal, onceThe goal, once
Who iteratesYouInner agentInner + outer agent
VerifierYou, manuallyprepare.py (val_bpb)Same metric, two levels
StateChat onlyExperiment logLog plus injected code
Human roleEngineAuthor of program.mdAuthor of program.md
Reported resultVaries700 runs → 20 fixes, 11% speedup5x larger val_bpb drop
The Five Building Blocks
Consequently, AI engineering teams now assemble working loops from five reusable pieces:
Automation fires the loop on a schedule, event, or trigger.
A skill stores project knowledge in a markdown file, read on every run.
Sub-agents split the writer from the reviewer, since one model grades itself too generously.
Connectors let the loop act inside real tools, like an issue tracker or Slack.
Finally, a verifier remains the gate that rejects bad work. Claude Code and Codex now ship all five.
Bilevel Autoresearch: A Loop On Top Of The Loop
Next, researchers asked a sharper question. If autoresearch is research, can you autoresearch autoresearch? The research paper Bilevel Autoresearch: Meta-Autoresearching Itself answers yes.
The inner loop matches Karpathy’s original: propose, train, evaluate, keep or discard. The outer loop watches the inner loop and reads its code and traces. It identifies where the search itself keeps stalling. Then it writes new Python mechanisms, injects them at runtime, and reruns the inner loop.
The result held on Karpathy’s GPT pretraining benchmark. The outer loop cut val_bpb 5x more than the single loop (-0.045 vs -0.009). Notably, both loops used the same LLM, so the gain came from architecture, not a smarter model. In practice the design splits into three levels. Level 1 runs the base loop. Level 1.5 tunes search parameters every five iterations. Level 2 generates mechanisms through a four-round session. The reported experiments used an RTX 5090 32GB and a 300-second budget.
The reason is worth noting. The inner loop kept returning to the same priors, even after they stopped working. The outer loop broke those patterns by forcing unfamiliar exploration.
Use Cases With Examples
These ideas transfer well beyond pretraining. For model work, a loop searches hyperparameters until val_bpb drops. For software, it refactors until tests, types, and the build pass. For content, it rewrites until every rubric score clears a threshold. For data, it tunes a pipeline until schema checks hold. Each case shares one trait: an automatic gate that can fail the work.
Try It Yourself: A Loop In One Prompt
Theory aside, you can feel the mechanic without Claude Code or Codex. Paste this into any capable model and watch it self-correct.
Copy CodeCopiedUse a different Browser
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:
- PLAN - state the single next step.
- DO - produce or improve the work.
- VERIFY - score the result 1-10 on each criterion. Be honest.
- DECIDE - if every criterion is 8+, print FINAL and stop.
Otherwise print ITERATING and fix 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 questions. Make a sensible assumption and continue.
Begin.
Underneath, the control flow is small. The skeleton below shows those three parts in Python: a verifier, a decision, and two stop conditions.
Copy CodeCopiedUse a different Browser
current = baseline
best = evaluate(current) # verifier: lower val_bpb is better
for step in range(MAX_STEPS): # stop condition 1: experiment budget
candidate = propose_change(current) # agent edits train.py
score = train_and_eval(candidate) # train 5 min, then verify
if score < best: # keep only real improvements
current, best = candidate, score # commit
# else: discard candidate, restore baseline
if best <= TARGET: # stop condition 2: goal met
break
Both versions are limited. You are still the trigger, and closing the tab erases the state. Adding automation, a state file, and a real gate turns this into an autonomous loop.
See It Run
The interactive demo below animates one full loop: propose, train, verify, then keep or roll back. Adjust the target and step limit, and watch val_bpb fall until the stop condition fires.
(function(){
var f=document.getElementById('mtp-loop-demo');
window.addEventListener('message',function(e){
if(e&&e.data&&e.data.loopDemoHeight){ f.style.height=e.data.loopDemoHeight+"px"; }
});
})();
Key Takeaways
A loop needs three parts: a verifier, persistent state, and a stop condition.
autoresearch lets an agent edit only train.py and never the evaluator.
Karpathy’s overnight runs kept 20 fixes from 700 experiments, for an 11% speedup.
Bilevel Autoresearch adds an outer loop for a 5x val_bpb gain.
Loops shift the work to design and review; they do not remove thinking.
The post Guide to Loop Engineering: How ‘autoresearch’ and ‘Bilevel Autoresearch’ Turn AI Agents Into Autonomous Machine Learning ML Research Loops appeared first on MarkTechPost.
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み