Sub topic 7
Project instructions with CLAUDE.md and AGENTS.md
What an instruction file does and what you need first
This chapter assumes you can run your project's tests and build from a shell, you know basic git, and you have read the earlier chapters on the agent loop and the context window. You should also have one of the three tools installed.
A terminal coding agent starts each session knowing nothing about your repository. It can read files, but it does not know which command runs the tests, which directories are generated, or which style rules your team follows. An instruction file is a Markdown file in your repository that the tool loads into the model's context at the start of a session. It gives the agent that knowledge without you retyping it in every prompt.
Because the file is loaded into the context window, it competes with your code and conversation for space. Every line you add is read on every session. This is the main design constraint for the file.
Which file each tool reads
The tools use different file names for the same idea.
| Tool | Primary instruction file | Notes |
|---|---|---|
| Claude Code | `CLAUDE.md` | Also supports user-level and nested files |
| Codex CLI | `AGENTS.md` | An open convention that other agents also read |
| Copilot CLI | `AGENTS.md` and Copilot's own instruction files | Check the current documentation for the exact set of files it loads |
Tool behavior changes between releases, so confirm the file names and lookup rules in each tool's documentation before you depend on them. The ideas below hold across all three.
If you use more than one tool in the same repository, keep one source of truth. A common approach is to write AGENTS.md and make CLAUDE.md a short file that points to it. Claude Code supports importing another file with an @ reference, so CLAUDE.md can contain a single line such as @AGENTS.md. A symbolic link also works on systems that support it. Either way, you maintain the content in one place.
Where files live and how they combine
Instruction files can exist at several levels. The tool gathers the ones that apply and adds them to the context.
- User level: a file in your home configuration directory, such as
~/.claude/CLAUDE.mdfor Claude Code. Use it for personal preferences that apply to every project, like your preferred language for explanations. - Repository root: the file you commit and share with your team. This is the most important one.
- Subdirectories: a file inside
packages/api/applies when the agent works with files in that folder. This keeps rules for one part of a monorepo out of the context when the agent works elsewhere.
When rules from different levels conflict, do not rely on the tool to resolve it the way you want. More specific files are generally meant to take precedence, but a model weighs text, it does not run a strict override. Avoid the conflict by keeping each rule in one place.
What to put in the file
Write the file for a new teammate who will start work today and cannot ask you questions. Three kinds of content earn their place.
- Commands. Exact commands to install, build, test, and lint, including how to run a single test. The agent cannot guess that your project uses
npm ciinstead ofnpm install. - Conventions. Rules that are not obvious from the code: type strictness, naming, how tests are organized, which libraries to prefer.
- Boundaries. Directories or files the agent must not edit, such as build output or generated code.
Here is a compact example for a TypeScript project.
# Project instructions
## Commands
- Install: `npm ci`
- Test: `npm test`
- Lint: `npm run lint`
## Conventions
- TypeScript strict mode; do not use `any`.
- Add or update a test for every behavior change.
- Do not edit files under `dist/` or `generated/`.Each line is specific and checkable. Compare "Write good tests" with "Add or update a test for every behavior change." The second one lets you look at a diff and say whether the agent followed it.
What to leave out
A long file is not a better file. Common mistakes:
- Describing what the code already shows. The agent can read
package.jsonand your source. Do not repeat the directory tree or list every dependency. - Vague advice. Lines like "write clean code" add tokens and change nothing.
- Stale commands. If the test command changes and the file does not, the agent will run the old one and report confusing failures. Treat the file as code that needs review.
- Secrets. The file is committed and sent to a model provider. Never put keys, tokens, or internal URLs you would not share in it.
- Long reference material. Link to a document or put it in a separate file and tell the agent when to read it. For example: "For database migrations, read
docs/migrations.mdfirst."
A useful rule of thumb is to keep the file short enough that you would read it all before starting work. Many teams aim for a few dozen lines.
Instructions versus permissions
This distinction is a frequent source of errors. An instruction says what the agent should do. A permission controls what the tool will let it do. In the next chapters you will configure permissions. For now, compare them side by side.
| Need | Instruction file | Permission or sandbox setting |
|---|---|---|
| Use `npm ci` to install | Yes | Not applicable |
| Follow TypeScript strict mode | Yes | Not applicable |
| Never read `.env` | Weak (advisory) | Yes, a deny rule |
| Never write outside the repository | Weak (advisory) | Yes, a sandbox mode |
Use both together. The file tells the agent the intended path, and the settings stop it from leaving that path.
Creating and maintaining the file
Most tools can draft a file for you. In Claude Code, the /init command scans the repository and writes a first CLAUDE.md. Codex CLI and Copilot CLI have similar helpers in some versions. Treat the output as a draft. Generated files often include facts the agent could discover itself, so trim them.
Then keep the file current with a simple habit: when the agent makes the same mistake twice, add one specific line that would have prevented it. When a line no longer matches the project, delete it. Review changes to the file in pull requests like any other code, because a bad instruction affects every session for everyone on the team.
You can test whether a file works. Start a fresh session and ask a question the file should answer, such as "How do I run the tests in this repo?" If the agent answers with the wrong command, the file is missing, misplaced, or not being loaded. Most tools also have a command that shows what context is loaded, which helps you check.
Common errors
- File in the wrong place. The tool only loads files at the paths it knows. A file named
agents.mdor placed indocs/may not load. Check the exact name and case. - Starting the tool from the wrong directory. Root and nested lookups depend on where you launch the session. Run the tool from the project root.
- Editing the file mid-session and expecting an immediate effect. The file is usually read at session start. Start a new session or use the tool's reload mechanism.
- Conflicting files. If
CLAUDE.mdandAGENTS.mdboth exist with different content, the tools will behave differently from each other. Make one import the other. - Overloading the file. A large file wastes context and dilutes the rules that matter.
Exercise
Your team uses both Claude Code and Codex CLI in one repository. A teammate suggests copying the full instructions into both CLAUDE.md and AGENTS.md. Give two problems with this approach and describe a better setup. Then say why "never read .env" should not rely on the instruction file alone.
Show answer
Two copies drift apart over time, so the two tools end up following different rules, and every change must be made twice. A better setup keeps the real content in AGENTS.md and makes CLAUDE.md a short file that imports it with @AGENTS.md (or a symlink). The "never read .env" rule is advisory text that the model can ignore, especially late in a long session. Enforce it with a deny rule in the tool's permission settings, such as Read(./.env) in Claude Code, or with a sandbox, and keep the instruction as a supporting reminder.
Exercise
Rewrite this vague instruction so you could check it in a code review: "Keep the code tidy and test things."
Show answer
One possible answer: "Run npm run lint before finishing and fix all errors. Add or update a test for every behavior change, and run npm test to confirm it passes." Each sentence names a command or an action that a reviewer can verify from the diff and the test output.