The Agent Loop: Think, Act, Observe, Repeat
See the loop at the heart of every agent, how it ends, and why limits and error handling matter.
One tool call answers one question. Real tasks need several: find the trains, filter them, check the fare, add it to the calendar. Agents get there by running the same small cycle over and over.
- Think. Given the goal and everything so far, what’s the next step?
- Act. Call a tool.
- Observe. Read the result, which is added to the context.
Then back to think. In code it’s strikingly simple: a loop that sends the conversation to the model, runs any tool it asks for, appends the result, and repeats until the model answers without asking for a tool.
The power comes from observing. A fixed script breaks when something unexpected happens; an agent sees the surprise and adapts. If the calendar tool returns “invalid date format”, the agent reads the error, fixes the format and tries again. That’s why tools should return clear, helpful errors rather than silent failures.
The loop needs exits, though, or it can run forever:
- Done: the goal is met, so it answers the user.
- Limits: a maximum number of steps, tokens, time or money, because agents can get stuck repeating a failing action.
- Ask: when it’s unsure or about to do something risky, check with a human.
One more cost to keep in mind: every turn of the loop re-sends the growing context, so long runs get expensive. That problem gets its own lesson soon.
Key takeaways
- Agents work in a loop: think, act (call a tool), observe the result, repeat.
- Observing lets them adapt, including recovering from errors, so tools should return clear errors.
- The loop ends when the goal is met, a limit is hit, or a human is needed.
- Every turn re-sends the growing context, so long loops cost more.
Check your understanding
Go deeper (more technical)
In practice the loop is a few lines of code: call the model with the messages and tool definitions; if the reply contains tool calls, run them, append the results as new messages and call again; otherwise return the reply. Agent frameworks add retries, timeouts, streaming, tracing and limits around that core.
Teams watch agent runs through traces: a step-by-step record of every thought, tool call and result, which is essential for debugging and for the audit trails in the last lesson.
The simulation’s steps and token counts are illustrative.