Loading
0x00Lesson 1 of 8

Model an agent loop

Trace a task through think, act, observe - and see why the environment, not the model, is the source of truth.

20 min 6-question quiz 2 code exercises
By the end of this lesson you can
  • 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:

  1. Observe - the current state: the goal, what’s been done, the latest result.
  2. Think - the model decides what to do next.
  3. Act - your code executes the chosen tool call.
  4. 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 XP

One loop, step by step

An agent is asked to fix a failing test. Step through the loop and predict the marked steps.

Message 1 of 7Predicted 0/0
Controller (your code)
Model
Environment
loop.py
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")
Output
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.

Exercise 1

Parse a ReAct trace

+25 XP

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
main.py
Loading editor…

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.

Exercise 2

Run a loop against an environment

+25 XP

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: prints final: TEXT and stops;
  • if the budget is already used up, print stopped: step budget reached and stop;
  • otherwise print step N: ACTION -> RESULT, using error: unknown tool call for 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
main.py
Loading editor…

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…