Sub topic 1
Why Coding Agents Need Project Context
A coding agent can write correct code and still fail in your repository. It may run the wrong test command, put a file in the wrong directory, or edit a file that is generated. This chapter explains why that happens and what kind of information prevents it. Later chapters cover the file format that supplies that information.
Prerequisites
This chapter assumes you have:
- Basic Git and repository layout knowledge: you know what a repository root, a branch, and a pull request are.
- Comfort with Markdown.
- Experience with command-line build and test workflows, such as
npm testormake build. - Some experience using an AI coding assistant or agent.
What a coding agent does
A coding assistant that only suggests completions in your editor reacts to the text near your cursor. A coding agent does more. It takes a task in plain language, such as "add a rate limit to the login endpoint", and carries it out in several steps:
- Read files to understand the code.
- Edit or create files.
- Run commands such as builds, tests, and linters.
- Read the output and decide what to do next.
- Stop and report, or open a pull request.
The agent repeats steps 1 to 4 until it believes the task is done. Each loop depends on knowing what to read, what to run, and what "done" means in this repository.
What the agent can and cannot see
The working model has three parts.
The model is general. It has learned from a very large amount of public code. It knows common frameworks and conventions, but it knows nothing about your repository until it looks.
The agent sees a limited window. The text the model can consider at one time is its context window. It holds your request, the files the agent has read, command output, and any instructions. It has a size limit. A large repository cannot fit, so the agent must choose what to read.
Each session starts fresh. In most tools, the agent does not remember earlier sessions. If it learned yesterday that tests need a running database, it does not know that today unless the information is written somewhere it will read.
So the agent works by discovery. It lists directories, opens a package.json, searches for test files, and infers the rest. Discovery is good at facts that the code states directly. It is poor at facts that the code only implies.
Knowledge that lives outside the code
Much of what a new teammate learns in the first week is not obvious from reading source files. The same gaps affect an agent.
| Kind of knowledge | Example | Can the agent discover it reliably? |
|---|---|---|
| Exact commands | Tests run with `npm test -- path/to/file.test.ts` | Sometimes. Scripts in `package.json` help, but there may be several candidates. |
| Environment setup | The database must be started with `docker compose up -d db` first | Rarely. A failing test shows the symptom, not the cause. |
| Conventions | Named exports only, no default exports | Partly. It can copy nearby code, but nearby code may be inconsistent. |
| Boundaries | Never edit existing migrations | No. Nothing in the file shows the rule. |
| Generated files | `src/api/client.ts` is generated from a schema | Sometimes, if a header comment says so. |
| Team process | Pull request titles use `[area] Short description` | No. It lives in team habit. |
The rows near the bottom are the dangerous ones. The agent cannot find them by reading code, and breaking them often produces no error at all.
What goes wrong without context
These failures are common. They give the agent's work a recognizable pattern: it looks plausible, but it does not fit the repository.
Wrong commands
A repository might use pnpm while the agent defaults to npm. It might use pytest -x with a specific marker, or make test instead of calling the framework directly. The agent runs a command, it fails or runs the wrong thing, and the agent spends several loop iterations recovering.
A worse case is a command that succeeds but checks less than you expected. If the agent runs only the unit tests, it reports "tests pass" while integration tests would have failed.
Skipped verification
If the agent does not know that lint and type checks are required before a pull request, it will usually stop once the code seems to work. Your CI then fails on the pull request, and a human has to send the work back.
Edits to the wrong places
Without a map of the directory layout, the agent places business logic in a route handler because that is where the nearest example was. Or it edits a generated file, and the change is lost the next time the generator runs.
Violated rules that leave no trace
Some rules protect data or history. Editing an applied database migration is the classic case. The code still compiles and the tests may still pass, but you have broken deployments. The agent cannot discover this rule from the code alone.
Style drift
Even when the agent copies local style, it picks one example. If the codebase is in the middle of a migration, say from class components to function components, the agent may copy the older pattern.
Wasted effort
Every command the agent guesses wrong costs time and tokens. It also fills the context window with noisy error output. That leaves less room for the files that matter.
An example in practice
Suppose the repository has this situation: a Node project that needs a running Postgres container for tests, and a lint step that CI enforces.
Without instructions, a typical session looks like this:
Task: Add a validation check for empty invoice numbers.
Agent: reads src/routes/invoices.ts
Agent: edits src/routes/invoices.ts (adds the validation inline)
Agent: runs `npm test`
Output: ECONNREFUSED 127.0.0.1:5432 (database is not running)
Agent: concludes the failure is unrelated, reports "done"
CI: fails on lint and on missing testsEach step was reasonable on its own. The agent had no way to know that logic belongs in src/services/, that the database must be started first, or that lint is mandatory.
With a few lines of project context, the same session changes:
Context says: start the database with `docker compose up -d db`,
keep handlers thin, run `npm run lint && npm test` before finishing.
Agent: starts the database
Agent: puts logic in src/services/invoices.ts, adds a unit test
Agent: runs `npm run lint && npm test`
Output: all checks pass
Agent: reports "done" with the commands it ranThe model did not get smarter. It got the facts it could not find.
Where context can come from
An agent can pick up project context from several places. They differ in how well they suit the job.
- The code itself. Always read, but it states facts, not intentions.
- README. Written for humans. It often covers the project's purpose and quick start. It tends to be long, and it may be out of date.
- CONTRIBUTING.md. Describes process for human contributors. Useful, but not shaped for an agent that needs exact commands.
- CI configuration. The CI file is the most reliable record of which commands must pass. It is also verbose, and it describes what runs, not what a developer should run locally.
- Your prompt. You can paste instructions into each request. That works for one-off details but does not scale. You will forget things, and teammates will not repeat them.
- A dedicated agent instruction file. A file kept in the repository that is written for agents, versioned with the code, and loaded automatically by the tool.
The last option is the subject of this tutorial. The convention for it is a Markdown file named AGENTS.md. Because it lives in the repository, every teammate and every agent gets the same instructions, and changes to it go through code review like any other change.
Why a shared file instead of per-tool setup
Different agent tools exist, and each could define its own configuration format. That would force a team to maintain several files with the same content. AGENTS.md is an attempt to settle on one plain filename that many tools read. Chapter "What AGENTS.md is" covers its origin and how tools treat it. For now, the key point is that the information is the same whichever agent reads it.
Limits of project context
Instructions help, but they do not solve everything.
- They compete for space. Everything loaded from an instruction file uses part of the context window. A long file leaves less room for code and can dilute the rules that matter.
- They are advice, not enforcement. An agent may still ignore or misread an instruction. If a rule must hold, such as "never commit secrets", back it with a tool like a pre-commit hook or CI check.
- They go stale. A file that names a removed script sends the agent down a wrong path with full confidence.
- They cannot replace review. Context lowers the rate of avoidable mistakes. A human still reviews the changes.
Remember these limits as you read the following chapters. Most good advice about writing instruction files, such as being concrete, short, and verifiable, follows from them.
Summary of the working model
- An agent loops: read, edit, run, check.
- It discovers facts by reading, and it starts each session with no memory of earlier ones.
- Facts about commands, setup, boundaries, and process are hard or impossible to discover from code.
- Missing facts cause wrong commands, skipped checks, misplaced edits, and silently broken rules.
- A versioned instruction file in the repository supplies those facts to every agent and every teammate.
Exercise
An agent edits a file that is generated from a schema. The code compiles and tests pass, but the next build overwrites the change. Why could the agent not have avoided this by reading the code, and what kind of context would have prevented it?
Show answer
Nothing in the generated file necessarily marks it as generated, and the rule "do not edit this file, change the schema instead" is a boundary that exists in team knowledge, not in the code's behavior. A short instruction naming the generated path and where to make the real change (for example, "src/api/client.ts is generated from schema.yaml; edit the schema and run the generator") would have prevented it.
Exercise
A teammate says, "The agent is smart enough, so it will figure out how to run our tests." Give two reasons this can fail even for a capable model.
Show answer
First, the model has no memory of your repository between sessions and can only discover facts by reading files, so setup steps such as starting a database are often invisible until a test fails with an unclear error. Second, several plausible commands may exist (for example, a unit-only script and a full test script), and the agent can choose one that passes while checking less than the team requires. Any answer that names discovery limits and ambiguity of commands is acceptable.
Exercise
Why is "put the instructions in each prompt" a weaker approach than a file in the repository?
Show answer
Prompt instructions depend on each person remembering and retyping them, so they vary between teammates and are not versioned or reviewed. A repository file is shared, loaded automatically, kept in sync with the code through normal pull requests, and the same for every agent session.