LevelUpWith

Explore / Autoresearch Loops: Agents That Experiment and Improve / Lesson 1

Lesson 1: Anatomy of an Autoresearch Loop

Learn the five steps every autoresearch loop shares (hypothesis, edit, run, measure, keep or revert) by reading a real minimal reference: Karpathy's open-source autoresearch repo, where an agent edits one training file, runs a fixed-length training job and keeps the change only if validation bits-per-byte goes down.

Free to read, no account needed. Part of Autoresearch Loops: Agents That Experiment and Improve.

By the end of this level you'll be able to

  • Name the five steps of the loop and point to the file or command that carries out each one in a small reference repo
  • Tell apart the parts a loop lets the agent edit, the parts it keeps fixed, and the instructions a human writes
  • Explain why each run having a fixed time limit makes results comparable from one run to the next

The idea in one paragraph

You already run a small version of this loop when you tune a slow endpoint. You guess at the cause ("the N+1 query"), change the code, benchmark it, and then either keep the change or run git checkout . to throw it away. An autoresearch loop follows the same routine, but a coding agent does the work, follows written rules, and repeats it dozens or hundreds of times while you sleep. Every loop you build in this course has the same five steps:

  1. Hypothesis: the agent decides what to try ("a lower matrix learning rate might help").
  2. Edit: it changes the code or prompt.
  3. Run: it runs the experiment.
  4. Measure: it reads one number from the output.
  5. Keep or revert: if the number got better it keeps the change, and otherwise it undoes it.

The quickest way to make these steps concrete is to read a real, small loop.

The reference: karpathy/autoresearch

Andrej Karpathy open-sourced autoresearch (MIT licence) in March 2026. It gives an agent a small but real GPT training setup and lets it experiment on its own. To run it you need a single NVIDIA GPU (it was tested on an H100), Python 3.10+ and the uv package manager. Community forks cover macOS, Windows and AMD. For this lesson you only need to read it, not run it.

autoresearch/
├── prepare.py   # FIXED: constants, one-time data download + tokenizer, dataloader, evaluate_bpb()
├── train.py     # EDITABLE: GPT model, Muon + AdamW optimizer, training loop
├── program.md   # HUMAN-WRITTEN: the agent's instructions (a lightweight "skill")
├── analysis.ipynb, progress.png, pyproject.toml, uv.lock ...

Three kinds of file

This split is the most important design idea in the repo:

Role File Who changes it
What the agent may change train.py The agent: architecture, hyperparameters, optimizer, batch size, anything in this file
The ruler prepare.py (holds evaluate_bpb) Nobody during a run
The research brief program.md You, the human

In everyday engineering terms, train.py is the feature branch, prepare.py is the CI test suite you're not allowed to touch, and program.md is the ticket plus the team's conventions. As Karpathy puts it, you stop editing the Python yourself and start editing the brief.

program.md spells out the rules. The agent may not modify prepare.py, install packages or add dependencies, or change the evaluate_bpb harness. The goal is the lowest val_bpb (validation bits per byte, where lower is better). VRAM is a soft limit, and when two results are close, simpler code wins.

Why freeze the ruler? If the agent could edit the evaluation, the cheapest way to "improve" would be to change how the score is measured, not to train a better model. Keeping the scorer outside the editable area is the first line of defence. Later levels cover the subtler ways a loop can still game its score.

Walking one iteration

Here are the commands program.md tells the agent to run, with comments added:

# --- setup, once per session ---
git checkout -b autoresearch/mar5   # fresh branch per session, tagged by date
uv run prepare.py                   # the human runs this once: data + tokenizer
# The first run is always the UNMODIFIED train.py -> that's the baseline.

# --- one iteration ---
# 1. HYPOTHESIS: happens in the agent's reasoning, e.g. "try a deeper model"
# 2. EDIT: change train.py, then commit it
git commit -am "deeper model: DEPTH up by 2"
# 3. RUN: fixed-budget training, all output to a file (no tee, no streaming)
uv run train.py > run.log 2>&1
# 4. MEASURE: pull out the only lines that matter
grep "^val_bpb:\|^peak_vram_mb:" run.log
# 5. KEEP OR REVERT
#    val_bpb lower           -> keep the commit; the branch advances
#    val_bpb equal or higher -> git reset back to where this iteration started

The end of train.py prints a summary block, which is why a grep is enough to measure the result. Here is an illustrative example (your numbers will differ):

val_bpb:          0.991234
training_seconds: 300.1
peak_vram_mb:     45060.2
num_steps:        953

The agent records each result as a row in results.tsv. That file is tab-separated and deliberately left untracked by git:

commit   val_bpb   memory_gb  status   description
a1b2c3d  0.997900  44.0       keep     baseline
b2c3d4e  0.993100  48.2       keep     deeper model
c3d4e5f  0.000000  0.0        crash    custom attention, shape bug

Here is where each step lives:

Step Where it happens
Hypothesis Agent reasoning, steered by program.md
Edit train.py + git commit
Run uv run train.py > run.log 2>&1
Measure evaluate_bpb() in prepare.py, read back with grep
Keep/revert Compare with the best so far: keep the commit or git reset

Gotchas the brief handles: If the grep comes back empty, the run crashed. The agent then reads tail -n 50 run.log, fixes simple mistakes like typos or missing imports, and otherwise logs crash and moves on. If a run goes past 10 minutes, the agent kills it and treats it as a failure. The brief also says never stop: the loop keeps going until a human interrupts it.

Which agent drives the loop? The README suggests pointing Claude or Codex at program.md. Any coding agent that can edit files and run shell commands fits the same pattern, including one built on an open-weight model. The README also suggests running the agent with permissions disabled. That only makes sense on an isolated machine, never on your laptop with credentials on it.

Why a fixed time limit makes runs comparable

prepare.py sets TIME_BUDGET = 300 (five minutes). train.py imports that value and stops its training loop like this:

if step > 10 and total_training_time >= TIME_BUDGET:
    break  # the first 10 steps (compile/warmup) aren't counted

Every experiment gets the same five minutes of training on the same GPU, whatever the agent changed. Say the agent makes the model twice as large. It now completes fewer steps in the same time, so the question each run answers is always the same: what is the best model this hardware can train in five minutes? Without the fixed budget, a slower change could "win" just by training longer, and the comparison would be meaningless. A fixed budget also lets you plan: about 12 experiments an hour, or about 100 overnight.

The metric is chosen for fairness too. Bits per byte doesn't depend on vocabulary size, and the evaluation uses a fixed sequence length, so different configurations are scored the same way.

Trade-offs: A wall-clock budget ties your results to your hardware. A change that wins on an H100 may lose on a smaller GPU. And comparable is not the same as noise-free: run the same train.py twice and val_bpb will move a little. Level 2 deals with that.

Hands-on exercise

You don't need a GPU for this.

  1. Clone github.com/karpathy/autoresearch and read program.md, prepare.py and train.py.
  2. Fill in the step-to-location table above from your own reading, citing line numbers.
  3. Find TIME_BUDGET and the loop exit condition. Write one sentence on what would break if the agent could change either of them.
  4. List three things the agent may change and three it may not, quoting program.md.
  5. Write one hypothesis the way the agent would, as a commit message plus the single hyperparameter it changes.
  6. Optional, with an NVIDIA GPU: run uv sync, uv run prepare.py and uv run train.py to record your own baseline.

Key takeaways

  • Every loop has five steps: hypothesis, edit, run, measure, keep or revert.
  • Split the files into three roles: what the agent may edit (train.py), the fixed ruler (prepare.py/evaluate_bpb) and the human's brief (program.md).
  • A change is kept only if val_bpb goes strictly lower. If it is equal or worse, the change is reset.
  • A fixed five-minute wall-clock budget makes runs comparable on the same hardware, but not across different hardware and not free of noise.
  • In this setup, your job is writing the brief and guarding the ruler, not hand-editing the model.

A 6-question knowledge check follows this lesson in the app, free and without an account.

More like this