Imagine you hired the smartest person in the world — but they are locked in a room with no windows and no hands. The only way you can talk to each other is by sliding notes under the door.
You slide a note in: "Write me a story and save it on my computer."
The genius reads it. They are incredibly smart, but they cannot touch your computer. They cannot open a browser or create a file. All they can do is think and write on paper.
So they slide a note back: "I have written the story! But I cannot save it myself. Please write this text into a file called story.txt."
Now it is your turn. You take the note, walk to your computer, create the file, type in the text, and save it. Then you slide a note back under the door: "Done! The file has been saved."
The genius reads your note, smiles, and slides one final note: "Mission accomplished."
This is exactly how every AI coding assistant works. The genius is the AI model (the LLM). The room is the API. The notes are messages. And you — the one who runs errands — are The Agent.
The Note-Passing Loop
Every AI coding tool — Claude Code, Cursor, Copilot, all of them — runs the same secret pattern under the hood. We call it The Loop:
- Send — You slide a note under the door (send a message to the AI)
- Check — The genius slides a note back. Does it ask you to do something? Or is it a final answer?
- Execute — If the genius asked you to do something (like "save this file"), you go do it. Then you slide a note back saying what happened.
- Repeat — The genius reads your update. Maybe they need another errand. Maybe they are done. If they need more, you go back to step 1.
That is it. That is the entire secret. Every "magical" AI coding assistant is just this loop running over and over.
You are the Agent. A user has just sent you a request. Let's walk through what happens.
Two Ways to Stop
The loop has exactly two ways to end:
Exit 1: The genius is done. The AI replies without asking you to run any errands. No more notes to pass, no more files to create. The conversation is over.
Exit 2: The safety limit. What if the genius keeps asking for errands forever? What if they get confused and keep saying "now read this file" in an endless circle? You need a safety net — a maximum number of rounds. After 10 rounds (or whatever limit you set), you stop the loop even if the genius is not done.
Without this safety limit, a confused AI could burn through your entire API budget in seconds. Think of it like a budget alarm on your credit card.
Put the loop steps in the correct order
Drag to reorder, or use Tab + Enter + Arrow keys.
- Execute the tool (run the errand)
- Report the tool result back to the LLM
- Send the user's message to the LLM
- Check: does the response ask for a tool call?
Stop the runaway loop!
Press EMERGENCY STOP before the loop reaches the danger step!
What You Learned
You now understand the secret behind every AI coding assistant: The Loop. A genius locked in a room, passing notes, asking for errands. Send, check, execute, repeat — until the genius is done or you hit a safety limit.
In Chapter 2, you will discover how the genius learns new skills. Right now they can only ask for one errand (write a file). What happens when they need to read files, search code, run tests? That is the Tool Registry.
What You Can Do Next
You now have a working agent loop — the same primitive that powers every coding agent. But the dispatch function is fragile: it is a hardcoded if statement that handles one tool. What happens when you need ten tools? What happens when a tool returns complex data?
In Chapter 2: Tool Registry and Structured Results, you will replace the dispatch function with a proper registry that makes tools pluggable, testable, and safe.