Skip to content
Mastering Claude

Home / Fundamentals and the agent loop

Fundamentals and the agent loop8 minFondation

Claude Code and the agent loop

Claude Code chains reflection, a real tool call and reading the result in a loop until the goal is reached, it does not answer once like a chat.

Claude Code does not return a single reply and then wait for a new question. Faced with a goal, it thinks about what action to take, calls a real tool, reading a file, running a command, modifying code, then observes what that tool produced before deciding what comes next. This loop repeats as many times as needed, until the goal is reached or control is handed back to the user.

What the loop changes compared with a chat

A classic conversational assistant produces a reply and stops there. Claude Code actually runs the commands it proposes, reads what they return on standard output and standard error, and adjusts its plan based on that result. Giving a complete goal, rather than a sequence of instructions broken down step by step, lets this loop deploy its own intermediate steps. A request phrased as fix the failing test in calc.test.js triggers a sequence that no one needs to spell out in advance: reading the test file, running the suite, reading the error message, fixing it, running it again.

Goal received: fix the failing test in calc.test.js

Reflection: read the test file to understand the failure
Tool: reading calc.test.js
Observation: the test expects 4, the function returns 5

Reflection: read the function concerned
Tool: reading calc.js
Observation: an addition replaces a subtraction

Reflection: fix the faulty line
Tool: editing calc.js
Observation: file modified

Reflection: check that the test now passes
Tool: running the test suite
Observation: loop finished, control handed back

Interrupting without losing the loop

The loop runs visibly in the terminal: every reflection, every tool called and every observation displays as it happens, not just the final result. This makes it possible to follow a long task without having to restart it to know where it stands, and to interrupt it partway through if the direction it is taking is not right, while keeping what has already been accomplished.

What feeds each turn of the loop

Each reflection draws on what the session already knows about the project. Part of this knowledge comes from a file read automatically at the start of the session, CLAUDE.md, covered in the next lesson, which sets rules before even the first turn of the loop. Without a complete goal or starting context, the loop still runs, but it then spends more turns discovering information that a memory file would already have carried.

Figure 1

Claude Code's agent loop

01
Goal received
The user states the outcome wanted, not the sequence of commands to type one by one.
02
Reflection
Claude Code chooses the next action based on what it already knows about the task under way.
03
Tool call
A real action runs, reading a file, editing code or a shell command.
04
Observation
The tool's actual result, output or error message, is read before any new decision.
05
Decision
A new turn of the loop begins, or control is handed back once the goal is reached.
Five steps follow one another from a complete goal, and the last one leads either to a new turn or to the end of the loop once the goal is reached.
Calibrate it yourself

A developer asks Claude Code to fix a failing test in a Node project. Claude Code reads the test file, runs the test suite, reads the error message displayed, edits a line in the source file, then reruns the test suite.

Write in one sentence what this situation establishes, and in one sentence what it does not establish.

What to remember
  • The agent loop chains reflection, a real tool call and reading the result, and it repeats until the goal is reached or control is handed back.
  • A complete goal lets the loop deploy its own intermediate steps, whereas a series of isolated instructions interrupts this mechanism at every step.
  • Every tool call produces a real observation, standard output or an error message, which guides the next decision, it is not a guessed piece of text.
  • The full thread of the loop displays in the terminal as it happens, which makes it possible to follow a long task and interrupt it partway through.
Do this now

Open a Claude Code session on an existing repository and give it a complete goal in one sentence, for example look at what is failing in the test suite and fix it, then watch the sequence of tool calls display without interrupting it.

Check the source

Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.