The Agent Loop

13 min read

An agent does not answer in one shot. It thinks, uses a tool, looks at the result and thinks again. This lesson shows the loop step by step, how it stops, and what goes wrong when it does not.

New words

Agent loop
Code that repeats: ask the model, run its tool, send back the result.
ReAct
A pattern where the model reasons first, then acts, then reasons again.
Tool call
A request from the model to run one of your functions.
Max steps
A limit on how many times the loop may call the model.
Stop condition
A rule that ends the loop, such as a final answer.
History
The full list of messages sent to the model on every call.

What you will learn

  1. The problem: one answer is not enough
  2. The loop in simple steps
  3. ReAct: reason, then act
  4. When does the loop stop?
  5. Why does cost grow with every step?
  6. Try it: a weather agent, one step at a time
  7. What goes wrong, and how to handle it
  8. In a real system
  9. Interview tip
  10. Key takeaways

10 quiz questions at the end.

Start this lesson

The full lesson, with interactive demos and the quiz, opens after you sign in with Google. It is free.

All lessons · Back to the roadmap