The agent loop: act, observe, repeat
Describe the cycle the model runs: decide a next step, call a tool (read, search, run, edit), read the result, and repeat until it judges the task done. Predict that each step is driven by what it has observed so far.
Why does it read and run before answering?
You open a coding agent in a small project and type: `Cart total is 24.3, should be 27`. A chat assistant would reply with a paragraph of advice. The agent replies differently. It searches your files, opens one, runs your script, opens another file, changes a line, then runs the script again, and only then writes a short message. What is it doing, and what decides each of those moves?
A model that can use tools, in a loop
The model itself only writes text. The agent program around it gives it **tools**: read a file, search, run a command, edit a file. The model asks for a tool, for example to read `src/cart.py`. This course shows such a request as `⚙ Read: src/cart.py`. The program carries out the request and hands back the result. The model reads that result and picks its next move. It does not know your code in advance. It finds things out one step at a time.
The loop, step by step
1. **Decide** a next step from your request plus everything observed so far.
2. **Act**: call a tool (read, search, run, edit).
3. **Observe**: read the result, such as file contents, command output or an error.
4. **Repeat**, or stop when the model *judges* the task done and writes a summary.
Each step is chosen from what it has observed, especially the latest result. If an observation changes, the path changes. "Done" is the model's judgement, not a guarantee.
One task, one loop
A realistic session, shortened. Each `⚙` line is one act. The indented lines under it are what the agent got back; only the relevant lines are shown. For the edit, they show the change it made. The `◂` line is the summary it writes when it decides to stop.
▸ You: Cart total is 24.3, should be 27
⚙ Search: "Total" in src/
src/cart.py: print("Total:", total)
⚙ Read: src/cart.py
from pricing import final_price
total = final_price(items) * 0.9
⚙ Run: python src/cart.py
Total: 24.3
⚙ Read: src/pricing.py
return subtotal * 0.9
⚙ Edit: src/pricing.py
changed to: return subtotal
⚙ Run: python src/cart.py
Total: 27.0
◂ Agent: Fixed the doubled 10% discount.
Why each step followed from the last
It **searched** for `Total` because it didn't know which file prints it. The match was in `cart.py`, so it **read** that file and found a 10% discount (`* 0.9`) on a price from `pricing.py`. It **ran** the script to see the bug, then **read** `pricing.py` and found a second `* 0.9`. For items worth 30, 30 × 0.9 × 0.9 = 24.3, while one discount gives 27. It removed one, ran again, saw `27.0`, and stopped. Had that run shown an error, the error would have been its next problem, not a summary.
Remember
An agent works in a loop: decide, use a tool, look at the result, repeat, until it *judges* it's done. Every move is driven by what it has actually observed so far. So to predict its next move, or to doubt its claim, look at what it last saw.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.