Sub topic 3
How the agent loop works
This chapter assumes you have used chat-based AI assistants in an IDE, can read a Git diff, and can run build and test commands in a terminal. It explains what the agent does between your prompt and its final answer, so you can predict its behavior and spot where it goes wrong.
The loop in one pass
In ask mode, the model reads your question and replies once. In agent mode, the model runs in a loop. Each pass has four steps:
- Plan. The model decides what to do next, based on your prompt and everything gathered so far.
- Call a tool. It requests an action, such as searching the workspace, reading a file, editing a file, or running a terminal command.
- Observe. The editor runs the tool and sends the result back to the model. The result might be file contents, a search hit list, a compiler error, or test output.
- Decide. The model either picks the next action, which starts another pass, or decides the task is done and writes a summary.
The model never touches your files directly. It emits a structured tool request, and the editor carries it out. This is why approval prompts and permission settings can sit between the model and your machine.
A worked example
Suppose you send this prompt:
Add input validation to the createUser function in src/users.ts.
Reject empty names and malformed email addresses with a clear error.
Add unit tests for both cases next to the existing tests, then run `npm test` and fix any failures.
Do not change the public function signature.A typical run looks like this:
| Pass | Tool call | What the model learns |
|---|---|---|
| 1 | Read `src/users.ts` | The current shape of `createUser` |
| 2 | Search for `users.test.ts` | Where the existing tests live |
| 3 | Edit `src/users.ts` | Validation is added |
| 4 | Edit `src/users.test.ts` | Two new tests are added |
| 5 | Run `npm test` | One test fails: the error message differs from the assertion |
| 6 | Edit `src/users.ts` | The message is corrected |
| 7 | Run `npm test` | All tests pass, so the model stops |
Passes 5 to 7 are the part that makes agent mode different from edit mode. The model checks its own work against a real result and corrects itself. Your prompt supplied that check by naming the command to run.
What the model carries between passes
Every tool result is appended to the conversation, so each new decision is made with the full history in view. This history has a size limit, called the context window. Long runs with large file reads and verbose test logs fill it. When it fills, the editor may summarize or drop older material, and the model can lose track of earlier constraints. Chapter-level symptoms are covered later under troubleshooting, but the practical rule is simple: put hard constraints in your prompt or an instructions file, not only in the middle of a long session.
Where loops stop
A run ends in one of these ways:
- The model decides the task is complete and replies without a tool call.
- You stop it with the stop button.
- You decline a tool call that needs approval, and the model must choose another route or ask you.
- The editor hits a limit on tool calls per turn and asks whether to continue.
Common errors
- Declaring success without checking. If your prompt names no verification step, the model may stop after editing. Always name the test or build command.
- Repeating a failing action. The model sometimes retries the same command with small changes. If you see the same error three times, stop the run and add the missing information yourself.
- Fixing the test instead of the code. When a test fails, the model may weaken the assertion. Review test edits as carefully as source edits.
- Drifting outside scope. Without a constraint, a loop can edit extra files. State what must not change.
Exercise
In the worked example, why does the agent run npm test a second time in pass 7 after fixing the message in pass 6?
Show answer
Each pass ends with an observation. The edit in pass 6 only changed the code, so the model has no evidence it worked. Running the tests again returns a real result, which lets the model confirm success before it stops.
Exercise
A long agent session starts ignoring your rule "do not edit package-lock.json". What is the likely cause, and what is one fix?
Show answer
Earlier messages may have been summarized or dropped as the context window filled. Put the rule in a custom instructions file, or repeat it in a new prompt, so it is present on every pass.