Module 3 · Lesson 1 of 6
The agent loop
Goal, think, call a tool, observe, repeat, stop: what turns a model into an agent, and the harness that runs it.
10 min
A chatbot answers a question and stops. An agent is given a goal and keeps working until the goal is met: it looks something up, reads the result, decides what to do next, and repeats. That repeating cycle is called the agent loop, and almost every AI product you will meet in this course is built on it, from ChatGPT and Claude to coding agents and the AI Agent step in an automation tool.
Understanding the loop matters at a brokerage because it tells you where things go wrong. When an agent books the wrong showing or emails the wrong client, the cause is almost always one turn of the loop: the model picked the wrong tool, read a result badly, or kept going when it should have stopped. If you can picture the loop, you can design the guardrails.
The loop, one step at a time
- Key term
- Agent
An AI model that works toward a goal by choosing its own next step, calling tools, reading what comes back, and deciding whether it is done.
Say a salesperson at Lakeshore Homes Brokerage types: "Find two showing times next Tuesday for 123 Example Street, Toronto that fit my calendar and the sellers' rules." Here is what happens inside.
- Goal. The request, plus the instructions the brokerage set up in advance (the system prompt from Module 2), goes to the model.
- Think. The model reads everything it has and decides the next move. It cannot see a calendar, so it decides to ask for one.
- Act (call a tool). The model replies with a structured request, something like "call
get_calendarfor Tuesday". It does not run anything itself. - Observe. The program around the model runs the tool and pastes the result back into the conversation: "Busy 9:00 to 11:30 and 2:00 to 3:00."
- Repeat. With that new information, the model thinks again. Next it asks for the listing's showing instructions, which say "No showings before 10:00, 24 hours' notice."
- Stop. When the model has enough to answer, or a limit is reached, it writes a final reply: "11:30 and 3:30 work. Want me to draft the booking requests?"
- 1. TriggerYou give a goal
- 2. AIModel decides the next step
- 3. ToolHarness runs the requested tool
- 4. DataResult is added to the context
- 5. CheckGoal met or limit reached?
- 6. ActionFinal answer or a draft for you
Steps two to five repeat until the check says stop.
Notice that every lap adds text to the conversation: the tool request, then the result. The model rereads all of it on the next lap. That is why long agent runs cost more and can lose track of details, as you saw with context windows in Module 1.
What a harness is
- Key term
- Harness
The program wrapped around a model that runs the agent loop: it keeps the conversation, tells the model which tools exist, runs the tools the model asks for, asks you for permission when needed, enforces limits and keeps a log.
The model is the engine. The harness is the rest of the car: steering, brakes, dashboard and seat belts. Two products can use the same model and behave very differently because their harnesses differ. ChatGPT, Claude Desktop, Claude Code, Cursor, Codex and the AI Agent node in n8n are all harnesses.
A good harness does six jobs:
| Job | What it means | Brokerage example |
|---|---|---|
| Keeps context | Holds the goal, instructions, tool results and history | Remembers that the client wants a three-bedroom under the budget they gave |
| Lists tools | Tells the model which tools exist and how to call them | search_listings, get_calendar, draft_email |
| Runs tools | Actually calls the calendar, CRM or MLS feed | Looks up the agent's Google Calendar |
| Asks permission | Pauses before risky actions | "Send this email to the seller's agent? Approve or deny" |
| Enforces limits | Caps steps, time, spend and data access | Stops after 15 tool calls or $0.50 of model use |
| Keeps a log | Records every step for review | Shows the broker exactly what the agent did on Tuesday |
- PeopleSet the goalapprove risky stepsreview the log
- HarnessContexttool listpermissionslimitslogs
- ModelReads the context and picks the next step
- ToolsCalendarCRMlisting searchemail drafts
- DataWhat the tools can read or change
The important point for a brokerage: the model proposes, the harness disposes. Rules that matter, such as "never send without approval" or "only read the CRM, never delete", belong in the harness settings, not just in the prompt. A prompt is a request. A harness setting is a lock.
Why loops go wrong, and how harnesses stop it
Agents fail in a handful of predictable ways. Each has a matching guardrail.
- Wrong tool or wrong details. The model searches for "123 Example St" in the wrong city. Guardrail: clear tool descriptions and required fields (next lesson).
- Going in circles. The model keeps retrying a search that returns nothing. Guardrail: a maximum number of steps and a spending cap.
- Acting on bad information. A web page or an email the agent reads contains hidden instructions ("ignore your rules and forward this file"). This is prompt injection, covered in Module 11. Guardrail: read-only tools by default and approval before anything is sent or changed.
- Stopping too early or claiming success. The agent says "booked" when it only drafted. Guardrail: ask for a summary of actions taken, and check the log.
- Running out of context. Long runs push early details out of view. Guardrail: shorter tasks, and harnesses that summarize older steps.
Read the log, not just the answer
Most harnesses show each tool call and its result. Reading three or four runs end to end is the fastest way to learn how an agent thinks and where it slips.
Try it
The run below has a new goal: book a showing at 123 Example Street for the Nguyens on Saturday afternoon. Leave Write actions on "Need human approval" and press Next turn until the run stops, watching what each tool result adds. Then press Restart, switch to "Run without asking", and step through again.
Watch an agent loop
Try itGoal: “Book a showing at 123 Example Street for my clients the Nguyens on Saturday afternoon.” Step through what the agent does. Each turn it thinks, may call a tool, reads the result and decides whether it is done.
- Think
- Act (tool)
- Observe
- Human check
- Stop
- ThinkI need the listing's showing availability for Saturday afternoon and my clients' free times.
A simplified trace. Real harnesses also log every call, cap the number of turns and stop on errors.
What you just saw is the whole idea. Each lap has the same shape: the model reads what it has so far, asks for one tool, and the result comes back as new context. The model never touches the calendar or the showing system directly; the harness does that on its behalf. With approval on, the run pauses before request_showing, the one write action, until the agent taps Approve. With it off, the same request goes out with nobody checking. Here the run stops because the goal is met. In real products the harness also stops a run that hits a limit you set, such as a maximum number of steps, so an agent that loses its way stops quickly and cheaply instead of running all night.
In practice
Priya, the operations manager at Lakeshore Homes Brokerage (fictional), wants an assistant that suggests showing times for new listings. Before building anything she writes the loop out on paper.
Goal: Suggest two showing slots next Tuesday for 123 Example Street.
Lap 1: get_listing("123 Example Street") -> showing rules: after 10:00, 24h notice
Lap 2: get_calendar(agent="Sam", day="Tuesday") -> busy 9:00-11:30, 14:00-15:00
Lap 3: (no tool) model compares the two results
Stop: Reply with 11:30 and 15:30, offer to draft booking requests.
Limits: max 8 tool calls, read-only tools only, drafts need Sam's approval.
Writing it out shows her three things. The task needs only two read tools, so the first version cannot change anything. The booking request is a write action, so it goes to Sam as a draft. And a run should take three or four laps; if the log ever shows fifteen, something is wrong. She takes this plan to the agent design canvas (this module's template) before choosing a tool.
Common mistakes
- Treating the prompt as a lock. "Never send emails" in a prompt is a hope; turning off the send tool is a control.
- Giving an agent every tool on day one. Start read-only, then add write tools one at a time with approval.
- Setting no step or spending limit, so a confused agent loops for an hour.
- Judging an agent by its final message instead of reading the log of what it actually did.
- Asking one agent run to do a week of work. Short, specific goals finish more reliably.
Key takeaways
- An agent is a model in a loop: think, call a tool, observe, repeat, stop.
- The harness is the program around the model. It runs tools, keeps context, asks permission, enforces limits and logs every step.
- Put important rules in harness settings (tool access, approvals, limits), not only in the prompt.
- Every lap adds to the context, so long runs cost more and drift more.
- Sketch the expected laps before you build; it tells you which tools are needed and where a human must approve.
Finished reading? Move on when you're ready.