Sub topic 7
Project instructions with CLAUDE.md and AGENTS.md
This chapter assumes you can start an agent session in a repository (see the installation chapter) and understand that the agent's context window holds everything it can see at one time. You should also be comfortable with a project's build, test, and lint commands.
What an instruction file does
An instruction file is a Markdown file in your repository that the agent reads at the start of a session. It loads into the context window before your first prompt. The agent then follows it without being told again. This saves you from repeating the same facts in every session: how to run tests, which style rules apply, and which directories are off limits.
Two file names matter here:
CLAUDE.mdis read by Claude Code.AGENTS.mdis an open convention read by Codex CLI and by other agents. Copilot CLI also reads it, but check the current documentation for the exact file names and locations your version supports.
Because the formats are plain Markdown, the content you write is portable. Only the file name differs.
A working example
# 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/`.This file does three useful things. It gives exact commands, so the agent does not guess between npm test, yarn test, or pytest. It states rules that the agent cannot infer from the code. It also marks directories that hold build output, which the agent would otherwise edit by mistake.
How the files are found
Agents generally read the file in the directory where you start the session. Most also read files in parent directories and in subdirectories when they work there. A monorepo can therefore have a short root file for shared rules and a more specific file inside each package. Claude Code also supports a user-level file for personal preferences that apply to every project. Lookup details vary by tool and version, so confirm them in each tool's documentation.
Writing instructions that work
Keep the file short. Every line uses context on every session, and long files make the agent less likely to follow any single rule.
- Put commands first. Use exact strings that you have run yourself.
- Write rules as checkable statements. "Add a test for every behavior change" is better than "write good code".
- Include only what the agent cannot learn by reading the code.
- Remove rules that are out of date. A stale command causes failures that are hard to trace.
Common errors
- Treating the file as enforcement. The file is guidance that the model usually follows. It does not stop a command from running. To block an action, use permissions or a sandbox, covered in later chapters.
- Duplicating files. Keeping both
CLAUDE.mdandAGENTS.mdwith different content leads to drift. Write one file and have the other point to it. Claude Code supports an import line such as@AGENTS.mdinsideCLAUDE.md. A symbolic link also works. - Storing secrets. The file is committed and sent to the model provider. Never put keys or passwords in it.
Exercise
Your team uses Claude Code and Codex CLI. Name two ways to avoid keeping two instruction files that disagree, and explain why an instruction file cannot guarantee the agent never edits dist/.
Show answer
Keep the content in AGENTS.md and either import it from CLAUDE.md with @AGENTS.md or make CLAUDE.md a symbolic link to it. The file is only text in the model's context, so the model may still ignore it. To block edits, use permission rules or sandbox settings, which the tool enforces.