Lesson 42.1 · 1 of 4 · 5 min read · Reviewed 24 Nov 2026
The agent loop
By the end, you can
- Describe the agent loop as gather context, take action, check the work, repeat.
- Explain why checking work is part of the loop, not an afterthought.
Chapter 0 showed the mechanics: request, tool_use, tool_resulttool_result: The content block you send back with the output of a tool, matched to the request by tool_use_id., repeat. This lesson is about what a good loop does on each turn.
Gather, act, check
- Gather context. Read the files, search the docs, look up the order. Good agentsAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. look before they act.
- Take action. Edit a file, call an API, write a draft.
- Check the work. Run the tests, validate the JSON against a schema, lint the code, or ask a second call to review.
- Decide. Continue with what it learned, or stop.
Why checking is the heart of it
A model that acts without feedback is guessing. A failing test or a schema error is a cheap, objective signal the model can act on. That’s why coding agents work well: tests give them a check on every turn. When your domain lacks an obvious check, build one. A rubricRubric: A written set of criteria and score levels that a grader applies to each output. graded by a second call is better than nothing.
Who decides to stop
The model signals it’s finished with stop_reason: "end_turn". Your code should also stop the loop on its own terms: a turn limit, a budget, or a check that passes. Lesson 42.3 covers how.
Key points
- The loop: gather context, take an action, check the result, and decide whether to continue.
- Checking can be a test run, a linter, a schema check or a second model call.
- The loop ends on a stop condition you choose, not only when the model says it's done.
Close the loop with a test check
Build a gather, act, check loop where real tests decide whether the agent's fix is done.
You will have: A loop that tries candidate fixes for a buggy function, runs tests after each, and stops on the first that passes.
Steps
- Read the buggy function and its three tests.
- Gather: print the failing test names before acting.
- Act: apply the next candidate fix, standing in for the model's edit.
- Check: run the tests and collect failures as feedback for the next attempt.
- Decide: stop when all tests pass or when the attempt limit is reached.
Done when
- The first candidates fail with named tests in the feedback.
- The loop stops as soon as all three tests pass.
- The attempt limit would stop the loop even if no fix worked.
Open in the Playground No API key needed.
Starter code (Python)
def discount(price, percent): # buggy: treats percent as a fraction
return price - price * percent
tests = {"ten percent": lambda f: f(200, 10) == 180, "zero": lambda f: f(50, 0) == 50,
"never negative": lambda f: f(10, 150) == 0}
candidates = [
lambda price, percent: price - price * percent / 100,
lambda price, percent: max(0, price - price * percent / 100),
lambda price, percent: round(price * (1 - percent), 2),
]
def run_tests(f):
return [name for name, t in tests.items() if not t(f)]
# TODO: gather (print failing tests), then loop over candidates: act, check, decide
print("failing before:", run_tests(discount))
Show a solution
def discount(price, percent): # buggy: treats percent as a fraction
return price - price * percent
tests = {"ten percent": lambda f: f(200, 10) == 180, "zero": lambda f: f(50, 0) == 50,
"never negative": lambda f: f(10, 150) == 0}
candidates = [
lambda price, percent: price - price * percent / 100,
lambda price, percent: max(0, price - price * percent / 100),
lambda price, percent: round(price * (1 - percent), 2),
]
def run_tests(f):
return [name for name, t in tests.items() if not t(f)]
print("gather: failing before:", run_tests(discount))
MAX_ATTEMPTS = 3
for attempt, fix in enumerate(candidates[:MAX_ATTEMPTS], 1):
failures = run_tests(fix) # check
if not failures:
print(f"attempt {attempt}: tests passed, stop")
break
print(f"attempt {attempt}: still failing {failures}, feed back and continue")
else:
print("attempt limit reached: report what is still failing")