Map

AI Engineer Track · Ai Agents

The Agent Loop: Plan, Act, Observe

Foundational~27 min

The Agent Loop: Plan, Act, Observe

The agent loop is the basic engine underneath every agent: the model proposes a step, the system carries it out, the result is fed back in as an observation, and the cycle repeats until the task is finished or a stopping condition is hit. Every agent framework, no matter how sophisticated it looks on the surface, is this loop plus supporting plumbing wrapped around it.

Why this matters

Understanding the loop itself, not a framework's version of it, is what lets you build, debug, and safely constrain an agent, because essentially every dangerous or wasteful failure mode, running forever, losing track of what it already tried, burning unbounded cost, lives inside this loop and is fixed by adding limits directly to it.

The agent loop: plan, act, observe

The agent loop is the core pattern underneath every agent: the model plans its next move, acts by calling a tool, observes the result, and repeats until the task is actually done. The key implementation detail is that the tool's result is appended back into the conversation the model sees, not returned directly to the user, so the model continues reasoning with it exactly as if it had just learned a new fact mid-thought. A maximum-turns guard sits around the whole loop specifically to prevent it from running away, since without one a confused model has no built-in reason to ever stop calling tools.

Every agent, no matter which framework built it, is fundamentally this one loop. Understand it directly and every framework becomes a layer of convenience on top of something you already understand.

Stopping conditions and turn budgets

An agent with no way to stop is a runaway cost waiting to happen, so the loop has to be bounded deliberately. A natural stop occurs when the model's own response contains no further tool call, meaning it has decided the task is complete. A turn budget caps the loop at a maximum number of turns, after which the system stops and returns whatever it has, or reports failure outright, regardless of whether the model thinks it is done. A goal check adds an explicit evaluation step that confirms the answer is genuinely complete before the loop is allowed to stop at all, catching cases where the model declares victory prematurely. The turn-budget cap is non-negotiable in production: without it, a confused or looping model runs indefinitely, and every additional turn is billed regardless of whether it is making any real progress.

Bounding the loop with an explicit turn budget is the difference between a working agent and an agent that quietly becomes an unbounded bill.

Worked example

An agent tasked with finding a specific fact in a document that does not actually contain that fact will, without a turn budget, keep calling its search tool indefinitely trying slightly different queries, since nothing in the loop itself tells it to give up; with a turn budget of ten, it instead stops after ten attempts and reports that it could not find the answer, which is a far better outcome than an unbounded bill with no result at all.

Practice

Implement the loop by hand, with no framework, including a maximum-step guard, a token budget, and an explicit termination condition. Confirm the loop completes a three-step task correctly, and demonstrate it terminating gracefully on a task deliberately designed to make it loop forever.

Interview checks

  • Why does feeding a tool's result back into the model's own context matter more than just returning it to the user?
  • What specifically happens to a loop that has no turn-budget guard?
  • Why isn't the model's own 'I'm done' signal sufficient on its own as a stopping condition?

Common failure modes

  • No maximum-step guard, leaving the loop able to run indefinitely at real cost.
  • No token or cost budget, letting a stuck agent silently burn money with no upper bound.
  • Losing observation context between turns, causing the model to repeat the same failed action over and over.
  • Termination conditions written in a way that never actually triggers in practice.

Resources

Expert signals

Being able to write the agent loop from scratch, guarding it with a maximum-step count, a cost budget, and a genuine termination condition together, and debugging an agent by inspecting the loop's state at each individual step rather than only staring at its final output, is the baseline fluency everything else in agent engineering builds on top of.

Confidence

high