---
title: "When an agent runs the loop"
lesson_id: "07"
---

# When an agent runs the loop

- **Lesson ID:** 07
- **Goal:** Teach the autoresearch keep-if-improved loop, correct the post where the repo disagrees, and describe what you can run safely.
- **Human lesson:** [07-when-an-agent-runs-the-loop.html](07-when-an-agent-runs-the-loop.html)

## Prerequisites

- Lesson 04 or 06, so a training loop is already familiar.

## Inputs, outputs, and artifacts

- **Inputs:** The question of what happens when an agent runs the training loop.
- **Outputs:** A clear loop description with the real metric, and a cautious run path.
- **Artifacts:** Progressive mermaid diagrams of edit, train, score, and keep or discard.

## Agent build steps

1. Point at [autoresearch](https://github.com/karpathy/autoresearch).
2. Correct the inspiring post: Hao described this as an agent running stop 4 (nanoGPT). The README says the trainer is a simplified single-GPU nanochat.
3. Name the three files: `prepare.py` (fixed), `train.py` (agent-edited), `program.md` (human-edited).
4. Teach the loop: edit, train for five minutes of wall clock, read `val_bpb`, keep only if it improved. Hao said "validation loss"; the repo metric is `val_bpb`.
5. State about 12 experiments per hour and about 100 overnight, from the README.
6. Warn that a keep-if-improved loop can overfit one validation set. A held-out check is a good extra test.
7. Official platform is a single NVIDIA GPU. Link the macOS and other forks listed in the README.
8. Give `uv sync`, `uv run prepare.py`, and one manual `uv run train.py`. Warn about unsupervised agent permissions.
9. Credit autoresearch rounds 1 and 2 on the nanochat leaderboard (1.80 hours, then 1.65 hours).

## Constraints

Keep the language simple, friendly, and literal. No quizzes, no em dashes. Brand Edge FDE only. Do not tell the reader to disable safety permissions lightly. Do not require an H100 to finish the lesson.

## Key concepts

- Autoresearch lets an agent edit train.py, train for five minutes, and keep the change only if validation bits per byte improves.
- The training code is a simplified single-GPU nanochat, not nanoGPT, even though the inspiring post described it as running stop 4.
- The human edits program.md. The agent edits train.py. prepare.py stays fixed.
- A five-minute budget gives about 12 experiments per hour, or about 100 overnight, on one GPU.
- The official repo needs NVIDIA GPU support. Community forks exist for other platforms.
- Autoresearch rounds 1 and 2 are credited on the nanochat leaderboard for the 1.80 hour and 1.65 hour records.

## Takeaways

- Describe the loop with the metric the repo actually uses, val_bpb, not only the words validation loss.
- If you run it, start with one manual uv run train.py, and think carefully before you give an agent unsupervised access.
- Keep a held-out check in mind. A loop that only watches one validation set can fit that set.

## Acceptance checks

- The nanochat-not-nanoGPT correction and the val_bpb correction are present.
- Overnight count and the five-minute budget are sourced to the README.
- Agent brief link is present.
- [ ] Course complete. Return to the [course home](./) and the [course SKILL.md](SKILL.md).
