
An agent will tell you it is finished. That sentence is cheap. What matters is whether it can show you, with something you did not have to take on faith. This lesson is about building loops where the agent checks its own work against something real, fixes what the check found, and goes round again until the check passes or a limit tells it to stop.
In the last lesson the loop had three phases: gather context, take action, verify. Most beginners put their effort into the first two, writing a careful request and hoping the action is right. The people who get the most out of agents put their effort into the third. Anthropic's own advice for Claude Code says it in one line: "Give Claude something it can run that comes back pass or fail."

Why "it works" tells you so little
A language model writes the most likely next words. After a long stretch of work, the most likely next words are often "Done, everything works." That is not lying in any human sense. It is the shape that the end of a task usually takes. The model has no built-in way to know whether the page loads, the numbers add up or the email reads well, unless something in the loop goes and finds out.
Anthropic's best-practices page is blunt about this: "'All tests pass' on its own tells you very little." Ask what the tests checked. Ask for the screenshot. Ask for the line of output. When I started asking for evidence instead of reassurance, the share of work I had to redo dropped sharply. That is my experience, not a measured figure, but you will notice the same thing within a week.
What counts as a check
A check is anything the agent can run that answers yes or no without asking the agent's opinion. For software, the usual ones are:
- A test: a small program that runs a piece of code and compares the result with the right answer.
- A build: compiling or bundling the project, which fails loudly if something is broken.
- A screenshot: the agent opens the page in a real browser and looks at it, at phone width and at desktop width.
- A linter: a tool that flags common mistakes and style problems.
You do not need code to use this idea. A check can be a word limit, a list of required sections, a reading-level score, a rule such as "every link opens," or a short checklist the agent runs against its own draft and reports on line by line. The test is simple: could two people run the same check and get the same answer? If yes, it is a check. If it depends on taste, it is a review, which comes later in the loop.
Step one: say what done means
A loop needs a finish line, and the agent cannot draw it for you. Before any work starts, I write one sentence that says what done looks like. "The contact page loads on a phone with no sideways scrolling, and the form sends a test message to my sandbox inbox." That sentence becomes the check.
Claude Code's Plan mode is built for this step. The agent reads the project and proposes how it will do the work, without changing anything. I read the plan, correct it, and only then let it build. Planning before code is the most common advice in Anthropic's guidance and in the practitioners I follow, and it costs about two minutes.
Step two: build one small step
Small steps make checks useful. If the agent changes forty files and the build fails, the error could be anywhere. If it changes one thing and the check fails, the cause is obvious. I ask for one piece at a time: the layout, then the form, then the email it sends. Each piece gets its own check before the next one starts.
Step three: feed back the exact error
When a check fails, the next round is only as good as what goes back into the loop. "It broke" gives the agent nothing. The failing line of a test, the exact error message or a screenshot showing the overlap gives it a target. In a well-built loop the agent reads that output itself. When I am in the loop, I paste the error rather than describe it.
This is where an agent earns its keep. A person who gets three error messages in a row starts to lose heart. An agent reads the third with the same attention as the first. That patience is real, and so is its limit, which brings us to the cap.
Step four: a reviewer who never saw the work
Passing checks is necessary, not enough. A page can load perfectly and still say the wrong thing. So the last check before I keep a piece of work is a review by a fresh agent, one that was not part of the conversation that produced it.
Anthropic explains why this works: a reviewer that never saw the conversation "isn't swayed by the reasoning that produced the code." The agent that did the work has its own explanations loaded in its context, and it tends to agree with them. A fresh one only sees the result. In my own setup I keep a reviewer whose only job is to find what is wrong. It does not write anything. It reads the change, looks for the case that breaks it and reports back. I fix what it finds, or I explain to myself why it is wrong. Then the step gets saved.
The same idea appears in Anthropic's guide to building agents as the evaluator-optimizer pattern: one model produces, another evaluates and sends feedback, and they loop. You do not need the jargon. You need a second pair of eyes that has no stake in the first draft.
The cap: when the loop should stop
Every loop needs a way to end other than success. Without one, an agent can go round and round the same failure, burning time and money, each attempt a slight variation on the last.
Anthropic's guidance gives a simple human rule: "Watch the first minute, press Esc the moment Claude drifts, and after two failed corrections run /clear and write a better prompt." Two corrections, then start fresh with what you learned. When the agent runs on its own, the cap has to be built in. My rule of thumb is three: if the same check fails the same way three times, the agent stops and tells me what it tried. Usually the problem is my instructions, not its effort.
The harness can enforce a cap too. Claude Code lets you add a Stop hook, a small script that runs when the agent tries to finish and can refuse to let it stop until a check passes. Anthropic's example runs a type check and blocks the finish if it fails. The harness also has its own backstop: if a Stop hook blocks the finish eight times in a row, Claude Code ends the turn anyway. Hooks get a whole lesson next, Hooks: rules the harness enforces.
The same page lists habits you can now drop. You no longer need to type "loop until done" in capital letters or tell the model to "think harder." A goal with a real check, and a hook or a /goal command to hold it, does the job better.
Loops that run on their own
Once a loop works with you watching, you can run it without you. Claude Code has a headless mode, started with claude -p, that takes a task, runs the loop and exits. Two settings matter more than any others when nobody is watching: --max-turns caps how many rounds it may take, and --max-budget-usd caps what it may spend. With JSON output, the result even reports what the run cost.
Be careful with schedules. A loop that runs every five minutes fires 288 times a day, about 8,640 times in a 30-day month. A check that costs a few cents each time becomes a real line on a bill. Start with once a day and raise the frequency only when a result justifies it.
My loop, start to finish
Here is how I build a change to one of my sites, with the names taken out:
- I write the goal and the check in one line, in a progress file that lives with the project.
- The agent plans in Plan mode; I correct the plan.
- It builds one step on my local copy, never the live site.
- It checks: the page renders in a real browser, at phone width and desktop width, and the screenshots come back to me.
- A fresh reviewer reads the change and hunts for problems.
- The step is saved as its own commit, so a single command undoes exactly that step if I change my mind.
- The progress file gets a line saying what is done and what comes next.
- Only after all of that, and my own look at the result, does anything go to the live site.
None of these steps is clever. Together they mean I rarely find out about a problem from a visitor.
Loops for a practice that has no code
A monthly newsletter is a fine place to practise. Write a checklist once: under 400 words, one clear subject, the office phone number spelled out the same way as on your website, no client stories, every link tested. Ask the agent to draft, then to run the checklist against its own draft and report each item as pass or fail with the evidence. Then open a fresh chat, paste only the draft and the checklist, and ask it to find anything that fails. You have just built a loop with a check and a fresh reviewer, and it took ten minutes.
Try it
Take any short piece you need to write this week. Before asking for a draft, write two lines: what done means, and how anyone could check it. Give both to the agent. When it says it is finished, ask it to show you the check, item by item. Notice how often the first "done" was not quite done.
Further reading
- Best practices for Claude Code, Anthropic. Checks, evidence, fresh reviewers and when to start over.
- Run Claude Code programmatically, Anthropic. Headless mode, turn limits and budget caps.
- Building effective agents, Anthropic. The evaluator-optimizer pattern.
- Evaluating and improving agentic applications, DeepLearning.AI. A free course on measuring whether agents work.
- Harnesses and the agent loop. The loop this lesson strengthens.
Facts in this lesson checked November 24, 2026.
This lesson's video has its own page
Sources
- “Best practices for Claude Code.” Anthropic. "Give Claude something it can run that comes back pass or fail"; "'All tests pass' on its own tells you very little"; a fresh reviewer "isn't swayed by the reasoning that produced the code"; a Stop hook example, and the turn ending after eight blocks in a. https://code.claude.com/docs/en/best-practices (accessed Nov 24, 2026).
- “Automate workflows with hooks.” Anthropic. Stop hooks that block the finish until a check passes; exit code 2 blocks. https://code.claude.com/docs/en/hooks-guide (accessed Nov 24, 2026).
- “Hooks reference.” Anthropic. The Stop event and the other points in a session where hooks run. https://code.claude.com/docs/en/hooks (accessed Nov 24, 2026).
- “Run Claude Code programmatically.” Anthropic. claude -p, --max-turns, --max-budget-usd, and JSON output that reports total cost. https://code.claude.com/docs/en/headless (accessed Nov 24, 2026).
- “Building effective agents.” Anthropic, Dec 19, 2024. The evaluator-optimizer pattern; agents compound errors, so add stopping conditions. https://www.anthropic.com/engineering/building-effective-agents (accessed Nov 24, 2026).
- “Evaluating and improving agentic applications.” DeepLearning.AI. Free course. https://www.deeplearning.ai/courses/evaluating-and-improving-agentic-applications (accessed Nov 24, 2026).
- “Agentic AI.” DeepLearning.AI. Free course covering reflection and evaluation loops. https://www.deeplearning.ai/courses/agentic-ai (accessed Nov 24, 2026).
- “Stop Prompting Claude Code: Agent Loops in 5 Minutes.” CloudYeti (YouTube), Jun 15, 2026. The video on this lesson. https://www.youtube.com/watch?v=z9uVb0NUgqk (accessed Nov 24, 2026).
Study guide
Key terms
- Check: something the agent can run that answers yes or no: a test, a build, a screenshot, a word count
- Review: a judgment that depends on taste, done by a fresh agent or a person
- Fresh reviewer: an agent that never saw the work being made, so its reasoning does not sway it
- Cap: a limit that ends the loop without success: a number of tries, turns or dollars
- Stop hook: a script that runs when the agent tries to finish and can refuse until a check passes
- Headless mode: running Claude Code without a chat window, with claude -p
Facts that move (checked November 24, 2026)
- "Give Claude something it can run that comes back pass or fail" (Anthropic best practices)
- After two failed corrections, /clear and write a better prompt (Anthropic best practices)
- Headless caps: --max-turns and --max-budget-usd (Anthropic)
- Claude Code ends the turn after eight Stop-hook blocks in a row (Anthropic best practices)
The loop, in order
- Say what done means, in one line
- Plan before building
- Build one small step
- Run a check that comes back pass or fail
- Feed back the exact error on a fail
- Get a fresh review
- Save the step, then start the next
- Stop after the same failure three times and ask a person
Check what you learned
10 questions from this lesson. Answers are checked as you go; nothing is recorded.
1 / 10
According to Anthropic's advice, what is the most useful thing to give an agent?
Hint
Yes or no, not an opinion.
Anthropic: "Give Claude something it can run that comes back pass or fail."
2 / 10
Why does an agent often say "Done, everything works" when it is not?
Hint
Likely words, not knowledge.
The model writes the most likely next words. Without a real check in the loop it has no way to know whether the work works.
3 / 10
Which of these is a check rather than a review?
Hint
Two people, same answer.
A word count gives the same answer to anyone. The others depend on taste, which is a review.
4 / 10
What should happen before any building starts?
Hint
A finish line.
A loop needs a finish line. One sentence describing done becomes the check.
5 / 10
Why build in small steps?
Hint
Where did it break?
If one thing changed and the check fails, you know where to look. Forty changes and one failure is a hunt.
6 / 10
What should go back into the loop after a failed check?
Hint
A target, not a mood.
The exact output gives the next round something to aim at.
7 / 10
Why use a reviewer that never saw the conversation?
Hint
No stake in the first draft.
Anthropic: a fresh reviewer "isn't swayed by the reasoning that produced the code."
8 / 10
After how many failed corrections does Anthropic suggest clearing and writing a better prompt?
Hint
Fewer than you think.
"After two failed corrections run /clear and write a better prompt."
9 / 10
In headless mode, which two settings cap a run nobody is watching?
Hint
Rounds and money.
--max-turns limits rounds; --max-budget-usd limits spend.
10 / 10
About how many times does a loop that runs every five minutes fire in a 30-day month?
Hint
12 an hour, 24 hours, 30 days.
12 x 24 x 30 = 8,640. Small costs per run add up fast.
Worth another look