Skip to content
mentoor

Topic 1 of 5 · 4 min read

Inside the terminal agent loop

Goal: Trace a terminal agent's loop in a transcript and say which tool call read, changed or ran something.

A terminal coding agent is a language model in a loop: it asks for a tool call, your machine runs it, the result goes back to the model, and this repeats until the model decides it is done.

Check yourself · 1 of 3

In a terminal coding agent, what actually reads and writes the files on your machine?

Choose one answer

Why it matters

Claude Code, Codex CLI, Gemini CLI and Aider look different, but they share this loop. If you know the loop, a transcript stops being a wall of text. Every line is one of a few things, and each one tells you whether the agent only looked at your code or changed it.

The loop, step by step

  1. You give a task in plain words, for example "make parseDate reject empty strings".
  2. The model plans and picks a tool. The model cannot touch your files. It only writes a request such as "read src/date.ts".
  3. The CLI runs the tool on your machine, in your repo, with your permissions.
  4. The output goes back to the model as new context: file contents, a diff, test output or an error.
  5. The model decides the next step: read more, edit, run a command, or stop and report.

The tools fall into three groups. Read tools (open a file, search with grep, list a folder) change nothing. Edit tools write files. Run tools execute shell commands, and a shell command can do anything you can do: run tests, install packages, delete a folder or push to a remote.

A short trace

> make parseDate reject empty strings
Read   src/date.ts
Grep   "parseDate" src/        (14 matches)
Edit   src/date.ts             (+3 -0)
Run    npm test -- date        (1 failed)
Read   test/date.test.ts
Edit   test/date.test.ts       (+6 -0)
Run    npm test -- date        (all passed)

Read, Grep and Read changed nothing. The two Edit lines changed files. The two Run lines executed code. Note the loop: a failed test sent the agent back to read and edit again.

What goes wrong

  • The loop has no memory beyond its context. In a long session, early instructions can fall out of focus and the agent may repeat or contradict itself.
  • The agent only knows what it read. It can edit a function without opening its callers, then report success.
  • "Done" is the model's own judgement. It stops when it believes the task is complete, not when it is. A passing test only counts if the test checks the right thing.
  • Every Run is real. Output goes back into the context, so a command that prints a secret puts that secret in front of the model.

Before and after

Before: you read only the final message ("Fixed, all tests pass") and merge. After: you scan the tool lines, see the agent edited the test file as well as the source, open that diff, and find it loosened an assertion. The loop was the same. What changed is that you looked at which calls wrote files.

Examples

Spot the write

Read   src/cart.ts
Read   src/cart.test.ts
Edit   src/cart.test.ts   (+0 -4)
Run    npm test

The agent was asked to fix a cart bug but the only edit is to the test file, and it removed four lines. Tests that now pass may only mean the checks were deleted. Open that edit before anything else.

Different tools, same loop

Claude Code shows Read, Edit and Bash calls. Aider prints each edit and makes a git commit. Codex CLI and Gemini CLI list shell commands to approve. The names differ, but each line still falls into read, edit or run.

Key points

  • A terminal agent is a model in a loop: request a tool, run it locally, read the result, decide the next step.
  • The model never touches your files itself; the CLI runs each read, edit and shell call on your machine.
  • Read tools change nothing, edit tools change files, and run tools can do anything your shell can do.
  • The agent decides when it is done, so its final summary is a claim to check, not evidence.
  • Scanning the tool lines in a transcript shows what was read, what was changed and what was executed.

Common mistakes

  • Reading only the final summary and skipping the tool calls.
  • Assuming the agent read the files it edited or the code that calls them.
  • Treating a run command as harmless because it was "just a test" or "just a script".
  • Thinking the model has direct access to your disk, when the CLI performs every action with your permissions.

Check yourself · 2 of 3

Which transcript line is the only one that cannot change anything on disk or in the system?

Choose one answer

Check yourself · 3 of 3

After a test fails, what does the agent loop normally do next?

Choose one answer

Practice items, not counted towards your mastery.

Start here