Sub topic 1
How a Coding Agent Acts: The Model, the Tools, and the Loop
Prerequisites
This chapter assumes you can use a terminal and basic shell commands, know basic git, and have seen an AI coding assistant edit files or run commands. No other background is needed.
The model only produces text
A language model reads text and writes text. It cannot open a file, run ls, or send a request. A coding agent is a program, often called the harness, that wraps the model and does those things for it.
The harness gives the model a list of tools. A tool is a named function with described inputs, such as read_file(path) or run_command(command). When the model wants to act, it writes a structured tool call instead of plain prose. The harness reads that call, runs the real function on your machine, and sends the result back to the model.
This split is the basis of agent permissions. The model only proposes actions. The harness decides whether each one actually runs.
The loop
An agent repeats the same cycle until the task is done:
- The harness sends the conversation and the tool list to the model.
- The model replies with text, a tool call, or both.
- If there is a tool call, the harness executes it.
- The harness appends the result to the conversation and returns to step 1.
- The loop ends when the model replies with no tool call.
The harness is the only place where model output becomes a real action. That makes it the natural place for a permission check.
A minimal loop in code
This runnable sketch replaces the model with a scripted stand-in so you can see the structure. The comment marks where a real harness would check permissions.
import subprocess
def run_command(command: str) -> str:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
return result.stdout + result.stderr
TOOLS = {"run_command": run_command}
def scripted_model(messages):
"""Stand-in for a real model: one tool call, then a final answer."""
if not any(m["role"] == "tool" for m in messages):
return {"tool": "run_command", "args": {"command": "echo hello"}}
return {"text": "Done. The command printed: " + messages[-1]["content"].strip()}
def agent_loop(task: str) -> str:
messages = [{"role": "user", "content": task}]
while True:
reply = scripted_model(messages)
if "tool" not in reply:
return reply["text"]
# A real harness decides here: allow, ask, or deny.
output = TOOLS[reply["tool"]](**reply["args"])
messages.append({"role": "tool", "content": output})
print(agent_loop("Say hello"))The line TOOLS[reply["tool"]](**reply["args"]) runs whatever the model asked for. Without a check before it, the model's output has the same authority as you typing the command yourself.
Why the loop matters for permissions
Three properties of the loop shape every later chapter.
- The model's choices are not fully predictable. The same task can produce different tool calls on different runs.
- The model reads untrusted input. File contents, command output, and web pages all re-enter the conversation as tool results. Text in them can influence the next tool call.
- Actions run with your privileges. The harness runs as your user. It can read what you can read and delete what you can delete.
Where controls attach
The harness can intervene at three points: before a tool call runs (approve or block), around it (restrict what the process can reach), and after it (review what happened). The rest of this tutorial covers each in turn, starting with how to sort tool calls by risk.
Exercise
In the loop above, why is the step between receiving a tool call and executing it the right place for a permission check? Name one reason a check inside the model's prompt would be weaker.
Show answer
That step is the only point where model output turns into a real action, and the harness controls it, so the model cannot bypass a check there. A prompt instruction is only text the model may ignore, misread, or be talked out of, for example by instructions hidden in a file it reads.