Sub topic 1
How a coding agent reads, decides, and acts
Prerequisites
This chapter assumes you can use git and a code hosting platform (repositories, issues, pull requests), work at the command line, and read basic Python. You should also know in general terms what a large language model (LLM) is and how an AI coding assistant is used.
The tool loop
A coding agent is a program that wraps an LLM and lets it use tools. The model only produces text. The surrounding program turns some of that text into actions, such as running a command or editing a file. It then feeds the result back to the model.
The cycle repeats until the model decides the task is done:
- Read: the program builds a context, which is all the text the model sees on this turn.
- Decide: the model replies with either a final answer or a request to call a tool.
- Act: the program runs the requested tool, sometimes after asking the user for approval.
- Observe: the tool's output is appended to the context, and the loop starts again.
What goes into the context
The context usually holds several kinds of text:
| Part | Written by | Example |
|---|---|---|
| System prompt | The agent's vendor | Rules about tool use and style |
| User task | You | "Fix the failing test" |
| Project rules | Often the repository | A rules file in the repo |
| Tool output | Whatever the tool read | File contents, command output, an issue body |
The first two rows come from parties you trust. The last two can contain text written by other people. Every file the agent opens and every page it fetches is added to the same context as your task.
A minimal loop in code
The sketch below shows the shape of the loop. The model function is a stand-in for a real LLM call, so the code runs as written.
import subprocess
def model(messages):
# Stand-in for an LLM: first asks to run a command, then finishes.
if not any(m["role"] == "tool" for m in messages):
return {"tool": ["git", "status"]}
return {"answer": "Checked the repository status."}
def run_agent(task):
messages = [{"role": "user", "content": task}]
for _ in range(10): # hard cap on turns
reply = model(messages)
if "answer" in reply:
return reply["answer"]
result = subprocess.run(reply["tool"], capture_output=True, text=True)
messages.append({"role": "tool", "content": result.stdout + result.stderr})
return "Stopped: too many steps."
print(run_agent("Show me the repo status"))Notice that the tool output is appended to messages with no special treatment. The model reads it the same way it reads your task.
Why this matters for security
Two properties of the loop drive the rest of this tutorial:
- The agent acts with your permissions. A shell command runs as your user, and a request uses your credentials.
- Text from files, issues, and docs flows into the same context as your instructions.
Later chapters show how these combine into prompt injection, and which controls limit the damage.
Exercise
In the loop above, which step appends text written by a third party, such as the body of an issue, to the context, and why is that a concern?
Show answer
The Observe step. The tool output, which contains the issue body, is added to the context like any other text. The model may treat instructions inside it as if they came from the user, and the agent's tools run with your permissions.