Sub topic 3
Give the agent the context it needs
A plan is only as good as the information behind it. This chapter covers what context to give a coding agent before you ask for a plan, and what the agent can find on its own.
This chapter assumes you have used a coding agent on a repository, can read a diff, and can run the project's tests.
Why context matters
An agent starts each session knowing nothing about your project's history. It can read files, but it cannot read your mind. If you leave out a constraint, the agent fills the gap with a guess. The plan will then look reasonable and still be wrong for your project.
Context falls into two groups:
- Context the agent can discover. File layout, existing functions, test setup, and naming conventions are all in the repository.
- Context only you have. The reason for the change, limits on the solution, decisions made in meetings, and facts about production are not in the code.
Spend your effort on the second group. Let the agent read the first.
What to tell the agent
Four kinds of information have the most effect on a plan.
| Context | Example | What goes wrong without it |
|---|---|---|
| Goal | Reject more than 5 failed logins per IP in 10 minutes | The agent solves a nearby problem |
| Constraints | No new dependencies; keep the error format | The plan adds a library or changes responses |
| Environment | Several server processes behind a load balancer | The plan assumes one process |
| Existing conventions | Settings live in `config/app.yaml` | Values are hard-coded |
State the goal as an outcome you can check, not as a solution. "Reject more than 5 failed attempts per IP in 10 minutes" is checkable. "Add rate limiting" is vague, and the agent must decide what it means.
Point at the code that matters
You do not need to paste code into the request. Name the files, functions, or directories where the work belongs. This saves the agent from searching and reduces the chance it picks the wrong module.
Add rate limiting to the /login endpoint.
Start with `auth/routes.py`. Settings live in `config/app.yaml`.
Tests for this area are in `tests/auth/`. Follow the style of the
existing tests.If a similar feature already exists, say so. "The password reset endpoint already throttles requests in auth/reset.py" gives the agent a pattern to follow. It also tells the agent which approach to avoid duplicating.
Share what is not in the repository
Some facts never appear in the code. Write them down in the request:
- Deployment shape, such as the number of processes or whether a shared cache exists.
- Decisions already made, such as "we chose Redis for shared state".
- Things that must not change, such as a public API response or a database schema.
- Deadlines or scope limits, such as "this is a hotfix; keep it small".
Keep it short and relevant
More context is not always better. A long request that mixes unrelated details makes the important constraints harder to see. Include a fact only if it would change the plan. If removing a sentence would not alter what the agent proposes, remove it.
For information you repeat in every session, such as test commands or coding conventions, use the project's persistent instructions file if your agent supports one. That keeps each request focused on the task.
How context flows into the plan
If the plan looks wrong at review, check which input was missing. A wrong assumption about the environment usually means you left something out. A wrong file choice usually means the agent needed a pointer.
Exercise
You ask an agent to "add caching to the product list." The plan uses an in-memory dictionary, but your app runs on four server processes. Which kind of context was missing, and how would you fix the request?
Show answer
The environment was missing. An in-memory dictionary is not shared across processes, so each process would hold its own copy. Add the deployment fact to the request, for example "the app runs on four processes and a shared Redis client already exists in core/redis.py", and state the goal as a checkable outcome, such as "product list responses are served from a cache shared by all processes".