Station 3 · The Agent Loop
This is the idea. An agent isn't magic — it's a simple cycle running over and over: take in the goal, decide the next move, do it, look at the result. Repeat until the job is done.
What you'll walk away with
Concept 1
Every agent — no matter how fancy — is running this same little cycle. Each trip around the loop is one "step." Read them in order:
Take in the goal plus anything new: the user's request, and any results from the last step.
The LLM thinks: "What's the single next step that gets me closer?" It picks one action.
Do the thing — call a tool (search, calculate, fetch data) with real inputs.
Read the result. Is the goal met? If yes, answer. If no, loop back to Stage 1 with what you just learned.
That last arrow is the whole trick: Stage 4 feeds back into Stage 1. The agent keeps circling — perceive, reason, act, observe — and each lap it knows a little more than the last.
Your GPS doesn't plan the whole drive once and go silent. It perceives where you are, reasons the next turn, tells you to act ("turn left"), then observes where you actually ended up. Missed the exit? It doesn't crash — it loops, re-routes from your new spot, and keeps going until you arrive. An agent works exactly like that: it re-plans every step from wherever it actually landed.
Concept 2
A plain chatbot gets one shot: it reads your question and must answer immediately, right or wrong. The loop changes everything.
Question in, answer out. No chance to check its work or fix a mistake. If step one is wrong, the whole answer is wrong.
Each pass gets closer. A bad tool result? It sees the failure in Stage 4 and tries a different move next lap. It self-corrects.
This is why agents handle messy, multi-step jobs. Booking a trip, debugging code, researching a topic — none of these can be done in one guess. The loop lets the agent break a big goal into steps and recover when a step goes sideways.
Perceive → Reason → Act → Observe → repeat. An agent is just an LLM running this cycle until the goal is met or a step limit is hit. Everything else in this roadmap — tools, memory, planning — is a piece that plugs into this loop.
Concept 3
A loop that never ends is a bug, not a feature. Agents get two brakes so they always halt:
Think of the step limit as a safety net. The "done" check is the normal way out; the iteration cap catches the agent when it gets confused and can't find the exit on its own.
Concept 4
Strip away the jargon and the whole agent fits in a few lines. Watch the four stages — they're labeled right in the code:
# goal comes from the user; give it a step limit so it can't run forever def run_agent(goal, max_steps=10): for step in range(max_steps): # ── Stage 1: PERCEIVE ── take in goal + everything so far context = goal + history # ── Stage 2: REASON & PLAN ── LLM picks the next move thought, action = llm.decide(context) # ── Stage 4 (early exit): the "done" check ── if action == "finish": return thought # goal met — leave the loop # ── Stage 3: ACT ── call the chosen tool for real result = tools.run(action) # ── Stage 4: OBSERVE ── record the result, then loop again history += (thought, action, result) return "Hit the step limit before finishing." # the safety brake
Concept 5
Let's send one goal through a few laps. This shape — Thought, Action, Observation — is called ReAct (Reason + Act), and it's the most common way agents run the loop. Here's a goal that takes 3 passes:
# GOAL: "How many days until New Year's, and what's the weather in Austin?" # ── Pass 1 ── Thought → "Two things to find. Start with the date math." Action → days_until("Jan 1") Observation → 170 days # ── Pass 2 ── (loops back with what it just learned) Thought → "Got the days. Now the live weather." Action → get_weather("Austin, TX") Observation → 99°F, sunny # ── Pass 3 ── (the "done" check passes) Thought → "I have both answers. Goal met — finish." Action → finish Answer → "170 days until New Year's, and it's 99°F and sunny in Austin."
Notice how the agent could've hit a snag — say get_weather failed — and it would simply loop again with a different action instead of giving up. That resilience is the payoff of the loop. You'll build a full ReAct agent in Station 7.
Checkpoint
What makes the agent loop different from a chatbot's single reply?
A chatbot answers in one shot. The agent loop runs the four stages over and over — reasoning, acting with a tool, then observing the result before the next pass. That repetition is what lets it break down a big goal, check its own work, and re-route when a step fails, stopping only when the goal is met or the step limit is reached.
See it yourself — 5 minutes
Open any AI tool that can browse the web or run code (ChatGPT, Gemini, or Claude) and give it a two-part goal so it has to loop:
Recap. The agent loop is the heart of everything: perceive → reason → act → observe → repeat. It's what turns a one-shot chatbot into an agent that chips away at a goal, recovers from bad results, and stops when it's done (or hits a step limit). The ReAct shape — Thought, Action, Observation — is how you'll usually see it. Next up: the tools the loop reaches for when it acts.