The Architecture of Autonomy: How the AI Agent Loop Actually Works
A chatbot is built to respond. An agent is built to act. The thing that turns one into the other is almost embarrassingly small: a loop.
You have built a chatbot. It works. Someone asks a question, it answers, done. Then someone asks it to do something that takes more than one step, "find me the three cheapest flights to Tokyo next month, check whether my points cover any of them, and book the best one", and it stalls. It can explain flights. It can explain how points work. It cannot carry out the task. Each prompt is handled in isolation, with no memory of intermediate results and no way to chain one decision into the next.
That gap is not a limitation of the model. The same models reason through multi-step problems all day. The gap is architectural. A chatbot is built to respond. An agent is built to act. And the thing that turns one into the other is almost embarrassingly small: a loop.
I spend my working life around control systems, where a loop is the most ordinary object in the room, read a sensor, compare it to a set point, move an actuator, read again. So when I first looked closely at how AI agents are built, the shape was instantly familiar. Strip away the vocabulary and an agent is a control loop wrapped around a language model. This piece is about that loop: what it is, how it runs, the levels it grows through, and, through a real engineering example you can clone and run, how it delivers actual work rather than just describing it.
The loop, in one everyday picture
Forget AI for a second and think about washing the dishes.
The goal is simple: every dirty dish in the sink is clean. You don't reach a clean kitchen in one motion, you run a loop. You look in the sink (dirty plate?). You decide (yes, pick up the sponge). You act (scrub). You observe (clean? onto the rack). Then you look again, more dishes? Round you go. The loop ends only when the sink is empty.
That is the whole idea. An agent cycles through the same stages, take in the situation, reason about it, act, and observe the result, over and over, until the job is genuinely done. It exists for one reason: some tasks can't be finished in a single pass. No one response can read a brief, draft a document, check it against every requirement, and revise it on feedback. That needs a mechanism that lets the system act, see what happened, and act again. That mechanism is the loop.
What the loop actually is
Here is the entire pattern, stripped to its bones. It's worth reading slowly, because everything else is decoration on these six lines:

That's it. The model is asked what to do. If it wants to use a tool, the tool runs and the result is fed back in. If it doesn't, the loop is over. This little while loop is the architecture underneath every autonomous AI system shipping today. The definition of an agent that has aged best is thirty years old, Russell and Norvig, 1995: anything that perceives its environment and acts upon it. The operative word is acts. Not responds. Acts.
Spelled out, each turn of the loop moves through a few stages:
- Perceive. The agent takes in input, a request, a tool result, an error, the outcome of its last action.
- Reason. It looks at everything in context and decides the next best step. For bigger tasks this is also where it plans, breaking the objective into pieces before diving in.
- Act. It does something with an effect in the world: calls a tool, queries data, writes a file, runs code.
- Observe. It examines the result. Did it work? Is the task complete? Does the plan need adjusting? Then it loops back and does it again.
One thing worth saying plainly, because it saves a lot of grief: an agent that appears to "learn" mid-task isn't retraining itself. The model's weights are frozen. What feels like learning is the loop reading from memory and feeding results back into context. That boundary tells you which problems are memory-and-scaffolding problems (most of them) and which genuinely need a new model (very few).
The loop grows through three levels
The loop isn't one fixed thing, it deepens as you add memory and structure around it. A clean way to locate your own system, which I'm borrowing from Richmond Alake's breakdown on the Oracle Developers blog, is three levels. Most production trouble comes from a mismatch between the difficulty of the task and the level you've actually built for it.
Level 1, model plus tools. The minimal loop: a model that can call tools and return an answer, with no memory beyond the current run. It iterates only because tool results have to come back before it can finish. Genuinely useful for self-contained jobs. Its ceiling is structural, every run starts cold, the context window is its only memory, and that resets when the run ends.
Level 2, a lifecycle with memory. Now memory operations live inside the cycle: the system pulls relevant memory before reasoning and writes new memory after acting. The loop stops being a pipe for tool calls and becomes a reasoning engine with state. The interesting shift is from a system that merely has memory injected into it to one that actively decides what to store, retrieve, and forget, and with that come new failure modes: stale facts, near-miss retrievals, too many tools to choose between cleanly.
Level 3, the harness as a system. Here the scaffolding around the loop becomes the real engineering. Some things happen automatically (the agent should never have to decide whether to load its own history); some are left to the agent (whether to search the web now). Get that line wrong and you either bloat the context or starve it. This is where context monitoring, conversation compaction, offloading big results to storage, and pulling in only the tools relevant to the current step all become unavoidable.
The inner loop, reason, act, observe, never changes across the three. Everything around it does.
Knowing when to stop
A loop has to end, and "the model went quiet" is only one of the ways. The cautionary tale writes itself: an agent told to scrape a site and retry until it gets data, pointed at a page whose structure just changed, will happily call a broken tool four hundred times in five minutes and burn through your budget before anything stops it. A maximum iteration count of three would have prevented the whole thing.
So a well-built loop defines its exits explicitly: a final answer with no tool calls left; a goal-completion check that actually tests whether the objective is met rather than trusting that the model fell silent on purpose; a hard cap on iterations; a timeout; an unrecoverable error; and no-progress detection, the same tool called with the same arguments three turns running is a stuck agent, not a working one.
That goal-completion point matters more than it looks. A terminal message ends the agent's turn; it does not prove the goal is satisfied. Deciding whether the work is truly done is the harness's job, and it's exactly where domain knowledge earns its place. Knowing what "complete and correct" looks like for a control narrative, a validation protocol, or a functional specification isn't something the model infers from a prompt. It's what the engineer brings.
A real example: from customer requirements to a finished specification
Most loop explanations reach for "fix the failing tests." Fine for developers, but it hides the part that makes loops valuable in a business: the agent checking its own work against a standard, and a human owning the outcome.
So here's a task that exists across software, automation, construction, and any regulated industry. A customer hands you a URS, a User Requirement Specification, what they need. Your job is a Functional Specification (FS), how the system will meet each of those requirements. Every requirement must trace to a section in the spec; miss one and the deliverable is incomplete. Then an engineer reviews the draft, marks it up, and it gets revised until it's signed off.
Take a small customer portal with five requirements, each with an ID:
- UR-001, a registered user can log in with email and password
- UR-002, a logged-in user can view their past orders
- UR-003, the system emails an order confirmation when an order is placed
- UR-004, an administrator can deactivate a user account
- UR-005, every login attempt, success or failure, is recorded in an audit log
That's a loop with a human in it. Here's how it runs:
- Read the requirements. Given "produce a complete, review-approved functional specification," the agent calls a tool that returns the five requirements.
- Draft the specification. It writes a first-draft FS, a section per requirement, each citing its ID.
- Check its own coverage. This is the step that matters. It calls a coverage check that compares the draft against the URS and reports which IDs are addressed and which aren't. Say it finds UR-004, the admin deactivation, slipped through.
- Close the gap. It revises to cover UR-004 and checks again. Now all five trace. The first loop, draft until complete, is done. Notice the loop didn't stop because the model fell silent; it stopped because an explicit traceability test passed.
- Take human review. The draft goes to an engineer, who marks it up. The agent reads the comments back: the login section is too vague, specify that five failed attempts locks the account for fifteen minutes; and add a data-retention section, audit entries kept at least twelve months. This begins the second loop, revise until approved.
- Revise and finish. It folds in both comments, and with nothing left to do concludes: all five requirements traced, both review comments addressed, ready for delivery.
Two nested loops, one task. The first drives the agent to completeness against an objective standard. The second folds in human judgment until a person is satisfied. Nobody choreographed each step, but a human still owned the result.
Where does this sit on the three levels? Honestly, Level 1, tools and a loop, no memory beyond the run. And that's the right size for drafting one specification. You'd climb to Level 2 the moment you wanted it to remember an engineer's review preferences across projects; you'd reach Level 3 when the tool list or the documents grew large enough to need real context management. Matching the level to the task is the whole skill.
This example is a real, runnable project, a hardcoded sample URS, simulated review comments, the full loop, and a turn-by-turn trace you can watch in your terminal. Clone it from the repository linked at the top of this article, read the loop in real code, and change it to your own requirements.
The loops around the loop
The agent loop doesn't run alone. The training loop that produced the model runs offline, over weeks, and as noted, your agent never touches it at runtime. The feedback loop is how the system knows it's working: tool results, user corrections, and completion metrics. And the human loop is the one my field cares about most. Long tasks reach points where the agent lacks the authority, context, or confidence to proceed, and a person steps in. That's a different kind of stop condition, the loop pauses not because the work is finished but because it has reached the edge of what the agent should decide alone.
The URS-to-FS example has exactly that shape: the agent drafts and self-checks on its own, but the spec isn't done until a human's comments come back in. That isn't a safety net bolted on afterward. In every plant I've worked in, automation runs the loop and a human owns the boundary, the operator who can hit stop, the engineer who signs the drawing. Building agents well is the same instinct in a new medium: give the loop real autonomy, and put the human exactly where authority and accountability have to live.
The takeaway
Writing a loop for an agent isn't "calling a model." It's designing a system that can reason, act, and learn from the result of its own actions, and then knowing exactly where to put the guardrails and the human. The dishes get washed because you keep looking back in the sink. The specification gets delivered because the agent keeps checking its work against the requirements, and an engineer stays in the loop until it's right.
That last part is the whole game. The loop gives an agent autonomy; the structure around it, the stop conditions, the caps, the human review, is what makes that autonomy something you'd trust with real work. Build both, and you move from a system that talks to a system that delivers.
Written by Usman Nasir — control systems engineer, Stockholm.