Sub topic 2
How the agent loop works
This chapter assumes you can use a shell, run your project's tests, and know roughly what a large language model (LLM) is. It explains the loop that Claude Code, Codex CLI, and Copilot CLI share. The rest of the tutorial builds on it.
The loop in one pass
A terminal coding agent does not write code in one shot. It repeats three steps until the task is done or you stop it:
- Model call. The tool sends the conversation so far to the LLM. The model replies with text, a request to use a tool, or both.
- Tool call. If the model asked for a tool, the tool checks permissions and then runs it. Examples are reading a file, editing a file, or running
npm test. - Observation. The tool's output (file contents, a diff, test failures) is added to the conversation. The loop returns to step 1.
The loop ends when the model replies with no tool request. That final reply is the answer you see.
A worked example
Suppose you type: "The date parser fails on ISO week dates. Fix it." A typical run looks like this:
| Step | Model asks for | Observation returned |
|---|---|---|
| 1 | Search for `parseDate` | Three matching files |
| 2 | Read `src/date.ts` | File contents |
| 3 | Run `npm test` | Two failing tests with stack traces |
| 4 | Edit `src/date.ts` | Edit applied |
| 5 | Run `npm test` | All tests pass |
| 6 | None | Summary of the change |
The model never touches your disk itself. It only emits a structured request, and the tool does the work. This is why permission controls sit between steps 1 and 2 of the loop, and why later chapters cover approval modes and sandboxing.
Why the loop matters in practice
- The model acts on what it observes. If it never ran the tests, it does not know whether its edit works. Ask it to run them.
- Everything accumulates. Each observation stays in the context window, the limited amount of text the model can consider at once. Long outputs crowd out earlier instructions.
- Each pass costs time and tokens. A vague task leads to many exploratory tool calls. A specific task needs fewer.
- Errors compound. A wrong early assumption becomes part of the history and shapes later steps. If a run goes off track, correct it early or start a fresh session.
Common errors
- Treating the final message as proof. The agent may say "fixed" without running anything. Check the transcript for a test run, or run the tests yourself.
- Assuming the agent sees everything. It only knows what it has read or been told. Point it to files and instruction files.
- Letting the loop run unbounded. A repeated failing step can burn tokens. Interrupt it and narrow the task.
Exercise
In the loop, who executes a command such as npm test, and what happens to its output?
Show answer
The terminal tool executes it, after checking permissions, not the model. The model only requests it. The output is added to the conversation as an observation, and the next model call uses it to decide what to do next.
Exercise
An agent reports "Fixed the bug" but the transcript shows no test run. Which part of the loop is missing, and what should you do?
Show answer
There is no observation confirming the fix. Ask the agent to run the tests, or run them yourself, before accepting the change.