An agent is an LLM with tools and a goal, run in a loop: it picks an action, sees the result, and decides the next step instead of answering in one shot.
One pass through the loop
- Define the goal and give the model the current task state.
- The model reasons about that state and emits a tool call.
- Your runtime executes the tool and captures the observation.
- The observation is added to the context.
- The model decides whether the task is complete. If not, the loop repeats.
The loop ends when the model reports it is done or when a stop condition fires, such as a maximum iteration count or an exhausted budget.
Take debugging code: the model reads the error message, queries the relevant documentation, then uses what it found to refine the next action. Each iteration builds on the last.
The adaptivity is the point. Without observations fed back into the context, the model has nothing to base its next action on, and the answer is generic and less suited to the task. The loop lets it try one path, see the result, and switch to another.
Model judgment as the transition function
A REPL or an event loop driving a state machine is the closest software analogue. The difference is that the transition function here is the model's judgment, so identical input can lead to different paths. The loop is therefore harder to predict and test.
Agent loop versus a single prompt or a scripted workflow
| Task shape | Approach | Why |
|---|---|---|
| Later steps depend on earlier results | Agent loop | A single prompt cannot know follow-up questions before it sees the first results. |
| Open-ended: research, debugging, multi-system commands | Agent loop | The model can explore paths and adjust. |
| Known, fixed sequence of steps | Scripted workflow | Cheaper and more reliable, with no nondeterminism. |
Compounding errors and unbounded spend
Each step's output feeds the next input, so an early mistake shifts every later step. Without limits the model can also consume an unbounded number of tokens. Cap iterations and set guardrails.
Runaway calls on a simple request
Example scenario: one request triggers about 12 LLM calls and 8 tool calls, takes about 25 seconds and costs about $30. All figures are illustrative. The usual cause is a missing execution budget: nothing tells the agent when to stop.
- Measure LLM and tool calls per request.
- Log execution time and cost for each request.
- Look for repeated tool calls whose results could be cached.
Set execution budgets on calls and tokens, and route simple requests to smaller models. Monitor call counts, tokens, latency, cost and success rate.