Skip to content
Agent Builder
Module 1Full course9 min read

The Agent Loop: Think, Act, Observe

The simple loop at the heart of every agent. Trace a real agent run step by step and learn where things go right and wrong.

Every agent you will ever use, from a coding assistant fixing a bug to a research agent writing a 20-page report, runs on the same simple loop. The model thinks about what to do, takes an action with a tool, observes the result, and repeats. That's the entire engine. The impressive part isn't the loop itself; it's what a capable model can accomplish by going around it dozens or hundreds of times.

Understanding this loop is the single most useful mental model in the course. In this lesson you'll trace a real agent run step by step, see exactly what the model sees at each turn, learn the four ways a loop can end, and build a field guide to where loops go wrong, so that when an agent misbehaves you can tell what happened and how to fix it.

The loop in one picture

Here's the agent loop with nothing left out:

          ┌──────────────────────────────────────────┐
          │                                          │
          ▼                                          │
  ┌───────────────┐    ┌───────────────┐    ┌────────┴──────┐
  │    THINK      │───▶│     ACT       │───▶│   OBSERVE     │
  │ model decides │    │ program runs  │    │ result added  │
  │ next step     │    │ the tool      │    │ to context    │
  └───────┬───────┘    └───────────────┘    └───────────────┘
          │
          │ no more tools needed
          ▼
     Final answer

Each pass through the loop is called a turn or an iteration. Two very different things share the work:

  • The model does the thinking. On each turn it reads the entire context, meaning your request, its instructions, and everything that has happened so far, and produces either a request to use a tool or a final answer.
  • The harness does everything else. The harness is the ordinary program wrapped around the model: it sends the context to the model, executes the tools the model asks for, appends the results, checks permissions, and enforces limits. Claude Code, Codex, and Cursor are, in large part, very well-built harnesses around a model.

That split matters. The model never touches your files directly. It only ever writes text that says, in effect, "please run this tool with these inputs". The harness decides whether to do it.

In code, the core of a harness is surprisingly small. Here's the shape, written as labeled pseudo-code; you'll write a real, runnable version in A Minimal Agent Loop in 60 Lines:

PSEUDO-CODE
 
context = [system instructions, tool descriptions, user's request]
 
repeat up to MAX_TURNS times:
    response = model(context)               # THINK
    add response to context
    if response has no tool requests:
        return response                     # done: final answer
    for each tool request in response:
        result = run_tool(request)          # ACT
        add result to context               # OBSERVE