What is AGENTS.md in Codex CLI?
Learn what AGENTS.md does in Codex CLI, how instruction hierarchy works, what to include, and common mistakes. Includes setup, examples, and common fixes.
Short answer
AGENTS.md is a project instruction file for Codex. It tells Codex about repository structure, commands, tests, coding style, commit rules, and collaboration preferences.
It is for stable project rules, not one-time tasks. Put one-time requirements in the current conversation.
What to include
- How to install dependencies, start the project, and run tests
- Common validation commands
- Coding style and naming conventions
- Directories that should not be changed casually
- Required checks before finishing
- Team preferences for communication and output
What not to include
- API Keys, tokens, passwords, or private credentials
- Temporary task instructions
- Long product background stories
- Generic prompts unrelated to the repository
- Outdated commands and paths
Scope and precedence
Codex builds its initial instruction chain when a run starts. The launch directory matters:
- It first reads global guidance from
CODEX_HOME, which defaults to~/.codex: the first non-emptyAGENTS.override.mdor, otherwise,AGENTS.md. - It then checks each directory from the project root, usually the Git root, down to the current working directory. Without a project root, it checks only the current directory for project guidance.
- In each project directory it uses at most one non-empty file:
AGENTS.override.mdfirst, thenAGENTS.md, then names configured inproject_doc_fallback_filenames.
More specific guidance later in the chain takes precedence where instructions conflict; unrelated parent rules still apply. An override replaces the ordinary file at the same directory level, not every instruction from its parents.
A directory example
~/.codex/AGENTS.mdrepo/AGENTS.mdrepo/services/payments/AGENTS.override.mdrepo/services/payments/AGENTS.mdrepo/services/search/AGENTS.mdSuppose global guidance says to use npm test, the repository file requires linting, and the payments override replaces the test command with make test-payments. When you launch from repo/services/payments, Codex loads the global file, the repository file, and the payments override. It keeps the lint rule, uses the payments test command, and skips the ordinary payments file. The sibling search file is outside that startup chain.
Suggested structure
Keep the template below short and replace its commands with commands that actually exist in your repository. A rule should say when it applies and how to check the result.
# Project instructions
## Build and test- Run `npm test` before finishing behavior changes.- Run `npm run lint` after editing TypeScript files.
## Style- Keep diffs small and focused.- Match existing component patterns.
## Safety- Do not edit files outside this repository.- Do not commit secrets.Check missing or conflicting rules
After changing instructions, start a new session in the target directory and ask Codex to list its loaded sources and applicable commands:
codex -C /path/to/repo/services/payments "List the instruction files you loaded and the test command that applies here. Do not edit files."- Check the working directory and
CODEX_HOME. Starting from the repository root does not preload every nested directory’s instructions. - Check the exact filename, non-empty contents, and any
AGENTS.override.mdat the same level. A fallback filename must appear inproject_doc_fallback_filenames. - Inspect
project_doc_max_bytesif long instructions are truncated. Shorten duplicated guidance and keep essential rules near the start. - For a conflict, identify both source files and the directory each rule governs. Put a narrow exception in the relevant directory instead of copying contradictory rules everywhere. Instruction files do not change sandbox permissions.
Common mistakes
- Turning
AGENTS.mdinto a very long universal prompt. - Writing secrets into the file.
- Forgetting to update it when project commands change.
- Mixing
AGENTS.mdwith user-levelconfig.toml. - Assuming rules remove the need to review diffs and tests.
About BetterToken
If a team uses BetterToken to unify model routing, AGENTS.md still matters. The API layer handles model requests and usage records. AGENTS.md handles project context and execution norms.
Use both to reduce configuration confusion and collaboration cost.
Related docs
- What is Codex CLI?
- How to configure Codex CLI config.toml
- What are Codex CLI sandbox and approval modes?
- What is CLAUDE.md in Claude Code?

