The loop behind every agent

Lesson 1 of 27 · How an agent works · 2 min · Free Watch this lesson free

Your agent just told you the work is done, and it sounds completely sure. Should you believe it? That depends on one thing: what the agent was able to check before it said so.

By the end of this lesson you will know why that is, and you will have one habit that improves almost every prompt you write.

Model tools loop

Strip away the branding and every coding agent is the same thing: a language model, a set of tools, and a loop. The model reads the situation and picks an action. A tool runs it. The result goes back to the model.

Claude Code's documentation names three phases that blend together: gather context, take action, and verify the result. The product names change from tool to tool, but this shape does not.

The tools

The tools are what make it an agent rather than a chatbot. They come in a few families: reading and editing files, searching the codebase, running shell commands, fetching from the web, and asking the editor about types and definitions.

Notice what is missing from that list. Nothing in it knows your standards, your architecture, or what your team decided last quarter. All of that reaches the agent only through the text you give it.

Who decides it is finished

Here is the part that matters most. The loop ends when the model judges the task complete, not when a rule says so. A workflow stops at a step fixed in code. An agent stops when it decides it is finished.

That is why the same agent looks brilliant on one task and sloppy on the next. If it can run something that fails, it keeps going. If it has nothing to run, its own confidence is the only signal.

The habit

So the habit is simple. End every prompt with the check that proves success: a test to run, a command with an expected result, a file that must not change. Now the loop has something real to push against.

Compare these two prompts. The second names the failing test, tells the agent to run the suite until it passes, and forbids editing the tests. Same agent, much better odds.

Keep this idea: an agent is a loop that stops on its own judgement, so your job is to hand it a better judge than itself. Next, what the loop reads from, the context window.

This is the narration of a video lesson from Engineering with Coding Agents. In the video it is animated, and you can pause it to ask a question.