LearnAILevel 1.5 · Agents ← Roadmap

Station 3 · The Agent Loop

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.

3 of 10 · ~7 min

What you'll walk away with

Concept 1

The four stages, one cycle

Every agent — no matter how fancy — is running this same little cycle. Each trip around the loop is one "step." Read them in order:

Stage 1

👀 Perceive / Input

Take in the goal plus anything new: the user's request, and any results from the last step.

Stage 2

🧠 Reason & Plan

The LLM thinks: "What's the single next step that gets me closer?" It picks one action.

Stage 3

🛠️ Act / Tool Call

Do the thing — call a tool (search, calculate, fetch data) with real inputs.

Stage 4

🔎 Observe & Reflect

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.

🧭 Analogy: GPS re-routing

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

Why the loop is the whole point

A plain chatbot gets one shot: it reads your question and must answer immediately, right or wrong. The loop changes everything.

One shot

Chatbot

Question in, answer out. No chance to check its work or fix a mistake. If step one is wrong, the whole answer is wrong.

Many shots

Agent (loops)

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.

🔑 The loop, in one line

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

How it knows to stop

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

The loop, in pseudocode

Strip away the jargon and the whole agent fits in a few lines. Watch the four stages — they're labeled right in the code:

agent_loop.py
# 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
what to noticeThe for loop is the agent. Each pass runs the four stages once. It exits either at the "finish" check (goal met) or when it runs out of max_steps (safety brake). That's the entire skeleton.

Concept 5

Watch the loop run: Thought → Action → Observation

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:

react_trace.txt
# 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."
the loop in actionThree laps, one goal. Each Observation feeds the next Thought — the agent never guesses, it checks. On pass 3 the "done" check fires and it exits with a clean answer.

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?

The loop just makes the model answer faster. It repeats — act, observe the result, and try again — so the agent can reach a multi-step goal and recover from mistakes. The loop lets the model store more facts in memory.

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

Trace a loop with your own eyes

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:

  1. Ask: "Look up today's date, then tell me how many days until December 25." Watch it take a step (find the date), observe the result, then take a second step (do the math).
  2. If your tool shows its steps, label them yourself: which line is the Thought? The Action? The Observation?
  3. Notice where it stops — that's the "done" check firing once the goal is met.

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.