How to write CLAUDE.md and AGENTS.md files that agents follow
Write project instruction files that coding agents actually follow, with a copyable template and one setup for every tool.

Key takeaways
- CLAUDE.md is read by Claude Code; AGENTS.md is the open format read by Codex, Cursor, VS Code, Gemini CLI and others.
- Put exact commands, a short layout, rules with reasons and a definition of done; skip what the agent can read from the code.
- Keep each file short (Anthropic suggests under 200 lines) and make every line specific enough to check.
- Share one file across tools by keeping AGENTS.md as the source and importing it from CLAUDE.md with @AGENTS.md.
CLAUDE.md and AGENTS.md are plain Markdown files in your project that an AI coding agent reads before it starts work. CLAUDE.md is Claude Code's file; AGENTS.md is the open format read by Codex, Cursor, VS Code, Gemini CLI, GitHub Copilot's coding agent and many others. A good one is short, lists the exact commands to build and test, and states the few rules the agent would otherwise get wrong.
Below: where each file goes, what to put in it, a template you can copy, and how to keep one set of instructions working across tools.
What these files do
Every new agent session starts with no memory of your project. Without instructions it guesses: it runs npm test when your project uses pnpm, adds a library you banned last month, or puts files where they don't belong. An instruction file is loaded at the start of each session, so the agent begins with the facts a new teammate would need on day one.
Two points are easy to miss. First, these files are context, not enforcement. Anthropic's memory docs say so directly: to block an action no matter what, use a hook or a permission rule, not a sentence in CLAUDE.md. Second, every line costs context on every turn, so length matters.
Where the files go
Claude Code reads CLAUDE.md from several places and combines them, from the broadest to the most specific:
~/.claude/CLAUDE.md: your personal preferences for every project.CLAUDE.mdor.claude/CLAUDE.mdin the project: shared rules, committed to Git so the whole team gets them.CLAUDE.local.mdin the project: your private notes for this project. Add it to.gitignore..claude/rules/*.md: topic files, which can be limited to certain paths with apathsfield in their frontmatter.
Files in parent folders load at launch. A CLAUDE.md inside a subfolder loads when Claude starts working on files in that folder. Organisations can also deploy a managed CLAUDE.md that applies to everyone on a machine.
AGENTS.md is simpler. According to agents.md, you put one at the root of the repository and, in a monorepo, optionally one per package. The agent reads the nearest file to whatever it is editing, so packages/api/AGENTS.md wins over the root file for API code. Instructions you type in the chat override both.
What to put in it
Write what the agent cannot work out quickly by reading the code, and skip what it can. The AGENTS.md site suggests a project overview, build and test commands, code style, testing instructions and security notes. In practice, the sections that pay off most are:
- Commands. Install, run, build, test, lint. Exact and copy-pasteable.
- Layout. Where the important things live, in a few lines.
- Rules with a reason. "Don't edit
generated/: it is rebuilt from the schema" is followed far more reliably than a bare "don't". - How to check work. What "done" means: tests pass, types check, the page works at phone width.
- Gotchas. The trap every newcomer falls into once.
Make each line testable. Anthropic's examples are a good model: "Use 2-space indentation" instead of "format code properly", and "Run npm test before committing" instead of "test your changes". Their docs suggest keeping each CLAUDE.md under 200 lines, because longer files take more context and are followed less closely.
A template you can copy
# AGENTS.md
## Project
Next.js 15 app with a Postgres database. Customer-facing booking site.
## Commands
- Install: `pnpm install`
- Dev server: `pnpm dev` (http://localhost:3000)
- Tests: `pnpm test` (run before every commit)
- Types and lint: `pnpm typecheck && pnpm lint`
## Layout
- `app/` routes and pages, `lib/` shared code, `db/migrations/` SQL migrations
## Rules
- Never edit `db/migrations/` files that are already merged; add a new migration.
- Keep secrets in `.env.local`; never commit `.env*` files.
- Use the existing `Button` and `Dialog` components instead of new ones.
## Done means
- Tests and typecheck pass, and new pages work at 390 px width.
Claude Code can write a first draft for you: run /init in a session and it studies the codebase and proposes a CLAUDE.md with the commands and conventions it finds. If one exists, it suggests improvements instead. Treat the result as a draft and cut anything generic.
One set of instructions for every tool
If your team uses more than one agent, you do not need two copies. Claude Code reads AGENTS.md as its project instructions when the project has no CLAUDE.md. If you want both, keep the shared content in AGENTS.md and import it from CLAUDE.md, then add anything Claude-specific below:
# CLAUDE.md
@AGENTS.md
## Claude Code only
- Use the `reviewer` subagent before opening a pull request.
Imports work with relative or absolute paths and can nest up to four levels. They help organise a long file, but they don't make it cheaper: the imported text loads in full.
Keep it current
Instruction files rot. A command gets renamed, a rule stops applying, and the agent keeps following the old text. Two habits help:
- Add a line every time you correct the agent twice about the same thing, and delete lines that no longer apply.
- Check for contradictions. If two instructions conflict, the agent may pick either one. Recent Claude Code versions include
/doctor prompt-audit, which looks for outdated or conflicting instructions and proposes edits.

How Ninja Builder uses these files
Ninja Builder follows your CLAUDE.md, and also reads rule files written for other tools: AGENTS.md, .cursorrules, .cursor/rules/*.mdc, .github/copilot-instructions.md, GEMINI.md and .windsurfrules. Project settings › Project › Rules lists each one it found and how it is used. When a project has no AGENTS.md, the builder writes one with your first task, listing only commands that really exist in the project, so you can open it in Codex or Cursor later and carry on. An AGENTS.md you already have is never overwritten.

For personal preferences that follow you across projects, there is also Settings › Memory, where the builder saves corrections you give it and you can edit or delete any entry. Details are in the projects and sessions docs and the memory docs. Ninja Builder is free for 14 days, then $4.99 a month: download it.
Frequently asked questions
Does Claude Code read AGENTS.md?
Yes. Recent versions read AGENTS.md as the project instructions when the project has no CLAUDE.md. If you have both, import AGENTS.md from CLAUDE.md with a line containing @AGENTS.md.
How long should CLAUDE.md be?
Short. Anthropic suggests keeping each CLAUDE.md under 200 lines, because longer files take more context and are followed less reliably. Move rules for one part of the code into path-scoped rule files.
Where do I put AGENTS.md in a monorepo?
Put one at the repository root and add one inside each package that needs its own rules. Agents read the nearest AGENTS.md to the file they are editing.
Can CLAUDE.md stop the agent from doing something?
No. It is guidance, not enforcement. To block an action reliably, use a permission rule or a hook in Claude Code's settings.
Keep reading

How to prompt AI coding agents: examples that work
The four parts of a prompt a coding agent can act on, with before-and-after examples for a bug fix and a feature.

What is MCP? The Model Context Protocol explained for developers
How the Model Context Protocol connects AI agents to your tools, how to add a server, and how to keep it safe.

How to reduce token usage when coding with AI
Where AI coding tokens really go, and seven habits that cut them without slowing you down.