Sub topic 3
Give the Agent the Context It Needs
This chapter assumes you have used a coding agent on a real repository, can read a diff, and can run the project's tests. It covers what to tell the agent before it plans, and what it can find for itself.
Why context decides plan quality
A plan is only as good as the information behind it. An agent that lacks context fills the gaps with guesses. Those guesses show up as assumptions in the plan, or worse, as silent choices you only notice in the diff.
Context falls into two groups:
- Context the agent can discover. Source files, directory layout, existing tests, dependency manifests, and configuration are all in the repository. The agent can read them.
- Context only you have. The real goal, deadlines, team conventions that are not written down, decisions made in meetings, and facts about production are not in the code. The agent cannot find them, so you must state them.
Your job is to supply the second group and point the agent toward the first. Do not paste the whole codebase into the request. Too much text buries the important constraints.
What to state yourself
Include these items in or alongside your planning request.
| Item | Why the agent needs it | Example |
|---|---|---|
| Goal | Defines what "done" means | Reject more than 5 failed logins per IP in 10 minutes |
| Constraints | Rules out plans you would reject | No new dependencies; keep the error format |
| Environment facts | Changes which designs work | We run four server processes behind a load balancer |
| Conventions | Keeps the change consistent | Config values live in `config/settings.yaml` |
| Out of scope | Prevents scope creep | Do not touch the signup endpoint |
| Known history | Avoids repeating failed attempts | An earlier attempt with a global counter caused lockouts |
Environment facts deserve special attention. In the rate limiting example, whether the app runs in one process or several decides whether an in-memory counter is acceptable. If you leave that fact out, the agent will probably assume one process and propose the simplest design. You will then have to correct it during review.
Point to files instead of pasting them
Name the places that matter and let the agent read them. This keeps the request short and the information current.
Relevant code:
- `auth/routes.py` holds the /login handler
- `config/settings.yaml` holds runtime settings
- `tests/test_auth.py` shows how we test auth
Follow the style of the existing handlers.Naming files also protects against a common failure: the agent reads a similar but wrong module and builds the plan on it. A pointer such as "follow the style of the existing handlers" is short, and it gives the agent a concrete pattern to copy.
Use project instruction files for standing context
Some context applies to every task: how to run tests, which linter to use, naming rules, and directories the agent must not edit. Retyping it in each request is tedious and easy to forget. Most coding agents read a project instruction file from the repository, and the filename depends on the tool. Check your agent's documentation for the name and location.
A good instruction file is short and factual:
# Project notes for the agent
- Run tests with `pytest -q` from the repository root.
- Format code with `black`; lint with `ruff check .`.
- Settings live in `config/settings.yaml`. Do not hard-code values.
- Never edit files under `migrations/` without asking.
- We run several server processes, so state must not live in process memory.Keep this file to rules that hold for most tasks. Task-specific details belong in the planning request. If the file grows long or contradicts the code, the agent may follow stale rules, so review it the way you review any other file in the repository.
Let the agent discover the rest
You do not need to describe code the agent can read. Asking it to explore is cheaper and more accurate than summarizing from memory. A good request tells it what to look for:
Before planning, find where failed logins are currently handled,
how errors are returned to the client, and whether any rate limiting
or caching already exists in the project.The next chapters cover read-only exploration in detail. For now, the point is that discovery and context work together. You state the goal and the facts that are not in the code. The agent reads the code and reports what it found.
Separate facts from preferences
State facts as facts and preferences as preferences. The agent treats everything you write as binding unless you mark it otherwise.
- Fact: "We run several processes." The plan must respect it.
- Hard rule: "Do not add dependencies." The plan must respect it.
- Preference: "I would like to avoid a new module if possible." The agent may weigh it against other factors.
- Unknown: "I am not sure whether Redis is available in staging." The agent should flag it instead of assuming.
Marking unknowns is valuable. When you say what you do not know, the agent can list it under risks or ask a question, instead of quietly choosing an answer.
Common context mistakes
- Stating the solution instead of the problem. "Add a dictionary to count attempts" locks in a design before the agent has seen the code. State the goal and constraints, and let the plan propose the design.
- Leaving out the constraint that matters most. The fact you consider obvious is the one most likely to be missing. If you would reject a plan for violating it, write it down.
- Overloading the request. Ten paragraphs of background hide the three lines that matter. Put the goal and constraints first.
- Trusting outdated notes. Old instruction files and comments may describe code that has changed. When the agent's plan conflicts with a note, check the code.
- Assuming the agent remembers. A new session starts without the previous one's discussion. Restate decisions that still apply, or keep them in a file.
A worked comparison
Consider two requests for the same task.
Weak request:
Add rate limiting to login.The agent does not know the limit, the window, whether new dependencies are allowed, or how the app is deployed. Its plan will contain many guesses.
Stronger request:
I want to add rate limiting to the /login endpoint.
Goal: reject more than 5 failed attempts per IP address in 10 minutes.
Constraints: no new dependencies; keep the existing error response format.
Environment: several server processes share one Redis instance.
Code to start from: `auth/routes.py`; settings are in `config/settings.yaml`.
Do not edit any files yet.The second request is only a few lines longer. It removes the three biggest guesses: the numbers, the dependency rule, and the process model.
A short checklist before you send the request
- Did I state the goal in one or two sentences?
- Did I list the hard constraints, including the ones that seem obvious?
- Did I give deployment or environment facts that change the design?
- Did I name the files or areas to start from?
- Did I mark unknowns as unknowns?
- Are standing rules in the instruction file, so I do not repeat them?
Exercise
You ask an agent to "make the export faster." It proposes adding a caching library. The project forbids new dependencies, and the slowness is in a database query. Name two pieces of context you could have supplied up front, and say where each belongs: in the request or in a standing instruction file.
Show answer
First, the constraint "no new dependencies." It applies to every task in the project, so it belongs in the standing instruction file. Second, the goal and known facts for this task, such as "the export endpoint takes 30 seconds, and I suspect the main query," which belong in the request, along with a pointer to the export code. A measurable goal ("under 5 seconds for 100,000 rows") also belongs in the request.
Exercise
Rewrite this request so it separates a hard rule, a preference, and an unknown: "Add pagination to the users API, use cursors if you can, and I think we are on Postgres 12."
Show answer
Hard rule: keep the existing response fields so current clients do not break (state any real rule you have). Preference: "I would prefer cursor-based pagination, but offset-based is acceptable if cursors add much complexity." Unknown: "I am not sure whether we are on Postgres 12. Check the configuration and tell me what you find, and flag anything in the plan that depends on the version."