Sub topic 1
How a coding agent reads, decides, and acts
Prerequisites
This chapter assumes you can do the following:
- Use git and a code hosting platform: repositories, issues, and pull requests.
- Work at the command line.
- Read short Python and YAML snippets.
- Describe a large language model (LLM) in general terms and have used an AI coding assistant.
You do not need any security background. This chapter builds the working model that the later chapters on injection and defenses rely on.
The agent is a loop around a model
A coding agent is a program that repeatedly asks a language model what to do next, runs the action the model picks, and feeds the result back. The model only produces text. The surrounding program, called the harness, turns that text into real effects such as reading a file or running a command.
One pass through the loop looks like this:
- The harness builds a prompt (the full text sent to the model). It contains the system prompt, the user's request, the tool descriptions, and everything gathered so far.
- The model replies with either a final answer or a tool call. A tool call is a structured request such as "run
pytest -q" or "readsrc/app.py". - The harness checks the request, often asks the user for approval, and runs the tool.
- The harness appends the tool's output to the conversation and returns to step 1.
The loop ends when the model produces a final answer, the user stops it, or a step limit is reached.
The arrow from the tools back into the context is the important one for this tutorial. Every pass through the loop adds text that someone other than the user may have written.
What the agent reads
The context window is the text the model sees on a single call. It is assembled from several parts, and they do not all come from the same author.
| Part of the context | Usual author | How much you control it |
|---|---|---|
| System prompt | Tool vendor | Little |
| User request | You | Full |
| Rules files, such as project instruction files | Whoever committed them | Depends on the repository |
| File contents the agent opens | Repository contributors, dependencies | Varies |
| Search results, fetched web pages, documentation | Third parties | Little |
| Output of shell commands | Programs, test suites, build scripts | Varies |
| Issue and pull request text | Anyone who can post | Little |
The model receives all of this as one stream of text. It has no separate channel marking which parts are trusted. Later chapters explain why that matters. For now, note that most of the table is text you did not write.
How the agent decides
On each pass the model predicts the next piece of text given everything in the context. When tools are available, that prediction can take the form of a tool call. The model does not run a program or check a rule list. It continues the text in the way its training suggests is most useful.
Three consequences follow:
- Decisions are influenced by everything in the context, including a file the agent opened five steps ago.
- The same input can produce different decisions on different runs.
- A good-looking plan is not a guarantee. The model may misread a file, or may follow wording in it that sounds like a task.
The harness is where firm rules live. The model suggests an action, and the harness decides whether the action runs. This split is the basis for most defenses covered later.
How the agent acts
Tools are the agent's only way to affect the world. A typical coding agent offers some mix of the following:
- Read and search files.
- Write or edit files.
- Run shell commands.
- Fetch a URL or call an API.
- Call tools from external servers through the Model Context Protocol (MCP), a standard for plugging tools into agents.
Each tool inherits the permissions of the process that runs it. If the agent runs in your terminal, a shell tool can read your home directory and use your cloud credentials. If it runs in CI, it can use the job's token. The model never needs to "hack" anything to do this. It only has to ask the harness for an action that the harness is willing to run.
A worked example
The following is a minimal agent loop in Python. It is not a real model client. The ask_model function is a stand-in that returns a fixed script so the loop runs as shown. The structure matches real agents.
import subprocess
ALLOWED = {("git", "status"), ("git", "diff")}
SCRIPT = [
{"tool": "run", "argv": ["git", "status"]},
{"tool": "run", "argv": ["rm", "-rf", "build"]},
{"final": "Checked the repository status."},
]
def ask_model(messages, step):
# Stand-in for a real model call. A real model reads `messages`.
return SCRIPT[step]
def run_tool(argv):
if tuple(argv) not in ALLOWED:
return f"DENIED: {argv}"
result = subprocess.run(
argv, capture_output=True, text=True, timeout=30, check=False
)
return result.stdout + result.stderr
def agent(task):
messages = [{"role": "user", "content": task}]
for step in range(10): # step limit
reply = ask_model(messages, step)
if "final" in reply:
return reply["final"]
output = run_tool(reply["argv"])
print(f"step {step}: {reply['argv']} -> {output[:60]!r}")
messages.append({"role": "tool", "content": output})
return "Stopped: step limit reached."
if __name__ == "__main__":
print(agent("What is the state of this repo?"))Read it with these points in mind:
messagesis the context. Each tool output is appended to it, so it grows on every pass.run_toolis the harness decision point. The scripted second step asks forrm -rf build, and the allowlist denies it. The denial text goes back into the context like any other output.- The
range(10)limit is a basic safeguard against endless loops. - Whatever
run_toolreturns becomes input to the next model call. If the output came from a file or a web page, the model reads it with the same attention it gives your request.
Run it inside a git repository and you will see git status run and rm -rf build get denied.
Where the loop is exposed
Looking back at the loop, text from outside enters at three points:
- At the start, through rules files and repository contents the agent loads.
- At each tool result, through file contents, command output, and fetched pages.
- Through the user's own request, when the user pastes text from somewhere else, such as an issue body or a log.
The harness and the user's approvals sit between the model's suggestion and the effect. This chapter does not argue those layers are weak. It establishes where they are. Later chapters show what happens when text from the second entry point carries wording that reads like an instruction, and how to limit the damage.
Common mistakes in the mental model
- Thinking the model "runs" commands. It requests them. The harness runs them, with your permissions.
- Thinking only your prompt steers the agent. Anything read during the task can steer it.
- Thinking read-only work is harmless. Reading is how outside text gets in, and a read-only agent that can also reach the network may still send data out. Later chapters cover this combination.
- Thinking an approval prompt makes every action safe. A prompt helps only if the person reads what is being approved, and people tire of prompts.
Exercise
In the loop described above, name the component that decides whether a tool call actually runs, and name the point where text written by a third party most often enters the context during a task.
Show answer
The harness (the program around the model) decides whether a tool call runs, often with user approval. Third-party text most often enters through tool results, such as file contents, command output, fetched pages, and issue or pull request text that the agent reads and appends to its context.
Exercise
In the Python example, the model requests rm -rf build. What happens, and what does the model see on the next pass?
Show answer
run_tool finds that ("rm", "-rf", "build") is not in ALLOWED, so it does not run the command. It returns the string DENIED: ['rm', '-rf', 'build'], which is appended to messages, so the model sees the denial as the latest tool output on the next pass.
Exercise
A teammate says, "Our agent only has read access to files, so nothing it reads can hurt us." Give one reason this reasoning is incomplete.
Show answer
Reading is how untrusted text reaches the model, and the model's next decision is influenced by it. If the agent also has any other tool, such as a shell, a network fetch, or a way to post comments, then text it read can steer it to use that tool. Read-only file access limits writes but not what the agent does with other tools or where it sends what it read.