Topic 3
The loop: think, act, observe
An agent doesn't produce its answer in one go. It runs a loop: decide on a step, take it, look at the result, decide again.
AI Engineering (Ch. 6, "Reflection and error correction") traces the common form to ReAct, from Yao et al. (2022), which interleaves reasoning and acting. At each step the agent explains its thinking, takes an action, then analyses the observation, until it decides the task is finished. The paper reported that on two interactive benchmarks, ALFWorld and WebShop, this beat imitation and reinforcement learning methods by 34 and 10 points of absolute success rate, with only one or two examples in the prompt.
You've met the mechanics already: the tool-call loop in Module 03, where the model emits a tool call and your code runs it. An agent is that loop, run until the model says it's done.
Predict first
Scroll sideways to see the whole diagram
Each turn, the model reads the whole context and writes its next step. ReAct (Yao et al., 2022) interleaves a reasoning trace with each action.
Text version
- Each turn, the model reads the whole context and writes its next step. ReAct (Yao et al., 2022) interleaves a reasoning trace with each action.
- If the step is a tool call, the model doesn't run it. The harness, your code, parses it, checks it, and executes it.
- The result is appended to the context as an observation, and the loop goes round again.
- It ends when the model writes a final answer instead of a tool call.
- Or when the harness ends it. Anthropic recommends stopping conditions such as a maximum number of iterations.
The loop has two exits. The model can end it by answering. And the harness can end it: on an error it can't recover from, a step limit, a cost limit, or a timeout. Anthropic's "Building effective agents" (2024) recommends stopping conditions such as a maximum number of iterations, and stresses that agents need “ground truth” from the environment at each step: the tool results, not the model's assumptions about them.
Quick check