HAMMAD YOUSUF

FREE RESOURCE · FROM THE "LOOP" REEL

How AI agents work: the loop, the harness, and your first small agent in Claude Code

By · 1 November 2026

An agent is a model inside a harness that runs a loop: build context, decide, act, observe, repeat.

In four lines

  • An agent is a model inside a harness that runs a loop: build context, decide, act, observe, repeat.
  • The model only writes text; the harness runs the tools and feeds the results back.
  • Set a step cap: without one, a failing call can repeat and every try costs money.
  • Guardrails, permissions, human sign-off, evals and logs sit around every action.
The LOOP reel: the agent loop lights up step by step, then the harness around it. Watch on Instagram

The agent loop, step by step

This is the order the reel lights up. The model is one box in it; the harness is everything else.

  1. Trigger: you, an event, a timer or another agent starts it

  2. Build context: instructions, tool list, chat so far, recalled memory, earlier tool results

  3. Model decides: answer now, or call a tool

  4. Act: the harness runs the tool (search, code, files, APIs, MCP servers, sub-agents)

  5. Observe: the result goes back into the context and the loop runs again

  6. Final output: an answer, a finished action or a handoff

  7. Around it all: memory, guardrails, observability and evals

One request through the loop

You type: "Move my 3pm with Omar to Thursday."

1/ Context

  • The model can't see your calendar. It only sees what the harness hands it.
  • Your message, its instructions, the tools it may use, anything pulled from memory.
  • If nothing says which Omar, it has to guess or ask.

2/ Decide

  • It reads all of that and picks one next step: reply now, or use a tool.
  • Here it needs the calendar.
  • The model only writes text. It can't move a meeting on its own.

3/ Act

  • It writes a tool call: "find today's 15:00 event with Omar".
  • The harness runs that call against your calendar.
  • Get the date wrong in the call and it finds nothing.

4/ Observe

  • The result comes back: "1 event found", or "no match".
  • The harness adds it to the context and asks the model again.
  • Now it decides: move it, ask you, or stop.

That loop repeats until the model says it's done or a limit stops it. With no step cap, it can retry the same failing call again and again, and you pay for every one.

The checks around the loop

  • Permissions: what the agent may touch
  • Guardrails: what it must never do
  • Evals: whether its results are actually right
  • Human sign-off: who approves the risky actions
  • Traces and logs: what it did, and why

So understanding an agent means seeing what it was given, what it called, what came back, and who checked it. Once you can see that, you can debug it and make it better.

Build your first small agent in Claude Code

Reading about the loop is one thing; watching it print is another. Paste this into Claude Code in an empty folder with a ./sandbox of a few text files.

Build a tiny agent loop in Claude Code

prompt · claude code
Build me the smallest possible AI agent in Python, in one file, so I can see the loop. It should: take a request from the command line; send the model the request plus a list of two tools (read_file and list_files, limited to ./sandbox); if the model asks for a tool, run it, print what happened, add the result to the conversation and ask the model again; stop when the model answers without a tool call or after 8 steps. Print every step (context size, tool call, result) so I can watch the loop. Explain each part in comments.

What to look for when it runs

  • Context size grows after every step: that is the observe step feeding results back in.
  • Each tool call names a tool from the list of two and nothing else: the model only writes text, the harness runs it.
  • The run stops when the model answers without a tool call, or at step 8: that is the step cap doing its job.

The step cap and one guardrail

The step cap

The prompt stops the loop after 8 steps. Lower it to 3 for a first run and watch what the agent does when it runs out of steps; a failing call that repeats is the most common way an agent burns money.

One guardrail

The two tools are limited to ./sandbox. Keep it that way: an agent that can read any file on your machine has a permission it never needed for the task.

Common questions

What is the difference between an AI agent and a chatbot?

A chatbot answers with text and stops. An agent is a model inside a harness that runs a loop: it builds context, decides whether to answer or call a tool, the harness runs the tool, the result goes back in, and it repeats until the task is done or a limit stops it.

Does the model itself run the tools?

No. The model only writes text, including the text of a tool call. The harness around it runs the tool (search, code, files, APIs, MCP servers, sub-agents) and feeds the result back into the context.

What is ReAct?

The name of the pattern most agents run: the model reasons, takes an action such as a tool or API call, reads the observation and repeats (Yao et al., 2022, arxiv.org/abs/2210.03629).

Why does an agent need a step cap?

Without one, a failing call can repeat and every try costs money. Anthropic's guide lists stopping conditions such as a maximum number of iterations, plus human checkpoints, as part of building effective agents.

Do I need Claude Code to follow this?

No. The loop is the same in any agent framework. Claude Code is used here because it reads files, runs commands and writes code in your project, so one prompt produces a runnable one-file agent you can watch.

Sources checked

  • ReAct interleaves reasoning and actions: the model reasons, takes an action (e.g. a tool or API call), reads the observation, and repeats. Source ↗
  • Agents are LLMs using tools based on environmental feedback in a loop; they need stopping conditions such as a maximum number of iterations, and human checkpoints for oversight. Source ↗
  • Claude Code reads files, runs commands and writes code in your project. Source ↗

About the video: the reel has no presenter; it is an animated board. The small headshot in it is an AI-generated image of Hammad Yousuf.

Keep going