All courses

A coding agent is a model in a loop

Explain the loop: the model proposes an action (read a file, run a command, edit code), a tool executes it, the output is appended to its input, and this repeats until the model decides to stop. Explain why that makes an agent more capable than a chat reply, and also why it can wander.

Read it card by card Start the whole course

Same model, very different result

You paste a failing test's error into a chat window. You get back a confident fix that calls a helper your code doesn't have. You give the same problem to a terminal agent built on a similar model. It opens files, runs the tests, edits, reruns and returns a working change. It is the same kind of model. So what does the agent have that the chat reply didn't?

The model writes; a loop acts

A model only produces text. In an agent, a program around the model reads that text as a request: read `parser.py`, run `pytest`, replace these lines. Real tools execute the request, and the result is appended to the model's input. Then the model is asked again. So each step is based on real file contents and real errors, and a failed attempt becomes the next clue. The chat reply had only what you pasted.

The loop, step by step

1. **Input**: your task plus every earlier action and its output. 2. The model proposes one or more actions (read, run, edit) or gives a final answer. 3. A tool executes each action. The model itself executes nothing. 4. The tool's output is appended to the input. 5. Repeat until the model replies without an action. Two consequences follow. The loop ends when the model *decides* to stop, not when your goal is checked. And each next action is chosen from whatever is in the input at that moment.

An agent loop in ten lines

This is a stripped-down agent loop. Note three things: - `model()` only returns text and requested calls. - `run_tool()` is what touches your files and shell. - The only exit is the model replying with no tool calls. Real agents add a step limit and ask before risky commands, but the shape is the same. Nothing in this loop compares the result to your goal.
// Python
msgs = [user(task)]
while True:
    reply = model(msgs)
    msgs.append(reply)
    if not reply.tool_calls:
        break  # model chose to stop
    for call in reply.tool_calls:
        out = run_tool(call)
        msgs.append(tool_result(call, out))
print(reply.text)

How the loop wanders

**Task:** fix the failing `test_parse_dates`. 1. The agent runs `pytest`. The test is red, and the output also shows a deprecation warning from `utils/io.py`. 2. It reads `io.py` and "tidies" it. 3. It reruns, sees a new failure there, and fixes that. Twelve steps later, the original test is still red. No single step was crazy; each one followed from the last output. The transcript shows exactly where it turned away. That is where you redirect it.

Remember

An agent is a model in a loop. It proposes an action, a tool runs it, the output joins its input, and this repeats until the model chooses to stop. Real feedback makes it far stronger than a chat reply. But each step is steered by whatever is in its input, often just the latest output, and stopping is a choice, not proof the job is done.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.