BAA 101 · Chapter 02

The agent loop and its exits

2 min read

The principle

An AgentA program that uses a language model to decide what to do (search, call a tool, answer, refuse), in a loop, with stopping rules.See in the glossary → is a loop: the model reads the message, chooses to answer or to call a tool, reads the result, then starts again. A loop is designed by its exits first: you write how it stops before writing what it does.

Why it matters

A loop without exits spins idle, spends without limit, or crashes on the first tool error. And without rules on what may follow what, an agent that decided to refuse can end up answering anyway.

The mechanism

  1. The loop: message → model → Tool callThe model's request to run a tool with specific arguments. That's what you read in the trace, not the final answer.See in the glossary → → result → model → … → answer. A framework can run it for you, or you write it; either way, its exits are yours.
  2. Exits first: an answer, a call identical to the previous one, a BudgetThe maximum number of calls an agent loop may make, checked before each call, never after.See in the glossary → reached, a tool error.
  3. The budget is counted before the call, never after: there’s never one call too many.
  4. Every stop says why, in one readable sentence.
  5. Transitions are declared: a table “state + event → next state”. Anything not in it is refused, and the state doesn’t move.
  6. The shape is chosen by counting: a fixed chain costs one model call, a loop one call plus a possible retry, a reflection two to three.

In the field

  • AWS Scholars, chapter 2: the agent picks a tool, the framework runs it, the result goes back to the model, until the final answer. A missing tool doesn’t crash the loop: it’s simply missing.
  • Dev3Pack, Session 5: a loop with four exits. The bot’s first attempt crashed on an unknown tool instead of refusing it.
  • Dev3Pack, Session 8: no arrow leads from “refusing” to “answering”, so a RefusalAn explicit "I can't answer": no citation, zero confidence, human review. A state, not a crash.See in the glossary → never becomes an answer.

The question to ask

How does this loop stop, and what do you read when it stops?