Sub topic 1
How an Agent Acts: The Loop and Its Tool Calls
An AI coding agent does not change your files or run commands by itself. It asks a program around it to do those things. This chapter describes that arrangement, because every hook in later chapters attaches to it.
Prerequisites
This chapter assumes you can:
- Use a shell and read simple commands such as
ls,rm, andgit push. - Read JSON objects and fields.
- Read short Python scripts.
- Describe an AI coding agent in general terms, as a model that can call tools.
The agent loop
An agent is two parts working together:
- The model is the language model. It reads text and produces text.
- The harness is the program around the model. It sends your prompt to the model, runs tools, and feeds results back.
The model cannot run anything. When it wants to act, it outputs a structured request called a tool call. The harness reads the request, runs the tool, and sends the result back to the model as new input. This repeats until the model replies with plain text and no tool call.
The diagram shows the key point. The model proposes and the harness executes, so a gap exists between the two. A hook runs in that gap.
What a tool call looks like
Each tool call names a tool and gives its arguments. The exact field names differ between agents, so check your agent's documentation. The shape is usually like this:
{
"tool_name": "shell",
"tool_input": {
"command": "git status"
}
}Common tools include:
| Tool kind | What it does | Typical input |
|---|---|---|
| Shell | Runs a command in your terminal | `command` string |
| File write or edit | Creates or changes a file | `path` and new content |
| File read | Returns file contents to the model | `path` |
| Web or API call | Fetches data from outside | URL or request body |
Each result goes back into the model's context. The model uses it to choose its next step. A single request such as "fix the failing test" can therefore produce dozens of tool calls, and you may not read each one.
Why the loop matters for safety
Three facts follow from this design.
- The model's text is only a proposal. Nothing happens until the harness runs the tool call.
- The harness runs tools with your permissions. A shell command from the agent can delete anything your account can delete.
- The model can be wrong. It can misread a task, guess a path, or follow instructions hidden in a file or web page it read earlier.
You usually cannot control what the model decides. You can control what the harness lets through. A pre-action hook is a small script that receives the proposed tool call, inspects it, and can stop it before the tool runs. It sees the call, not the model's reasoning. It judges only the action about to happen.
The next chapters cover which actions deserve a check, which lifecycle events exist, and how a hook reports its decision.
Exercise
A model outputs a tool call for rm -rf build/. At what point has the directory actually been deleted, and where could a pre-action hook stop it?
Show answer
The directory is deleted only when the harness runs the shell tool, after the model proposes the call. The model's output is just a request. A pre-action hook runs between the proposal and the execution, so it can inspect the call and block it before anything is deleted.