AI Agents and Coding Assistants
The Loop Inside an AI Agent: Observe, Decide, Act and Check
A plain-language walk through the four-part loop that drives AI agents, with a worked example, stopping rules and the places it tends to break.
Key takeaways
- An agent repeats a cycle of observing, deciding, acting and checking until something tells it to stop.
- The model chooses steps, while ordinary software carries them out and feeds the results back as text or images.
- The check step is what separates a useful agent from one that blindly runs through a plan.
- Every loop needs stopping rules, such as a step limit, a spending cap and a clear definition of done.
- Reading an agent's step record shows you which part of the cycle failed.
Most AI agents run on a simple cycle. They observe the current situation, decide on a next step, act by using a tool, and check what happened. Then they go around again until the goal is met, a limit is reached or the agent gives up and asks for help.
Once you can picture this loop, much agent behavior makes sense: why an agent can recover from a failed step, why it sometimes spins in circles, and why it may declare success too early.
Observe: what the agent can see
The agent starts each round with whatever information is in front of it. That includes your instructions, a description of the tools available, and the history of earlier steps and their results. Depending on the product, it may also include files, a web page, a screenshot or stored notes.
This collection is the model's working view of the world. It is limited by the context window, the amount of material a model can take into account at one time. In a long task, older details may be shortened or dropped to make room.
Two consequences follow. The agent knows nothing about things it has not looked at, even if they are obvious to you. And whatever it reads goes into the same pool as your instructions, so misleading text on a web page can influence what it does next.
A good agent often spends its first few rounds just looking: listing a folder, reading a document, searching for the relevant record. Rushing past this stage is a common source of confident, wrong work.
Decide: choosing the next step
Next, the model produces its choice. It might write a short plan, then name one tool to use and the inputs for it. Some models spend extra effort reasoning before they answer, which can help with steps that depend on each other.
The decision is a prediction about what would move the task forward. It can be sensible and still be wrong. The model may misread a result, assume a file exists, or pick a tool that sounds right but does something else.
Planning also varies. One style makes a full plan up front and follows it, revising when needed. Another decides only the next move each round. Long plans help keep the goal in view, while step-by-step choosing adapts faster to surprises. Many systems mix the two by keeping a task list that gets updated as they go.
Act: using a tool
Acting means requesting a tool call. The model writes something like "run a search for these words" or "write this text to that file" in a fixed format. The surrounding program, often called the harness, checks the request, runs it and returns the result.
The harness is where real control lives. It can refuse a request, pause for your approval, run the action in an isolated test space, or record it for later review. The model's wish to do something only becomes an event if this software allows it.
Actions differ greatly in weight. Reading a file changes nothing. Editing a file can usually be undone. Sending an email or placing an order often cannot. Sensible designs treat these classes differently, with approval steps for actions that are hard to reverse.
Check: reading what came back
After acting, the agent receives a result: search listings, an error message, the new contents of a page, a test report. It then has to judge whether the step worked and whether the whole task is finished.
The strongest checks come from outside the model's opinion. A program either runs or throws an error. A total either matches the ledger or does not. A confirmation page shows a booking number. When such evidence exists, the loop can correct itself.
Weak checks rely on the model rereading its own work and deciding it looks fine. That catches some slips but misses many, since the same misunderstanding that produced the error may approve it. This is one reason self-checking alone is not enough for work that matters.
Watch for silent failures too. A tool might return an empty list because the search was malformed, and the agent may conclude that nothing exists.
A worked example
Suppose you ask an agent: "Find the invoices from March in this folder and make a spreadsheet of supplier, date and amount." This is a hypothetical run.
1. Observe: the request and a folder path
2. Decide: list the folder first (nothing known yet)
3. Act: list files
4. Check: mixed names; dates not in all of them
5. Decide: open each file and read the date
6. Act: read files one by one
7. Check: one scan is unreadable (flag it, do not guess)
8. Act: write the spreadsheet
9. Check: row count matches readable March invoices
10. Stop: report results and the one problem file Step 4 changes the plan: file names cannot be trusted, so the agent opens each file. Step 7 is where behavior matters most. A poorly designed or poorly instructed agent might fill in a plausible amount. A better one records the gap and tells you.
Step 9 compares a count against the source. It is cheap, and it would catch a skipped file.
Knowing when to stop
A loop needs an exit. Common ones include:
- The goal is met, judged by a stated test where possible.
- A maximum number of steps or amount of time is reached.
- A spending or usage cap is hit.
- The same action has failed repeatedly.
- A step requires a decision only you can make.
- You press stop.
Without these, an agent can retry a broken action many times, wander into side tasks or keep "improving" finished work. Each round also costs computing time, which usually means money. Spending limits and stop buttons are part of the design, and you should be able to find them.
Stopping to ask is a good outcome. An agent that says "two accounts match this name, which one?" has used the loop well.
Where the loop breaks
You can often diagnose a failed run by asking which stage went wrong.
| Stage | Typical failure | What helps |
|---|---|---|
| Observe | Missed a file or lost an early instruction | Point to sources; restate key limits |
| Decide | Solved a different problem | State the goal and what is out of scope |
| Act | Wrong inputs or wrong target | Previews, test accounts, narrow permissions |
| Check | Declared success without evidence | Require a test, count or confirmation |
Most of these fixes sit in your instructions or the tool setup. Clear instructions that define done, list limits and say when to ask will improve every round of the loop.
Carrying information from round to round
The loop only works if the agent can keep track of where it is. In the simplest design, every earlier action and result stays in the history that the model reads each round. This is accurate but bulky, and a long task can fill the available space with search listings and file contents.
Many agents therefore condense. They may summarize older steps, keep a running to-do list, or write notes to a file that they reread later. Each method trades detail for room. A summary that drops "the client asked us not to contact them by phone" can undo good behavior ten steps later.
You can help by stating the constraints that must survive, and by asking the agent to keep them in its task list. For work that spans separate sessions, products may offer stored memory, which has its own risks of becoming outdated, covered in how agent memory works.
A handy sign of lost track is repetition. If the agent reopens the same file again and again, or retries an approach that already failed, it may no longer be seeing or weighing the earlier result. Stopping and restarting with a clean summary often works better than letting it continue.
What to look at after a run
Many agent products show a trace: the list of steps, tool calls and results. Reading one does not require programming. Look for the first moment where the agent assumed something instead of checking it. Look for errors it passed over, and compare its closing summary with what the record shows.
If the summary says "all files processed" and the trace shows one that failed to open, trust the trace. Over a few runs you will learn which kinds of tasks your agent loops through reliably and which ones need you at the checkpoint.