Model an agent loop
Trace a task through think, act, observe - and see why the environment, not the model, is the source of truth.
- Describe the stages of a tool-using agent loop
- Explain why each step needs ground truth from the environment
- Read and parse a ReAct-style trace
In this track, loop engineering means designing the repeated cycle that lets an AI system make progress on a task it can’t finish in one reply. Each turn of the loop:
- Observe - the current state: the goal, what’s been done, the latest result.
- Think - the model decides what to do next.
- Act - your code executes the chosen tool call.
- Check - the result goes back into the context; is the goal met, or should the loop stop?
This pattern was popularized as ReAct (Yao et al., 2022): interleaving reasoning (“Thought”) with actions and their observations. The crucial word is observation: Anthropic’s Building effective agents stresses that agents need ground truth from the environment at every step - test results, API responses, file contents - rather than assuming an action worked.
Try it
Up to 15 XPOne loop, step by step
An agent is asked to fix a failing test. Step through the loop and predict the marked steps.
1failing = 2
2step = 0
3while failing > 0 and step < 5:
4 step += 1
5 failing -= 1 # pretend each edit fixes one test
6 print(f"step {step}: {failing} failing")step 1: 1 failing step 2: 0 failing
Key takeaways
An agent loop repeats observe → think → act → check until done or stopped.
The model proposes; your code executes, records and decides whether to continue.
Verify progress with ground truth from the environment, not the model’s claims.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Parse a ReAct trace
Each line starts with Thought:, Action:, Observation: or Final Answer:. Pair each Action with the Observation that follows it and print step N: ACTION -> OBSERVATION. Then print final: ANSWER (or final: none) and actions: K.
- A finished trace
- No final answer
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Run a loop against an environment
Line 1 is the step budget. Line 2 is a JSON object mapping tool calls to results (the environment). Each following line is the model’s next action. Run the loop:
- an action starting with
answer:printsfinal: TEXTand stops; - if the budget is already used up, print
stopped: step budget reachedand stop; - otherwise print
step N: ACTION -> RESULT, usingerror: unknown tool callfor actions not in the environment (the model sees the error and can recover).
If the actions run out without an answer, print stopped: no answer.
- Recovers from a bad call
- Runs out of budget
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…