"An agent is just a language model in a loop — thinking, acting, and checking the result, over and over, until the goal is met."
Learning Objectives
By the end of this module, you will be able to:
- Describe the agent loop in plain words
- Follow a goal from start to finished action
- Explain the "reason then act" pattern
- Know how an agent decides it's finished
1. The Agent Loop
The heart of every agent is a simple, repeating cycle:
You give the agent a goal. Then it repeats: Think (what's the next step?) → Act (use a tool) → Observe (what happened?) → think again. When the goal is met, it stops and gives you the result.
The loop is powerful because the agent doesn't need to know every step in advance. It figures out step two after seeing the result of step one. That's why an agent can handle tasks where you couldn't possibly script every move ahead of time — it adapts as it learns what's true.
Explain like I'm new: Think of a person following a recipe they've never tried. They read a step, do it, glance at the pan to see how it's going, then decide what to do next based on what they see. That glance-and-adjust rhythm — do a little, check, do a little more — is exactly the agent loop.
Key idea: The loop is what separates an agent from a chatbot. A chatbot answers once. An agent keeps going — reasoning, acting, and reacting to results — until the job is actually done.
2. Step by Step
Here's what happens when you ask an agent, "What's the weather for my Friday trip?"
- User goal — "weather for my Friday trip."
- LLM reasoning — "I need the destination first. Let me check the calendar."
- Tool / action — calls the calendar tool, finds "Friday: Chicago."
- Observe — reads the result: Chicago.
- Loop again — "Now I need weather for Chicago." Calls the weather tool.
- Final response — "Chicago on Friday: sunny, 24°C. Pack light!"
Each pass through the loop does one small thing. Together, they solve a multi-step task no single reply could. Notice that step 5 depended on step 3 — the agent couldn't have looked up the weather until it discovered the city. That's the loop earning its keep.
This dependency between steps is exactly why a single chatbot reply can't do the job. A chatbot would have to guess the destination, or ask you for it and stop. The agent instead treats "find the destination" as its own small step, completes it, and then uses that answer to drive the next step. Complex tasks are really just chains of small steps like this, where each link needs the result of the one before it — and a loop is the natural way to follow such a chain.
3. "Reason, then Act"
The most common agent pattern is nicknamed ReAct — short for Reason + Act. The agent writes out a bit of reasoning ("I should look up X"), takes an action, sees the result, and reasons again. Thinking out loud like this makes agents more accurate and easier to debug.
Real-world use case: When an agent misbehaves, its written reasoning is your best clue. You can read "I decided to delete the file because…" and immediately spot the faulty logic — far easier than debugging a silent system that just acts. Reasoning traces turn a black box into something you can inspect.
The other quiet strength of the loop is recovery. Because the agent observes the result of each action before choosing the next one, a failed step doesn't have to be fatal. If a search returns nothing, the agent sees the empty result and can try a different query. If a tool errors out, it reads the error and adjusts. Contrast that with a rigid script, which just crashes at the first surprise. The observe step is what gives an agent this "notice and adapt" behavior — and it comes for free, simply because the loop always checks what happened before moving on.
4. Knowing When to Stop
An agent needs a clear stopping point: the goal is met, a step limit is reached, or it needs a human. Without one, it can loop forever. In practice, developers set a maximum number of steps (say, 10) as a safety net, so even a confused agent stops instead of spinning endlessly and running up costs.
Common mistake: Building an agent with no "stop" condition. If it never decides the task is done (or hits a limit), it can loop endlessly — wasting time and money. Always define what "finished" looks like and cap the number of steps.
Key Takeaway: An agent works by looping: think → act → observe → repeat, guided by a goal and ended by a clear stopping point. The popular ReAct pattern has it reason before each action. This loop is the single most important idea in how agents work.