Home / Fundamentals and the agent loop
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.
Claude Code's agent loop
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 this establishes: This sequence establishes that Claude Code chained several turns of the loop, reading, running, reading the result, editing and running again, before handing back control.
What this does not establish: It does not establish that the edit fixed the test, since the result of the second run is not known at this point in the account.
The three most common miscalibrations
- Too broad This sequence proves that Claude Code fixes any failing test without ever picking the wrong line.
- Too narrow This sequence shows only an isolated tool call, with no link between reading the test and editing the code.
- Off target This sequence shows that the project has an automated test suite covering the file concerned.
- 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.
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.
Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.