The agent loop
Reason, act, observe, repeat — the ReAct pattern, why every framework is a variation on it, and why termination is the hard part.
The loop that made agents work is ReAct — reason + act. The model writes a thought, chooses an action, the runtime executes it, and the observation is appended to the context. Then the model does it again, now reading everything that came before. It is almost embarrassingly simple, and it is the basis of essentially every agent framework.
Start here
The intelligence is still next-token prediction. What the loop adds is that each prediction can change the context — not just by adding more text, but by reaching out and bringing back a fact the model did not have. The loop is the mechanism by which a frozen model affects the world and then learns what happened.
One goal, one loop — the step budget decides if it finishes
Goal: Find the top three expenses and tell me if they blow the 1000 budget.
think: I need the expense rows first.
act: read_csv("expenses.csv")
observe: 8 rows returned
think: Sort them by amount, largest first.
act: sort(rows, "amount", desc)
observe: top: 420, 310, 275
think: Add the top three.
act: calc(420 + 310 + 275)
observe: 1005
think: Compare that to the budget.
act: calc(1005 > 1000)
observe: true
think: I can answer now.
act: finish("over budget by 5")
observe: done
Every step re-reads the whole context, so the 5th call is not 5× the 1st — it carries everything the loop has seen. That is why loop length, not question length, drives an agent's cost and latency. Counts are illustrative.
Slide the step budget and watch the task either finish or run out of steps mid-way. Both are normal; what matters is that the loop has a defined reason to stop.
One step, three moves
- Reason — the model writes what it is thinking and what it wants to do. This is where the plan lives, in plain language.
- Act — it emits a structured call to a tool: a name and typed arguments. The runtime — not the model — executes it.
- Observe — the tool's result is appended to the context. Errors are results too, and they are the loop's main feedback signal.
Termination is a decision
A loop that never stops is an infinite loop with a bill attached, so the runtime owns the exit conditions:
- Success — the model calls a
finishaction, or produces an answer with no further tool calls. - Step budget — a hard cap on iterations, so a confused agent cannot run forever.
- Cost or time budget — the same cap in the currency that actually hurts, since each step re-reads the whole context.
- No progress — the agent repeats the same call with the same arguments; a detector should cut it off.
Careful
Two failure modes dominate real deployments. Runaway loops burn budget on a task that is never going to converge. Premature stops end before the work is done because the model decided it was finished. Both are fixed in the harness — a step cap, a progress check, and a verifier on the critical step — not by asking the model to try harder.
Errors deserve a special mention: because a failed tool call is just another observation, a well-built agent retries with adjusted arguments instead of crashing. The loop turns failure into information.
Check yourself
Eduspheria wiki · Agentic AI, Foundations
0 / 4 answered
Next: what the model is actually calling — tools.