Every agent, regardless of framework, runs some version of the same loop: reason about the current state, decide on an action, execute it safely, observe the result, and repeat until done.
The Agent Loop, Step by Step
Goal
↓
Context (goal + conversation history + prior results)
↓
LLM reasoning/decision → "What should happen next?"
↓
Tool selection → LLM picks a tool + arguments
↓
Application validation → is this tool call allowed? are arguments valid?
↓
Tool execution → the actual function/API call runs
↓
Observation → the tool's result is read back
↓
State update → history/context updated with the new information
↓
Next action OR final response
The "application validation" step is not optional in a well-built system — the LLM's chosen tool call is a request, not a command that should execute unchecked. See Tool Validation.
Worked Example
User: "What's the total of my last 3 orders?"
Iteration 1:
LLM decides: call get_recent_orders(user_id=42, limit=3)
App validates: user_id matches authenticated session ✓
Tool runs → returns [{"id": 101, "total": 450}, {"id": 102, "total": 300}, {"id": 103, "total": 275}]
Observation added to context
Iteration 2:
LLM decides: it now has enough information — no more tools needed
LLM generates final answer: "Your last 3 orders total ₹1,025."
Why the Loop Terminates
A well-built agent stops when the LLM determines the goal is satisfied and returns a final answer instead of another tool call — but production systems also enforce a hard maximum iteration count as a safety net, since a model can occasionally get stuck repeating similar tool calls. See Infinite Agent Loops.
Common Mistakes
- Executing tool calls directly from the LLM's output with no validation layer — a missing safety step, not a minor detail
- No maximum iteration limit, risking a runaway loop that burns cost and time with no progress
- Not persisting intermediate state, so a crash mid-loop loses all progress instead of allowing resumption
Interview Relevance
Expect to be asked to draw or describe this loop from memory in an agentic AI interview — and to explain specifically why the validation step sits between "LLM decides" and "tool executes," not before or after.
Practice Question
Walk through the agent loop for a task: "Cancel my subscription and email me a confirmation." Where would you insert a human-approval step, and why?