How an AI agent works, step by step: think, act, observe, repeat (the agent loop)
The agent loop is a cycle. The model, the AI language program at the centre of the agent, thinks about what to do next, takes one action with a tool, a command like "run the test" that the program carries out for it, reads the result, and repeats. Think, act, observe, repeat. That cycle is the whole engine of an agent. Everything else is decoration.
New to these terms? Every word is explained in the series glossary.
Think of it like cooking. You taste the soup, decide it needs salt, add salt, taste again. You do not write a full recipe in advance and follow it blindly. You adjust after every taste. An agent does the same thing with tools instead of a spoon.
Here is one task broken into five loops. The task: fix a failing test, a small piece of code that checks other code works, in a small Python project.
Loop 1. Think: the test failed, so something is broken. Act: run the test and read the error. Observe: the error says a module cannot be found. The import, the line that pulls in code from another file or library, is missing.
Loop 2. Think: the import is missing, so the file needs it. Act: add the import line. Observe: run the test again. It still fails, but the error changed. Now a function, a named block of code, returns the wrong type of value.
Loop 3. Think: the function returns a list where the test expects a number. Act: fix the return value. Observe: run the test again. It passes one case and fails another. There is an edge case, an unusual input at the limits of what the code expects.
Loop 4. Think: the edge case is an empty input. Act: handle the empty input. Observe: run the test again. All cases pass.
Loop 5. Think: the fix might have broken something else. Act: run the full test suite. Observe: everything is green. Stop.
| Loop | Thought | Action | Result |
|---|---|---|---|
| 1 | Something is broken | Run the test | Missing import |
| 2 | Add the import | Edit the file | New error, wrong type |
| 3 | Fix the return value | Edit the function | One case still fails |
| 4 | Handle the empty input | Edit the edge case | All cases pass |
| 5 | Check nothing else broke | Run the full suite | Green, stop |
Animation: Five loops of think, act, observe fixing a failing test, stopping when the tests pass or at loop 5.
Notice what happened. The agent never planned all five steps in advance. It could not have. It did not know about the wrong type until loop 2, and it did not know about the edge case until loop 3. Each observation, the result that comes back after an action, changed the next thought. That is the loop.
Loops fail in three ways. First, the agent repeats the same action and gets the same result, forever. That is a loop with no learning. Second, the agent misreads the result and acts on the wrong thing. Third, the agent never stops, because it has no clear finish line. A good agent has a max number of loops and a clear definition of done, a checkable description of what finished looks like. Mine stops at five loops or when the tests pass, whichever comes first. That rule is its stop condition.
python# The loop, as code. Five loops max, stop when the goal is done. goal_done = False for loop in range(1, 6): thought = "read the last result and decide the next action" action = "run one tool" result = "the tool output" print(f"loop {loop}: {thought} -> {action} -> {result}") if loop == 5: goal_done = True print("done:", goal_done)
Try this
bashpython3 -c " for loop in range(1, 6): print(f'loop {loop}: think -> act -> observe') print('stop when the goal is done, or at loop 5') "
When you do not need a loop, see Part 3. It shows when a plain chatbot or a fixed workflow is the better tool, and when an agent is overkill.
Sources
- ReAct loop: Yao et al., 2022, arXiv:2210.03629.
- Loop failure modes: standard in agent framework docs and the ReAct paper.
