Sub topic 3
Giving the agent the context it needs
This chapter assumes you can read and write code in one language, use Git branches and commits, and run a test suite and linter from the command line. You should also have used an AI coding assistant before.
Why context decides the result
A coding agent starts each session knowing nothing about your project except what it can read or what you tell it. It can open files, search the code, and run commands. It cannot see your team's habits, your reasons for past decisions, or the parts of the system that are not in the files it opened.
When context is missing, the agent fills the gaps with guesses. In a refactoring, a guess often looks fine and breaks a convention or a hidden dependency. Good context turns those guesses into facts.
Three kinds of context
| Kind | What it answers | Example |
|---|---|---|
| Code context | Which files and functions are involved? | `pricing.py`, `tests/test_pricing.py` |
| Convention context | How does this project do things? | Naming rules, formatter, test layout |
| Intent context | Why is the change needed, and what must stay the same? | "Public API of `format_invoice` is frozen" |
The agent can discover most code context by itself. Convention and intent context usually exist only in people's heads, so you have to supply them.
Let the agent read, but point it
The agent can search a repository, but a search without a starting point wastes effort and can land on the wrong code. Name the entry points yourself. Give file paths, function names, and the test command.
Context:
- The function to change is format_invoice in pricing.py.
- Tests live in tests/ and run with `pytest`.
- We format with `black` and lint with `ruff check .`.
- Callers of format_invoice are in billing/report.py and billing/export.py.Listing callers matters. If you leave them out, the agent may change a function without checking who depends on it.
Write conventions down once
Retyping the same rules in every prompt is slow and easy to get wrong. Most agents read a project instructions file at the start of a session. The file name depends on the tool, so check your tool's documentation. A typical file holds the facts the agent cannot infer:
# Project notes for coding agents
## Commands
- Run tests: pytest
- Lint: ruff check .
- Format: black .
## Conventions
- Python 3.12, type hints required on public functions.
- Money values are rounded with round(x, 2) at the point of calculation.
- Do not edit files under legacy/ without asking.
## Boundaries
- Public function signatures in pricing.py are used by other teams.Keep this file short and accurate. A stale instruction is worse than none, because the agent will follow it.
State what must not change
For refactoring, the most valuable intent context is the list of things that stay fixed. A refactoring changes structure, not behavior, and the agent only knows that boundary if you say so. Typical statements include:
- Public signatures and return values stay the same.
- Output formats, error messages, and log lines stay the same.
- Only the named files may be edited.
Also state what is deliberately odd. If a function rounds in an unusual way because a customer contract requires it, say so. Otherwise the agent may "fix" it.
Check what the agent understood
Before any edits, ask the agent to restate the task and list the files it plans to read or change. A wrong file in that list shows a context gap while it is still cheap to fix. Chapter material on planning builds on this habit.
Common errors
- Assuming the agent knows your conventions. It follows common defaults unless told otherwise.
- Omitting the test command. The agent then cannot verify its own work, or it picks the wrong command.
- Leaving out callers and consumers. This leads to signature changes that break code the agent never opened.
- Letting the instructions file go stale. Review it when tooling or rules change.
Exercise
You ask an agent to extract a helper from a function in pricing.py. It renames a parameter, and a script in another directory that calls the function with a keyword argument now fails. Which two pieces of context, missing from your prompt, would most likely have prevented this?
Show answer
First, the intent context that the function's public signature must not change. Second, the code context naming the callers, including the script in the other directory, so the agent would have checked them. A test command that exercises those callers would also have caught the break.