AGENTS.md Explained: How Codex CLI, OpenCode and Claude Code Use It

18 min read

,
OpenAI Codex CLI running in a macOS terminal window on a purple background, with the OpenAI Codex header, a prompt asking it to explain the code base and an updated plan

Use AGENTS.md to give a coding agent the instructions you would otherwise repeat in chat, such as the test command or which files are generated. Codex CLI, OpenCode and Claude Code can all read it, but they look in different places and load files in different orders.

Codex reads one instruction file per directory and defaults to a 32 KiB project limit. OpenCode can fall back to CLAUDE.md. Claude Code gained direct AGENTS.md support in v2.1.277, with CLAUDE.md or CLAUDE.local.md taking precedence under its default setting.

What AGENTS.md is and what belongs in it

The AGENTS.md project describes the file as a README for agents. A README explains the project to people. AGENTS.md holds the extra context an agent needs to work on the code: how to install dependencies, how to build and test, code style, folder conventions and anything a new teammate would otherwise learn the hard way. The format came out of work by OpenAI Codex, Amp, Google’s Jules, Cursor and Factory, it is now stewarded by the Agentic AI Foundation under the Linux Foundation, and the project says more than 60,000 open-source repositories use it.

AGENTS.md card describing a simple, open format for guiding coding agents adopted by more than 60,000 open-source projects, with logos for Codex, Amp, Jules, Cursor, Factory, VS Code, Devin, Gemini CLI and Copilot
Image: AGENTS.md

There are no required fields or headings. It is ordinary Markdown, and the agent reads whatever text you give it. The sections the project recommends are a short project overview, build and test commands, code style guidelines, testing instructions and security considerations, plus extras such as commit message format, pull request rules and deployment steps.

A good rule of thumb from Anthropic’s Claude Code documentation applies to every tool: write instructions concrete enough to check. “Run npm test before committing” works. “Test your changes” does not. “API handlers live in src/api/handlers/” works. “Keep files organised” does not. Agents follow specific, verifiable lines much more reliably than general advice.

Two things do not belong in the file. The first is secrets: AGENTS.md is meant to be committed, so API keys and passwords would end up in your Git history. The second is hard security rules you expect to be enforced. Anthropic is explicit that instruction files shape behaviour but are not an enforcement layer; blocking commands or paths is a job for each tool’s permission settings.

How Codex CLI reads AGENTS.md

Codex builds an instruction chain once when it starts, which in the terminal interface usually means once per session. It looks in two places.

  • Global scope. In your Codex home folder, which is ~/.codex unless you set CODEX_HOME, Codex reads AGENTS.override.md if it exists and otherwise AGENTS.md. It only uses the first non-empty file at this level.
  • Project scope. Starting at the project root, normally the Git root, Codex walks down folder by folder to your current working directory. In each folder it checks AGENTS.override.md, then AGENTS.md, then any fallback names you configured, and it takes at most one file per folder. If it cannot find a project root, it only checks the current folder.

Codex then joins the files from the root down, separated by blank lines. Files closer to your working directory appear later in the prompt, which gives the more specific guidance precedence in Codex’s documented order. Empty files are skipped, and Codex stops adding files once the combined size reaches project_doc_max_bytes, which is 32 KiB by default. Anything past that point is left out, so a long root file can crowd out the nested file that matters most for the folder you are in.

Codex in a code editor showing a Codex Diff tab with four changed files on the left and the Codex panel on the right listing edited files with Undo and Review buttons
Image: OpenAI

The override file is the Codex feature the other two tools do not share. AGENTS.override.md replaces the normal file in the same folder, which is handy for a temporary global change or a team folder that needs different rules. In OpenAI’s example, services/payments/AGENTS.override.md tells Codex to use make test-payments instead of npm test, and the regular AGENTS.md in that folder is ignored. Delete the override and the normal file applies again.

Two more settings in ~/.codex/config.toml control discovery. project_root_markers decides what counts as a project root; the default is a folder containing .git, and setting it to an empty list makes Codex treat the current folder as the root. project_doc_fallback_filenames adds other file names Codex should accept when a folder has no AGENTS.md. Codex does not read CLAUDE.md unless you add it to that list. If you use Codex code review on GitHub, a ## Code Review Rules section in the nearest AGENTS.md tells it what to flag.

If you run Codex against local models, as in our guide to Codex CLI and OpenCode with Ollama, keep the file especially lean, because every byte of instructions comes out of a smaller context window.

How OpenCode reads AGENTS.md

OpenCode reads a global file at ~/.config/opencode/AGENTS.md, then every AGENTS.md from your current workspace folder upward toward your home folder. For a workspace outside your home folder, it stops at the project root. Unlike Codex, it does not stop at one file per level, its docs describe no size cap, and it does not resolve conflicts. The files are simply combined.

The order is the opposite of what Codex and Claude Code do. OpenCode’s documentation shows a workspace at ~/code/my-project/packages/web loading four files in this order:

~/.config/opencode/AGENTS.md
packages/web/AGENTS.md
packages/AGENTS.md
AGENTS.md

So the global file comes first, then the nearest project file, then its parents, with the repository root last. Since OpenCode does not decide which file wins, avoid writing contradicting rules at different levels. Put a rule once, at the level where it applies.

OpenCode terminal interface working on the request Find the homepage button and make it blue, showing Grep, Glob and Read tool calls followed by a question about which file to modify
Image: OpenCode

OpenCode is the most forgiving of the three when a repository was set up for Claude Code. In any folder without an AGENTS.md, it reads CLAUDE.md instead, and if you have no global ~/.config/opencode/AGENTS.md, it reads ~/.claude/CLAUDE.md. When both files exist in the same folder, AGENTS.md wins, and a symlink between the two is only loaded once. Setting OPENCODE_DISABLE_CLAUDE_CODE_PROMPT=1 turns off every CLAUDE.md, and OPENCODE_DISABLE_CLAUDE_CODE=1 also skips Claude skills under .claude.

Files below your workspace load later, as the agent explores. When OpenCode reads a file or lists a folder, it loads any AGENTS.md between that target and the workspace. Edits behave differently depending on where the file sits. Change the global file or one above your workspace and OpenCode picks up the change before the next model request. Change a nested file that has already loaded and OpenCode does not notice; start a new session for the new text to apply.

OpenCode also lets you pull in extra instruction files from opencode.json or opencode.jsonc with an instructions array, which Step 4 covers. If you already customise OpenCode, our roundup of OpenCode plugins and themes covers the rest of its config.

How Claude Code reads CLAUDE.md and AGENTS.md

Claude Code’s native file is CLAUDE.md, and it can live in four scopes, loaded from broadest to most specific:

  • Managed policy: /Library/Application Support/ClaudeCode/CLAUDE.md on macOS, /etc/claude-code/CLAUDE.md on Linux and WSL, and C:\Program Files\ClaudeCode\CLAUDE.md on Windows, for organisation-wide rules.
  • User: ~/.claude/CLAUDE.md, your personal preferences for every project.
  • Project: ./CLAUDE.md or ./.claude/CLAUDE.md, shared with the team through Git.
  • Local: ./CLAUDE.local.md, your private notes for one project, which you should add to .gitignore.

Claude Code loads these files from your working directory and every folder above it, ordered from the filesystem root down, so the instructions nearest to where you launched it are read last. In each folder, CLAUDE.local.md comes after CLAUDE.md. Files in subfolders load on demand, when Claude reads, writes or edits a file in that subfolder.

Claude Code running in a macOS terminal window titled Test Coverage, with the Claude Code header, the prompt audit and improve test coverage, a Read of package.json and a progress line about identifying the testing framework
Image: Anthropic

AGENTS.md support is newer and built in as a plugin. By default Claude Code reads your AGENTS.md only when there is no CLAUDE.md, .claude/CLAUDE.md or CLAUDE.local.md in your working directory or above it. Your personal ~/.claude/CLAUDE.md, a managed CLAUDE.md and .claude/rules/ files do not count, so they keep loading alongside AGENTS.md. When AGENTS.md is used, an interactive session shows a line such as no CLAUDE.md found; AGENTS.md loaded followed by the path.

That default has a trap. Because CLAUDE.local.md counts, creating one for your own notes in a repository that relies on AGENTS.md quietly stops Claude from reading AGENTS.md. Claude Code also ignores AGENTS.override.md, AGENTS.local.md and anything inside a .agents/ folder, so Codex-style overrides never reach it.

You can change the behaviour by typing /config and setting Project instructions to one of four values:

Project instructions valueWhat Claude Code reads
claude-md-or-agents-md (default)CLAUDE.md files, or AGENTS.md when no CLAUDE.md or CLAUDE.local.md exists in the working directory or above
claude-md-and-agents-mdBoth, with each folder’s CLAUDE.md files first and its AGENTS.md after; an AGENTS.md already imported or symlinked is not read twice
claude-mdCLAUDE.md files only
managed-onlyOnly the organisation’s managed CLAUDE.md and auto memory at launch

If the setting does not appear in /config, your session cannot load AGENTS.md directly. That happens on versions before v2.1.277, if you disabled the built-in plugin with /plugin, and sometimes in the first session after upgrading from an older version. Anthropic also notes that before v2.1.281, some sessions, such as those on Amazon Bedrock or with telemetry disabled, could not load it. The import in Step 3 works in all of these cases. If you run Claude Code on local models as in our Claude Code with Ollama guide, make sure that build is recent enough too.

Codex vs OpenCode vs Claude Code at a glance

Codex CLIOpenCodeClaude Code
Global file~/.codex/AGENTS.md (or AGENTS.override.md)~/.config/opencode/AGENTS.md, else ~/.claude/CLAUDE.md~/.claude/CLAUDE.md
Project filesOne per folder, project root down to working folderEvery AGENTS.md from working folder up to project root or homeCLAUDE.md files; AGENTS.md only if no CLAUDE.md, unless changed in /config
Order in promptGlobal, then root down; nearest file lastGlobal, then nearest file first; root lastRoot down; nearest file last
Nested files below working folderNot loaded at startLoaded when the agent reads that areaLoaded when Claude works on files there
Reads CLAUDE.md?Only if added to fallback file namesYes, where a folder has no AGENTS.mdYes, it is the native file
Override fileAGENTS.override.mdNot in the docsNot read
Size guidance32 KiB combined by default (project_doc_max_bytes)No cap documentedUnder 200 lines per file; files over 4 MiB skipped
Picks up editsNext run or sessionGlobal and parent files live; nested files need a new sessionRead at session start; root CLAUDE.md is re-read from disk after /compact
Comparison of how Codex CLI, OpenCode and Claude Code read AGENTS.md. Codex uses ~/.codex/AGENTS.md or AGENTS.override.md, reads one file per folder from the project root down to the working folder, puts the nearest file last, and does not load nested files below the working folder at start. OpenCode uses ~/.config/opencode/AGENTS.md, or else ~/.claude/CLAUDE.md, reads every AGENTS.md from the working folder up to the project root or home, and puts the nearest file first. Claude Code's native global file is ~/.claude/CLAUDE.md; it reads AGENTS.md only if a folder has no CLAUDE.md, unless you change that in /config, and puts the nearest file last
Image: Techzei

Step 1: Write a root AGENTS.md that works everywhere

Start with one file at the repository root. Most agents can draft it for you. Claude Code’s /init analyses the codebase and writes a starting file, and the AGENTS.md project notes that most agents will scaffold one if you ask. Treat the draft as a starting point and cut anything the agent could work out by reading the code.

This template follows the sections the AGENTS.md project and Anthropic recommend. Replace the example commands with your own:

# AGENTS.md

## Project overview
- Next.js web app with a Node API in `api/` and shared types in `packages/types/`.
- Package manager: pnpm. Do not use npm or yarn.

## Setup and commands
- Install: `pnpm install`
- Dev server: `pnpm dev`
- Tests: `pnpm test` (run before every commit)
- Lint and types: `pnpm lint && pnpm typecheck`

## Code style
- TypeScript strict mode. No `any` without a comment explaining why.
- 2-space indentation, single quotes.
- API handlers live in `api/handlers/`, one file per route.

## Rules
- Never edit files in `generated/`; run `pnpm codegen` instead.
- Ask before adding a new production dependency.
- Do not commit `.env` files or print secrets in logs.

## Pull requests
- Title format: [area] Short description
- Update `CHANGELOG.md` for user-facing changes.

Keep it to facts the agent needs in every session. Multi-step procedures and rules that only matter for one corner of the codebase belong in nested files, path-scoped rules or skills, which load only when needed.

Step 2: Add nested AGENTS.md files for subprojects

In a monorepo, give each package its own AGENTS.md with just the rules that differ from the root. The AGENTS.md project points out that OpenAI’s main repository had 88 of them at the time it was written.

my-project/
├── AGENTS.md              # repository-wide rules
├── api/
│   └── AGENTS.md          # API-only rules
└── packages/
    └── web/
        └── AGENTS.md      # frontend-only rules

All three agents read the root file plus the nested file for the area being worked on, but at slightly different moments. Codex only reaches nested files between the root and the folder you started in, so launch it from the package you are working on, for example with codex --cd packages/web. OpenCode and Claude Code pick up nested files below your start folder as the agent touches files there.

Write nested files as additions, not contradictions. Codex and Claude Code load more local files later. OpenCode lists the nearest project file first. Keep their instructions consistent so the model does not have to reconcile conflicts. A nested file that says “in this package, run pnpm test:web” works in all three. One that says “ignore the root rules” does not.

Diagram of nested AGENTS.md files in a monorepo: a root my-project/AGENTS.md for repository-wide rules, api/AGENTS.md for API-only rules, and packages/web/AGENTS.md for frontend-only rules. Nested files should be additions, not contradictions. Codex and Claude Code put the nearest file last. Launch Codex from the package with codex --cd packages/web. OpenCode puts the nearest file first and leaves conflicts unresolved. OpenCode and Claude Code pick up nested files below the start folder as the agent touches files there
Image: Techzei

Step 3: Make Claude Code read the same file

If your repository has no CLAUDE.md, a current Claude Code build already reads AGENTS.md and you can skip this step. Most teams that have used Claude Code for a while do have a CLAUDE.md, though, and then AGENTS.md is ignored by default. The fix Anthropic recommends is a small CLAUDE.md next to AGENTS.md that imports it:

@AGENTS.md

## Claude Code
Use plan mode for changes under `src/billing/`.

Claude reads the imported file first, then the lines below it, so Claude-only notes go underneath. This keeps working even if a session cannot load AGENTS.md natively, and it never makes Claude read AGENTS.md twice. Imports can be relative or absolute, resolve relative to the file that contains them, and can nest up to four levels.

Claude Code extension in Visual Studio Code with a .claude folder containing agents and settings.local.json in the Explorer, a CSS file open in the editor and a Unit Tests chat where Claude reads and writes a test file
Image: Anthropic

A symlink also works if you do not need Claude-specific lines:

ln -s AGENTS.md CLAUDE.md

Anthropic advises the import instead if anyone on the team uses Windows. Creating symlinks there needs Administrator rights or Developer Mode, and Git checks out a committed symlink as a plain text file unless core.symlinks is enabled, which leaves that person with a one-line CLAUDE.md. OpenCode handles both setups well: with a symlink it loads the content once, and with the import file it reads AGENTS.md because AGENTS.md wins in the same folder.

If you previously told Claude in words to “read AGENTS.md”, replace that sentence with the @AGENTS.md import. A plain sentence only works if Claude decides to open the file. And if you used a SessionStart hook to print AGENTS.md, remove it once Claude reads the file directly, or you get two copies.

Step 4: Add tool-specific settings only where needed

The shared file covers most needs. Each tool has a few extras for cases it cannot handle.

Codex: if your repository already uses another file name, or you want Codex to read CLAUDE.md in folders without AGENTS.md, add the names to the fallback list. You can raise the size limit in the same file:

# ~/.codex/config.toml
project_doc_fallback_filenames = ["CLAUDE.md", "TEAM_GUIDE.md"]
project_doc_max_bytes = 65536

Restart Codex after editing. Folders are then checked in the order AGENTS.override.md, AGENTS.md, then your fallback names, so AGENTS.md still wins where both exist.

OpenCode: the instructions array adds files, globs or URLs on top of AGENTS.md, loaded after it in the order listed. Relative paths are searched from the workspace up to the project root, and URLs are fetched when the config loads with a five-second timeout. Missing files are skipped.

{
  "$schema": "https://opencode.ai/config.json",
  "instructions": [
    "CONTRIBUTING.md",
    "docs/guidelines/*.md"
  ]
}

Claude Code: for rules that apply to one part of the codebase, use .claude/rules/. A rules file with a paths list in its frontmatter only loads when Claude reads, writes or edits a matching file, which saves context in every other session.

---
paths:
  - "src/api/**/*.ts"
---

# API rules
- Every endpoint validates its input.
- Use the standard error response format.

Anthropic also strips block-level HTML comments such as <!-- maintainer notes --> from CLAUDE.md before it reaches the model, so you can leave notes for people without spending tokens. That behaviour is documented for Claude Code only, so do not rely on it in a file Codex and OpenCode also read.

Step 5: Check what each agent actually loaded

Never assume an instruction file loaded. Each tool gives you a way to check.

  • Codex: OpenAI’s suggested test is to ask for a summary without approvals, for example codex --ask-for-approval never "Summarize the current instructions.". Codex should quote your global and project files in order. From a subfolder, add --cd with that folder. For a full audit, start Codex with codex -c log_dir=./.codex-log and read ./.codex-log/codex-tui.log.
  • OpenCode: the instructions page does not document a list command, so ask the agent what its project instructions say. Remember that nested files only load once the agent reads that area.
  • Claude Code: run /memory to see the instruction files in play, or /context all and look under Memory files. On versions before v2.1.280, an AGENTS.md read directly did not show in those lists. /doctor prompt-audit, on v2.1.283 or later, checks your CLAUDE.md, CLAUDE.local.md and AGENTS.md files for outdated or conflicting instructions and proposes edits without changing anything.

If you edited a file and the agent still follows the old version, start a new session. Codex rebuilds its chain on every run, Claude Code reads files at session start, and OpenCode only refreshes global and parent files live.

How long should AGENTS.md be?

Shorter than you think. Every line is sent to the model at the start of the session, so a long file costs context and money on every task, and Anthropic says longer files reduce how well instructions are followed. Its target for each CLAUDE.md is under 200 lines. Claude Code warns at startup and in /status when a file passes the recommended length, or when several files add up past a combined limit, and it skips any file over 4 MiB.

Claude Code status line showing the model name Opus 5.5 and a context window usage bar at 42 percent
Image: Anthropic

Codex works with a byte limit instead. Once the combined files reach 32 KiB, it stops adding more. OpenAI suggests raising the limit or splitting instructions into nested folders when you hit it. Claude Code has a similar cut for files that load on demand: a subfolder CLAUDE.md or on-demand rules file over 40,000 characters on a 200K context window is truncated, and Claude only sees the start plus a note telling it to read the rest.

Imports do not save space. A CLAUDE.md that imports five files loads all five at once. To actually reduce the cost, move rules into nested files or path-scoped rules that load only when relevant, and delete anything the agent can learn by reading the code, such as directory listings and dependency lists. Anthropic’s /doctor checkup proposes exactly those cuts for a committed CLAUDE.md.

Troubleshooting

SymptomLikely causeFix
Claude Code ignores AGENTS.mdA CLAUDE.md, .claude/CLAUDE.md or CLAUDE.local.md exists in this folder or aboveAdd @AGENTS.md to that CLAUDE.md, or set Project instructions to claude-md-and-agents-md
Project instructions missing from /configClaude Code older than v2.1.277, plugin disabled, or first session after upgradeUpdate, re-enable the plugin, or use the import
Codex follows the wrong rulesAn AGENTS.override.md higher up or in ~/.codexRename or delete the override
Codex misses the end of your rulesCombined files passed project_doc_max_bytesTrim, split into nested files, or raise the limit
Codex finds nothingEmpty file, wrong folder, or CODEX_HOME points elsewhereCheck the folder and run echo $CODEX_HOME
OpenCode loads only the global fileWorkspace is outside the project root, or OPENCODE_DISABLE_PROJECT_CONFIG=1 is setOpen the project folder or unset the variable
OpenCode reads an old nested fileNested files are not re-read after loadingStart a new session
Rule lost after compaction in Claude CodeIt lived only in chat, or in a nested file not yet reloadedMove it into the root CLAUDE.md or AGENTS.md

My take

If you use more than one coding agent, make AGENTS.md the single source of truth and treat everything else as a thin adapter. One root file, a few nested files in a monorepo, and a two-line CLAUDE.md that imports AGENTS.md covers Codex, OpenCode and Claude Code without duplicated text that drifts apart. It also covers the long list of other tools on agents.md, from Cursor and GitHub Copilot’s coding agent to Gemini CLI, which can be pointed at the file through its settings.

The setting most worth changing is the habit of writing too much. A 40-line file with real commands and three or four hard rules will do more than a 400-line essay about your architecture. Keep Codex overrides for genuine one-off cases, avoid CLAUDE.local.md in AGENTS.md-only repositories unless you switch Claude to read both, and check what loaded the first time you set it up.

FAQ

Is AGENTS.md the same as CLAUDE.md?

They do the same job in the same Markdown format. AGENTS.md is the shared open format read by Codex, OpenCode and many others. CLAUDE.md is Claude Code’s native file, and Claude Code reads AGENTS.md only when no CLAUDE.md is present, unless you import it or change the setting.

Where should I put the global file for each tool?

Codex uses ~/.codex/AGENTS.md, OpenCode uses ~/.config/opencode/AGENTS.md and falls back to ~/.claude/CLAUDE.md, and Claude Code uses ~/.claude/CLAUDE.md. Keep personal preferences there and project rules in the repository.

Should I commit AGENTS.md to Git?

Yes. Committing it means everyone working on the repository, and every agent they use, gets the same guidance. Keep personal notes in your global file or in a gitignored local file instead, keeping in mind that CLAUDE.local.md stops Claude Code reading AGENTS.md by default.

What happens if two AGENTS.md files disagree?

Load order differs by tool. Avoid contradictory rules and use the checks above to see which files were loaded. The AGENTS.md project adds that an explicit instruction in chat overrides the files. The safest approach is to avoid contradictions and state each rule at one level only.

Does AGENTS.md work with local models?

Yes. The file is added to the prompt no matter which model runs behind the agent. With local models the context window is usually smaller, so a short file matters even more.

About the author