Sub topic 1
Starting point: how a single coding agent works
Prerequisites
This chapter assumes you have used an AI coding assistant in a terminal or editor. It also assumes you know what a context window and a tool call are, and that you can read short Python snippets. Later chapters add Git branches and the command line.
The agent loop
A coding agent is a program that repeats one cycle until the task is done. A language model reads its current input, decides what to do next, and either answers or asks to run a tool. The program runs the tool and feeds the output back to the model.
The cycle has three parts:
- Prompt. The model receives the system instructions, your request, and everything that has happened so far.
- Tool call. The model asks for an action, such as reading a file, running a command, or editing code.
- Observation. The program runs the tool and appends the result to the conversation. The model reads it on the next turn.
The loop ends when the model replies without requesting a tool, or when a limit such as a step cap is reached.
A minimal loop in code
The sketch below shows the structure. The model is a stub, so the code runs as written. A real agent would call a model API where fake_model is.
import subprocess
def fake_model(messages: list[dict]) -> dict:
# Stub: asks for one tool call, then answers.
if not any(m["role"] == "tool" for m in messages):
return {"tool": "shell", "args": "echo hello from the tool"}
return {"answer": "Done. The command printed a greeting."}
def run_tool(name: str, args: str) -> str:
if name == "shell":
result = subprocess.run(args, shell=True, capture_output=True, text=True)
return result.stdout + result.stderr
return f"unknown tool: {name}"
def agent(request: str, max_steps: int = 10) -> str:
messages = [{"role": "user", "content": request}]
for _ in range(max_steps):
reply = fake_model(messages)
if "answer" in reply:
return reply["answer"]
observation = run_tool(reply["tool"], reply["args"])
messages.append({"role": "tool", "content": observation})
return "Stopped: step limit reached."
print(agent("Say hello using the shell."))Note that messages only grows. Every observation is appended and sent back to the model on every later turn.
What accumulates
The conversation holds more than your request. Over a real task it collects:
- file contents the agent read, including files it only needed once
- command output, such as test logs and build errors
- failed attempts and the corrections that followed
- the agent's own reasoning and edits
All of this stays in the context window, the fixed amount of text a model can consider at once. Large outputs use that space quickly. A single verbose test run can add thousands of tokens.
Where one agent runs into limits
For small tasks, one loop works well. Strain appears as the task grows:
| Symptom | Cause |
|---|---|
| Slower and costlier turns | The whole history is processed again on every turn |
| Earlier details are missed or contradicted | Important facts are buried among old tool output |
| The task stops or history must be trimmed | The window is full |
| Work happens strictly one step at a time | There is only one loop |
The next chapter looks at these limits more closely. They motivate subagents: separate loops with their own context, started by a parent agent for a scoped piece of work.
Exercise
In the loop above, why does each turn get more expensive as the task goes on, and which list holds the cause?
Show answer
The messages list only grows, because every tool observation is appended. The model receives the entire list on every turn, so later turns process more text than earlier ones.
Exercise
Name the three parts of one agent loop cycle and say which one adds new information to the conversation.
Show answer
The parts are prompt, tool call, and observation. The observation adds new information, because the tool's output is appended to the conversation for the next turn.