How an AI Agent Works: The Loop Explained Step by Step

An AI agent doesn't respond in one shot: it works in a loop that repeats until the task is done. Here's how that loop works, jargon-free and step by step.

Contributors: Ivan Garcia Villar

How an AI Agent Works: The Loop Explained Step by Step

The first time a friend asked me how an AI agent works on the inside, I didn’t launch into a definition. I turned it around: how do you get a sauce to the right salt level? You taste it, find it bland, add a pinch, taste again. That “taste again” is almost literally what an agent does. It doesn’t answer once and go quiet. It loops until the task is right.

You don’t need to know how to code or have built anything to follow this post. You just need to have used an AI chat like ChatGPT at some point, and be curious about what happens behind the scenes.

An AI agent is a program that uses a language model (the “brain” behind ChatGPT) to complete a task on its own, step by step, without you holding its hand. That last part — “on its own” — is what we’re going to unpack.

An agent doesn’t respond once: it loops

A regular chat replies to you once and goes quiet. An agent doesn’t: it keeps working through steps until it finishes what you asked.

Think about it from your side. When you write to a chat, the deal is simple: you ask, it answers, done. If the response doesn’t work for you, you’re the one who rephrases and tries again. That work of checking the result and saying “this doesn’t cut it, let’s try again” — you do that, manually.

An agent takes that job. You give it a goal (“fix this bug,” “sort these invoices by date”) and it judges whether what it has done so far is good enough, or whether it needs to try again. It loops on its own, without you standing over it pushing it forward step by step.

That might sound like a small detail. It’s the one that changes everything.

The loop diagram: think, act, observe, think again

An agent’s loop has four moments that repeat in the same order every time: think, act, observe the result, and think again. With those four understood, everything else is decoration.

Diagram of an AI agent's loop: four steps that repeat until the task is complete
The agent loop: think, act, observe the result, and decide whether to repeat or stop.

Think. The model looks at two things: the goal you gave it and what it knows so far. From that it decides the next step. It doesn’t try to solve everything at once. It just picks the one step that makes sense right now.

Act. The agent executes that step in the real world. This is where it uses a tool. A tool is anything the agent can use to do something outside its own head: read a file, search the web, run a piece of code, or send an email. When those tools are connected to an agent through a common standard — the most widely used today is MCP — I have a whole post dedicated to it: what MCP is and how it connects an agent with its tools.

Observe the result. The action returns something. The file said this, the search found that, the code threw an error on line 12. The agent reads that result just like you taste whether the sauce is still bland.

Think again. With the new result in hand, the agent decides once more. Is the task done? Or is there still a step left? If there’s more to do, it goes back to the start and repeats. That “goes back to the start” is the loop, and it’s the key word in all of this.

Laid out like that in the abstract, it sounds like not much. It makes a lot more sense with something you’ve done a thousand times without thinking.

The same loop, first in the kitchen and then in code

The agent’s loop is identical to the one you use to nail a sauce without a recipe. See them side by side and you’ll never forget it.

In the kitchen, without realizing it, you do this:

  • Think: “it’s bland, it needs salt.”
  • Act: add a pinch.
  • Observe the result: taste another spoonful.
  • Think again: “still short, one more pinch.” And repeat.

At some point you taste it, it’s just right, and you stop. Nobody tells you how many times to add salt. You decide by tasting, round by round.

Now the same loop with an agent asked to fix a bug in a program. Think: “the message says there’s a missing semicolon on line 12.” Act: open the file and add it. Observe the result: run the program and see that now a different error pops up, further down. Think again: “okay, that’s something else — let’s go after it.” And it keeps going, error after error, until the program starts up clean.

If we wrote it out as code, the heart of an agent is a loop as simple as this:

// pseudocódigo simplificado; las variables se declaran antes del bucle
// El corazón de un agente: repetir hasta que la tarea esté lista
while (true) {
  const decision = modelo.siguientePaso(objetivo, loQueSe);  // piensa: elige el siguiente paso (o si ya está)
  if (decision.tipo === 'fin') break;                        // si ya está, para
  const resultado = ejecutar(decision.accion);               // actúa
  loQueSe.push(resultado);                                   // mira el resultado y lo guarda
}

You don’t need to understand every line. Just notice the word while, which means “as long as.” As long as the task isn’t done, repeat. This whole post fits in that one word.

When does the agent stop?

The agent stops when the task is done. The interesting question is the one underneath: how does it know it’s done?

It needs a rule that tells it “you can stop now.” That rule is the stopping condition. With the sauce, your stopping condition is your own palate: you stop when the flavor convinces you. With an agent, the condition is usually something checkable — like the program starting up without errors, or the email arriving at its destination.

And there’s a second condition, less elegant but just as necessary: a safety limit. If the agent has looped many times and still hasn’t made it, it’s better for it to stop and report back than to keep trying forever. An agent without a brake is like a sauce where you keep adding salt without stopping to taste: at some point you have to say enough.

The first agent I let run for real got stuck right here. Without a good stopping criterion, it would repeat the same failed attempt over and over, convinced that the next try would nail it. The safety limit stopped it — not its own judgment.

Choosing those two conditions well is harder than it looks, and it’s exactly the point where an agent goes from useful to useless.

Why does this matter to you?

If you’ve understood that an agent works in a loop — not in a single shot — you already have the mental model that many people are missing. With that picture in your head, you stop expecting magic and start directing.

The loop doesn’t design itself. Someone decides what tools to give the agent, when it should stop, how it checks whether it’s on track, and what to do when it gets stuck. That specific loop — the one you just saw — has a technical name: the agentic loop. And designing it well, decision by decision, is the craft this series is about: I call it loop engineering — the difference between an agent that solves the task and one that keeps spinning without making progress.

I’m publishing a series on how to design this loop without losing control of it. If you want me to let you know when the next part drops, leave your email here:

One new concept every week

Two beginner mistakes with agents

Expecting it to get it right on the first try

A lot of people picture the agent as a genie in a bottle: you ask for something and it nails it on the first attempt. That’s what we expect from anything we call “intelligent,” which is why it’s so hard to trust something that makes mistakes on purpose. But here’s the twist: an agent is powerful precisely because it doesn’t need to get it right the first time. It fails, reads the failure, corrects it on the next pass, and tries again until it works. Failing cheaply and correcting quickly is its advantage, not its flaw. Demanding that it get it right on the first try is asking it to give up the only thing that makes it useful.

Treating it like a slightly smarter chat

A chat responds; an agent works. If you hand it a task and wait for the answer in a single message, you’ll get frustrated watching it take time, try things, and make mistakes along the way — it’s not broken, it’s looping, which is exactly what it’s supposed to do.

Frequently Asked Questions

Is an AI agent the same as ChatGPT?

Not exactly. ChatGPT, when you use it as a chat, responds once to what you write and stops. An agent uses a similar model but places it inside a loop that repeats: think, act, observe the result, and start again until a goal is met. In short: a chat answers, an agent works. That said, ChatGPT can also behave like an agent when it uses tools and chains several steps in a row.

How many times does an AI agent repeat the loop?

As many times as necessary. There’s no fixed number.

Does the agent do this on its own, or do I have to guide it?

It does it on its own — that’s the whole point. You give it the goal and the tools, and it decides the steps and when to stop. What changes is your role: you go from typing each step to designing the loop well and reviewing that what it does makes sense.

Do I need to know how to code to understand how an AI agent works?

No. The idea of the loop (think, act, observe, repeat) is understandable without writing a single line of code, just like you understood it with the sauce analogy. You’ll need coding later, when you want to build your own agent instead of just understanding how one works.