Agent Loop
The agent loop is the repeating cycle of observing state, deciding an action, acting, and evaluating the result.
Prerequisites
What Is the Agent Loop?
A single LLM call takes an input and produces an output, once. An agent needs to do more than that — take an action, see what happened, and decide what to do next, possibly several times, until the goal is reached. The agent loop is that repeating cycle.
Observe State
Current InputThe goal, conversation so far, and any prior results.
Decide Next Action
Model DecidesRespond directly, or call a tool for more information.
Act (respond or call a tool)
Takes ActionExecutes the chosen response or tool call.
Observe Result
New InformationThe action's outcome, added back into the context.
Repeat or Finish
Stopping CheckEnds when enough information exists, or a limit is hit.
Key Idea
The loop is what turns a model from something that answers a question into something that pursues a goal across multiple steps.
How a Single Step Works
- The model is given the current state — the goal, the conversation so far, and the results of any previous actions.
- The model decides what to do next: respond directly to the user, or call a tool to get more information or take an action.
- If a tool is called, its result is added to the context, and the loop runs again with that new information available.
- The loop ends when the model decides it has enough information to give a final answer, or when a limit — like a maximum number of steps — is reached.
Warning
Without a step limit or clear stopping condition, an agent loop can run far longer than intended, repeating unproductive actions or accumulating cost.
A Real-World Example
A user asks an agent to "find the cheapest flight to Tokyo next month and summarize the options." The agent can't answer this in one step — it needs to call a flight-search tool, look at the results, possibly call the tool again with refined dates, and only then summarize. Each of those is one pass through the agent loop, with the result of one step becoming part of the input to the next.
Common Mistakes
Not setting a maximum number of loop iterations
Without a hard limit, a stuck or confused agent can loop indefinitely, wasting time and cost.
Letting the loop run without visibility into each step
Logging each observe-decide-act cycle makes it possible to debug why an agent took the actions it did.
Assuming every task needs a loop
Simple, single-step tasks are better served by a direct model call — the agent loop adds latency and complexity that isn't always necessary.
Not handling a tool failure inside the loop
A tool call can fail or return an error — the loop needs a path for the agent to recognize that and adjust, not just crash or repeat blindly.
Interview Question
What is the agent loop, and why does an agent need one instead of a single model call?
The agent loop is the repeating cycle of observing the current state, deciding on an action, acting — which might mean calling a tool — and then observing the result before deciding what to do next. A single model call can't handle multi-step tasks, because it produces one output and stops; the loop is what lets an agent gather information, act on it, and reassess across multiple steps until the goal is actually reached. In practice, you need a clear stopping condition and a maximum number of iterations, since without one a stuck or confused agent can loop far longer than intended.
What an interviewer may ask next
- What happens if an agent loop has no maximum iteration limit?
- How does a tool call fit into a single pass through the agent loop?
- Why might a simple task be better handled without an agent loop at all?
Explain It in 30 Seconds
The agent loop is the repeating cycle an agent runs through — observe the current state, decide on an action, act (possibly by calling a tool), then observe the result and decide again. It's what lets an agent handle multi-step tasks a single model call can't, since each step's result becomes input to the next. In practice it needs a clear stopping condition and a maximum number of iterations, or a stuck agent can loop indefinitely.