Sub topic 1
How a coding agent works on existing code
Prerequisites
This chapter assumes you can read and write code in at least one language and use Git branches, commits, and diffs. You should also have run a test suite and a linter from the command line, and used an AI coding assistant at least once.
What the agent actually does
A coding agent is a language model that can call tools. The tools usually include reading files, searching the code, editing files, and running shell commands. The model does not hold your repository in memory. It sees only the text that tools return to it, plus your instructions.
The agent works in a loop:
- Read. It searches and opens files to find the code that matters.
- Edit. It changes one or more files.
- Run. It executes a command, such as a test suite, a type checker, or a linter.
- Observe. It reads the output and decides what to do next.
The loop repeats until the agent decides the task is done or it gets stuck.
Why existing code is harder than new code
In a new project, almost anything the agent writes is acceptable. In an existing codebase, the agent must work inside constraints it cannot fully see:
- Other code calls the function you are changing, and some of those callers are outside the files the agent opened.
- Behavior that no one documented may be relied on by users or other services.
- Conventions for naming, error handling, and layout exist only in the code itself.
The agent's view is partial, and that is the main risk. It can make an edit that looks correct in the file it read and still break a caller it never opened.
A worked trace
Suppose you ask the agent to extract the tax calculation from format_invoice in pricing.py. A typical trace looks like this:
read pricing.py -> finds format_invoice and the tax line
search "format_invoice" -> finds 3 callers, 2 in tests
read tests/test_pricing.py -> sees which behavior is already tested
run pytest tests/test_pricing.py -> 4 passed (baseline)
edit pricing.py -> adds calculate_tax, calls it
run pytest tests/test_pricing.py -> 4 passedThe run before the edit matters. It gives you a baseline, so a failure afterward points at the change and not at a test that was already broken.
Where the agent gets its feedback
The agent can only correct mistakes that something reports to it. The sources of feedback differ in how reliable they are:
| Feedback source | What it tells the agent | Limit |
|---|---|---|
| Test suite | Whether tested behavior still holds | Silent on untested behavior |
| Type checker | Whether signatures and calls are consistent | Silent on logic errors |
| Linter | Whether code follows style and catches common bugs | Narrow rules only |
| Your review | Whether the change matches your intent | Costs your time |
If a behavior has no check, the agent has no way to know it broke. Most of this tutorial is about adding checks and limits before the agent starts editing.
What this means for how you work
The loop gives you three places to take control:
- Before the loop: give the agent context and a scoped task, so it reads the right files and knows what must not change.
- Inside the loop: keep each edit small and run checks after every step, so a mistake shows up quickly and is easy to undo.
- After the loop: review the diff yourself, because the agent's summary describes what it intended, not necessarily what it changed.
The next chapters cover each of these in turn, starting with the difference between a refactoring and a migration.
Exercise
An agent changes a function, runs the test suite, and reports that all tests pass. Name one reason the change might still have broken behavior, and one step from the loop that would have helped detect it earlier.
Show answer
The changed behavior may not be covered by any test, so passing tests say nothing about it. Running the tests before the edit (a baseline) and adding a test for the untested behavior first would have helped. Reviewing the diff also catches edits to code the tests never exercise.