Sub topic 2
What AGENTS.md is
This chapter defines AGENTS.md, explains the problem it solves, and shows how an agent uses it. It assumes you know basic Git and repository layout, are comfortable with Markdown, run build and test commands from a terminal, and have used an AI coding assistant. Later chapters cover what to put in the file, how to write it, and how nested files and precedence work. Here you build the working model.
The problem: an agent starts every session cold
A coding agent is a program that uses a language model to read files, run commands and edit code in your repository. It does not remember your project from one session to the next. At the start of each task it knows only what the model learned in training, plus what it can find in the working directory.
Much of what a new teammate learns in the first week is not in the code itself:
- Which command runs the tests, and whether a single file can be run on its own.
- Which linter and type checker must pass before a change is acceptable.
- Which directories hold business logic and which hold thin handlers.
- Which files must never be edited by hand, such as existing database migrations.
- How pull request titles are formatted.
An agent can guess some of this. It may see package.json and try npm test. But guesses fail in predictable ways. It may run the whole suite when one file would do, skip the linter, or add a default export in a codebase that forbids them. Each wrong guess costs time and leaves review work for you.
What AGENTS.md is
AGENTS.md is a plain Markdown file, usually at the root of a repository, that tells coding agents how to work in that codebase. Think of it as a README written for an agent. A README explains the project to people: what it is, why it exists, how to get started. AGENTS.md holds the operational details an agent needs and that would clutter a README, such as exact commands and rules for code changes.
The name is a convention, not a feature of Git or any language. Agent tools that support the convention look for a file called AGENTS.md and load its text into the model's context before or while they work. The model then treats those instructions as part of its task.
AGENTS.md exists because every tool used to invent its own filename and format. One tool read one file, another tool read a different one, and teams ended up with several copies of the same advice. A shared name lets one file serve many tools. Many agent tools now read AGENTS.md. Some tools still look for their own filename instead, which a later chapter on tool compatibility covers.
How an agent uses the file
The mechanism is simple, and knowing it explains most of the advice in later chapters.
- You start an agent in a repository, or point it at one.
- The tool finds the relevant AGENTS.md file or files.
- The tool puts their text into the model's context, the working memory the model sees on every step.
- The model plans and acts with your instructions in view.
Two consequences follow from step 3.
First, the file is not enforced by software. Nothing parses it, validates it or blocks a command that breaks a rule. It is text the model reads, and models can overlook or misapply text, especially when it is long, vague or contradictory. Hard requirements belong in CI and tests as well. AGENTS.md reduces the number of mistakes you catch there.
Second, the file competes for space. Everything in context takes room that the task, the code and the conversation also need. Short, specific files work better than long ones.
A first example
An AGENTS.md can be a few lines. This one is deliberately tiny.
# AGENTS.md
## Build and test
- Install dependencies: `npm ci`
- Run all tests: `npm test`
- Lint and type-check before finishing: `npm run lint && npm run typecheck`
## Code style
- Use named exports; do not add default exports.Compare an agent with this file to one without it:
| Situation | Without AGENTS.md | With AGENTS.md |
|---|---|---|
| Installing dependencies | May run `npm install`, which can change the lockfile | Runs `npm ci` |
| Checking its work | May stop after editing | Runs lint and type-check |
| Adding a module | Follows whatever pattern it sees first | Uses named exports |
The file doesn't make the agent smarter. It moves knowledge that lived in your head or in a wiki into a place the agent reads automatically.
What the file is not
Several things get confused with AGENTS.md, so it helps to draw the boundaries.
- It is not a README replacement. The README stays the entry point for people. AGENTS.md can link to the README instead of copying it.
- It is not a configuration file. There is no schema and no required fields. The next chapter covers the format.
- It is not a permissions system. Writing "never delete files" does not stop a tool from deleting files. What an agent is allowed to do is controlled by the tool's own settings and your environment.
- It is not a place for secrets. The file is committed to the repository and read by a model, so anything in it can be seen by every contributor and may be sent to a model provider. Security and hygiene gets its own chapter.
- It is not documentation of everything. It should hold what an agent would otherwise get wrong or waste time finding out, not a tour of the whole architecture.
Why a repository file instead of a prompt
You could type these instructions at the start of every session. A file in the repository has advantages that a prompt does not:
- It is shared. Every contributor and every agent tool sees the same guidance, so behavior is consistent across people.
- It is versioned. The file changes in the same commits and pull requests as the code it describes. You can review a change to it, blame a line, or revert it.
- It is close to the code. When a build script changes, the instruction that mentions it sits in the same diff, which makes drift easier to notice.
- It is automatic. Nobody has to remember to paste it.
The cost is that the file can go stale. A command that was renamed in package.json but not in AGENTS.md sends agents down a path that fails. The maintenance chapter covers keeping the two in sync.
An instruction typed into a prompt still matters. When you give a direct instruction in a session, it generally takes priority over the file, because the file describes the default and you are asking for something specific. The precedence chapter covers this.
A first look at scope
Where the file sits decides which work it applies to. A file at the repository root covers the whole repository. A file in a subdirectory is meant for work in that directory tree, which lets a monorepo give each package its own commands and rules.
repo/
├── AGENTS.md # repo-wide rules
└── packages/
└── api/
└── AGENTS.md # rules for work under packages/api/In this layout, an agent editing packages/api/src/handler.ts should be guided by the file in packages/api/, and the root file supplies the shared rules. The placement, precedence and monorepo chapters work through the details and edge cases. For now, keep one idea: the file's location is its scope.
Signs you need one
You probably need an AGENTS.md if you see any of these in agent output:
- The agent runs the wrong test or build command, or invents one.
- Pull requests from agents routinely fail lint or type checks you would have run locally.
- You repeat the same correction in several sessions, such as "don't edit the migration" or "put logic in services".
- Different team members configure their tools differently and get different results.
A good habit is to add a line only after you see a real mistake. A file built from observed failures stays short and relevant. A file written up front from imagination tends to fill with generic advice like "write clean code", which a later chapter shows how to replace with checkable instructions.
Summary of the working model
- Agents begin each session with no memory of your project.
- AGENTS.md is a plain Markdown file whose text tools load into the model's context.
- It carries operational knowledge: commands, conventions, structure and review rules.
- It is advisory text, not enforced configuration, and it uses up context space.
- It lives in the repository, so it is shared, versioned and reviewed like code.
- Its location sets its scope.
Exercise
A teammate says, "I added 'never run the production deploy script' to AGENTS.md, so agents can't deploy now." Is that correct? Explain, and name one other place where you would enforce the rule.
Show answer
No. AGENTS.md is text that the model reads. No software parses it or blocks commands, so the model may still run the script if it overlooks the line or misreads it. The line is still useful as guidance, but real enforcement belongs in the agent tool's permission settings, in access controls on the deploy credentials or environment, and in CI or deployment pipeline approvals.
Exercise
You maintain a repository with no AGENTS.md. In the last week, agents opened three pull requests that failed the linter, and one edited an existing database migration. List two lines you would add to a first AGENTS.md, and say why you would not start by writing a long description of the architecture.
Show answer
Two good lines: "Lint and type-check before finishing: npm run lint && npm run typecheck" and "Never edit an existing migration in migrations/; add a new one." Each addresses a failure you observed and can be checked. A long architecture description adds context length without fixing the observed mistakes, and agents can discover most architecture by reading the code.