AGENTS.md and CLAUDE.md: Rules That Make AI Code Better

AGENTS.md is a Markdown file of project rules that AI coding agents read before each task. Claude Code reads CLAUDE.md and can also read AGENTS.md.
Every AI coding session starts with a blank memory. Without a rules file, the agent guesses your build command, invents a new helper instead of using yours, and makes the same mistake you corrected yesterday. The folder this website lives in has a CLAUDE.md of about 50 lines. It's the cheapest change I've made to the quality of AI-written code. This guide covers what these files are, which tools read which file, and a template you can copy.
Key takeaways
- AGENTS.md is an open format read by Codex, Cursor, GitHub Copilot's coding agent, Gemini CLI, Jules, Windsurf, Zed and others. The Agentic AI Foundation under the Linux Foundation now looks after it.
- CLAUDE.md is Claude Code's project memory. Recent versions can read
AGENTS.mddirectly, and@AGENTS.mdinside CLAUDE.md imports it. - Write what the agent can't discover: commands, conventions, boundaries and gotchas. Skip what's obvious from the code.
- Keep it under about 200 lines. Long files cost context and get followed less. Move details into nested files or skills.
- Update it when the agent repeats a mistake. That's the signal a rule is missing.
What is AGENTS.md?
AGENTS.md is plain Markdown at the root of your repository, a README written for agents instead of people. There are no required fields. Its website describes it as a simple, open format for guiding coding agents, and lists tens of thousands of open-source projects that use it. In a monorepo you can add more AGENTS.md files in subfolders. Agents use the nearest one in the directory tree, so apps/api/AGENTS.md can override the root rules for the API.
What is CLAUDE.md, and does Claude Code read AGENTS.md?
CLAUDE.md is the same idea for Claude Code. It loads at the start of every session from a few places:
~/.claude/CLAUDE.md: your personal rules for every project../CLAUDE.mdor./.claude/CLAUDE.md: project rules, committed to git../CLAUDE.local.md: your private project notes (add it to.gitignore).- CLAUDE.md files in subfolders load when Claude works in those folders.
Current Claude Code versions read AGENTS.md on their own when a repo has no CLAUDE.md. If you have both, the simplest way to keep one source of truth is a CLAUDE.md that imports AGENTS.md and adds only Claude-specific notes below it:
@AGENTS.md ## Claude Code - Use the `deploy` skill for production; never ssh in to edit files by hand.
What should you put in AGENTS.md?
Write down what you'd otherwise repeat in chat, and what a new teammate would need on day one. In order of value:
- Commands. Exact install, dev, type-check, test and lint commands, with the package manager. "Use bun, not npm" alone saves many broken lockfiles.
- Layout. Where things live: "API modules are in
src/modules/*", "public pages are in the(landing)route group". - Conventions to reuse. Name the helpers: "fetch data with
getQueryData()inlib/utils.ts". Agents rebuild existing helpers when they don't know they exist. - Boundaries. What the agent must never do or must ask about: pushing to main, editing migrations, touching
.env, running destructive commands. - Gotchas. The thing that cost you an hour: "ESLint is broken repo-wide, don't rely on it", "the dev server needs polling on this machine".
- Definition of done. "Run the type check and tests before saying a task is finished."
What should you leave out?
- Things the code already says. The agent can read
package.jsonand your folder structure. - Long procedures. A 40-step release process belongs in a script or a skill, not in every session's context.
- Vague style advice. "Write clean code" changes nothing. "Server components fetch with
.catch(() => [])so one failed call hides a section" changes behaviour. - Secrets. The file is committed and sent to a model provider. No keys, passwords or customer data.
A template you can copy
# <Project name> One line: what this app is and who uses it. ## Commands - Install: `pnpm install` - Dev: `pnpm dev` (http://localhost:3000) - Check before finishing: `pnpm tsc --noEmit && pnpm test` ## Layout - `src/app/` Next.js routes, `src/lib/` shared helpers - API client: `src/lib/api.ts`; reuse it, don't call fetch directly ## Conventions - Server components by default; "use client" only for interactivity - Validate every request body with Zod - Every query on user data filters by the session user's id ## Never without asking - Push, deploy, run migrations, edit .env*, delete files outside src/ ## Gotchas - Images come from S3; next.config remotePatterns must list the bucket
A real example: this website's rules file
The rules file for this site covers two repos: a Next.js 16 client built with bun and a NestJS API built with pnpm. A few of its lines show what earns a place:
- "This folder is not a git repo. It holds two independent repos." Without it, an agent would run git commands in a folder that has no repo.
- "Every page uses
pageMetadata(). Never set a canonical in a layout." One wrong canonical once pointed several pages at the homepage. The rule stops it coming back. - "Push to main deploys to production. Confirm before pushing." A boundary, stated next to the reason.
- "The prod API is slow from this machine. Retry before debugging." A gotcha that saves the agent from chasing a bug that isn't there.
None of these can be worked out by reading the code, so they're exactly what the file is for.
How do you keep the file useful?
Treat it like code. When the agent makes the same mistake twice, add one line. When a rule stops mattering, delete it. Every few weeks, read the file top to bottom and cut anything stale. Claude Code's /init command can draft a starting CLAUDE.md from your codebase, and you can ask the agent to suggest improvements after a long session.
For rules that must never be broken, don't rely on text. Claude Code hooks can block a command outright, and CI can reject a pull request. A rules file guides the agent; hooks and CI enforce. Security rules belong in both places. My vibe coding security checklist lists the ones worth writing down.
Does this work in Cursor, Codex and Copilot too?
Yes. That's the reason to use AGENTS.md as the shared file. Cursor also has .cursor/rules for file-pattern rules, Codex reads AGENTS.md natively, and Claude Code picks it up directly or through an import. If you're still choosing a tool, see my Claude Code vs Cursor vs Codex comparison.
Frequently asked questions
Should I use AGENTS.md or CLAUDE.md?
If your team uses more than one tool, put the shared rules in AGENTS.md. Add a CLAUDE.md that imports it with @AGENTS.md only if you need Claude-specific instructions.
How long should AGENTS.md be?
Short. Claude Code's docs suggest staying under about 200 lines per file. Most good files I've seen are 30 to 100 lines. Move folder-specific rules into nested files.
Where does AGENTS.md go in a monorepo?
Put shared rules at the root and package-specific rules in each package folder. Agents use the nearest file, so the closest one wins when rules conflict.
Is AGENTS.md sent to the AI provider?
Yes. It becomes part of the prompt, so treat it as public within your vendor agreements. Never put secrets or personal data in it.
Can the AI write its own AGENTS.md?
It can write a good first draft by reading the repo. Edit it yourself afterwards. The most valuable lines are the gotchas and boundaries that only you know.
Want AI agents set up properly on your codebase?
I set up AI coding workflows for teams: rules files, hooks, CI checks and safe deploys, so the agent's output is ready to ship. See my web development services or get in touch.
- AGENTS.md
- CLAUDE.md
- AI coding agents
- Claude Code
- Cursor rules
- vibe coding


