BuildBot

The Agent Loop

The agent loop

Lesson 1 of 3

What you'll learn

  • Understand what makes something an "agent" vs. a single prompt
  • Trace the think → act → observe → repeat cycle
  • See where the loop terminates

A single API call answers one question. An agent keeps going: it can decide to take an action, see the result, and decide what to do next. Strip away the buzzwords and an agent is a loop:

  1. Think — the model reads the conversation and decides what to do.
  2. Act — if it wants information or to change the world, it requests a tool call.
  3. Observe — your code runs the tool and feeds the result back.
  4. Repeat — until the model produces a final answer instead of a tool call.
user goal ─▶ [ think ] ─▶ wants a tool? ─yes─▶ [ run tool ] ─▶ [ observe ] ─┐
                  ▲                                                          │
                  └──────────────────────────────────────────────────────-─┘
                            no ─▶ final answer ─▶ done

The model never runs code itself. It asks; your program executes and reports back. That boundary is what keeps agents safe and debuggable.

Always bound the loop

A loop that never stops is a runaway agent. Cap the number of iterations so a confused model can't spin forever — a hard stop is your safety net.

The challenge implements the loop with a fake model that "decides" to use a calculator until it has an answer. Run it and watch the cycle.

A minimal agent loop

The model returns either a tool request or a final answer. Run it and follow the iterations.

Loading editor…

Next: how a real tool call is shaped, and how you dispatch it.

Sign in to save your progress across devices.