All guides

Make Codex read AGENTS.md by directory

Use global, repository-root and subdirectory AGENTS.md files with the official --cd command to confirm Codex reads rules from outer to inner.

AINavWritten / Updated Sources checked
On this page

At a glance

Before doing any work, Codex reads AGENTS.md files and joins them by directory level into one instruction chain; files closer to the current directory appear later and so take precedence. The task has two parts: build global, repository-root and subdirectory rules, then use the commands from the official documentation to have Codex list what it loaded. It is written for the default Codex home (~/.codex, with CODEX_HOME unset); if you set CODEX_HOME, replace ~/.codex everywhere below with the directory it points to. The directories and rules are fictional.

Workflow

  1. Global layer

    Global Codex home selects only the first nonempty AGENTS.override.md or AGENTS.md, skipping empty files. Each project-path directory selects the first nonempty AGENTS.override.md, AGENTS.md or project_doc_fallback_filenames entry in that order. Fallback names do not belong to global lookup. A selected nonempty override excludes the same-directory AGENTS.md.

  2. Project layer

    From the project root (usually the Git root) down to the current directory, each directory contributes at most one file, checked in the order AGENTS.override.md, AGENTS.md, then fallback names.

  3. Merge and limit

    Files are concatenated from root to current directory and later text overrides earlier text; adding stops once the combined size reaches project_doc_max_bytes (32 KiB by default).

Each stage cites official sources.

Before you start

  • The codex command runs locally, and you have a local repository for experiments (a practice repository is best).
  • First confirm your Codex home: run echo $CODEX_HOME. If the output is empty, use the default ~/.codex; if not, substitute that path wherever ~/.codex appears below.
  • You accept creating or editing AGENTS.md under the Codex home; if a file with that name exists, back it up first.

1. Create the global rules

First make sure the directory exists: mkdir -p ~/.codex (use your CODEX_HOME directory instead if you set one). Then create AGENTS.md in it containing only preferences you want in every repository, such as "run npm test after modifying JavaScript files" and "ask before adding new production dependencies". Keep rules short and specific; for a temporary override use AGENTS.override.md in the same directory and delete it afterwards to restore the base guidance.

2. Write shared rules at the repository root

In the root of your practice repository, create AGENTS.md with conventions common to the project, such as "run npm run lint before opening a pull request" and "update docs/ when behavior changes". This layer is added after the global rules.

3. Add an override in a specific directory

Write an actual project rule in services/reports/AGENTS.override.md, such as using make test-reports instead of npm test. Global Codex home selects only the first nonempty AGENTS.override.md or AGENTS.md, skipping empty files. Each project-path directory selects the first nonempty AGENTS.override.md, AGENTS.md or project_doc_fallback_filenames entry in that order. Fallback names do not belong to global lookup. A selected nonempty override excludes the same-directory AGENTS.md.

4. Confirm loading from a neutral location

From any directory, run the official command: codex --ask-for-approval never "Summarize the current instructions." Expect Codex to quote items from the global AGENTS.md before proposing work. If it does not, check that the file is non-empty and sits in the Codex home you confirmed.

5. Verify the override order from the subdirectory

From the repository root, run: codex --cd services/reports --ask-for-approval never "List the instruction sources you loaded." Expect the report to list the global file first, the root AGENTS.md second and the subdirectory override last. Compare the output line by line with what you wrote and confirm the make test-reports rule comes last.

6. Troubleshoot and clean up

If the rules are wrong, check in turn: an AGENTS.override.md higher in the tree or in the Codex home; an empty file; the project_doc_max_bytes limit; and whether CODEX_HOME points somewhere other than the directory you edited (run echo $CODEX_HOME; empty means ~/.codex). After editing, restart Codex; the documentation says the chain is rebuilt on every run, so there is no cache to clear. When verification is done, delete temporary override files and keep only the rules you really need.

Original example

Fictional practice repository demo-reports, with CODEX_HOME unset. The global ~/.codex/AGENTS.md has one line, "ask before adding new production dependencies"; the root AGENTS.md has "run npm run lint before opening a pull request"; services/reports/AGENTS.override.md has "use make test-reports instead of npm test". From the repository root run codex --cd services/reports --ask-for-approval never "List the instruction sources you loaded." Expected: Codex describes the global file, the root AGENTS.md and the reports override in that order, and treats make test-reports as the test command. If you also placed an AGENTS.md in services/reports, it is not loaded when the nonempty AGENTS.override.md is selected. This is an expected result for the example, not an observed output. Global Codex home selects only the first nonempty AGENTS.override.md or AGENTS.md, skipping empty files. Each project-path directory selects the first nonempty AGENTS.override.md, AGENTS.md or project_doc_fallback_filenames entry in that order. Fallback names do not belong to global lookup. A selected nonempty override excludes the same-directory AGENTS.md.

Completion checks

  • The order of global, root and subdirectory sources in the output matches the description.
  • Global Codex home selects only the first nonempty AGENTS.override.md or AGENTS.md, skipping empty files. Each project-path directory selects the first nonempty AGENTS.override.md, AGENTS.md or project_doc_fallback_filenames entry in that order. Fallback names do not belong to global lookup. A selected nonempty override excludes the same-directory AGENTS.md.
  • No empty file is mistaken for a rule source.
  • Total rule size is well below project_doc_max_bytes, so key rules are not truncated.
  • echo $CODEX_HOME is empty (so ~/.codex is used) or its value matches the directory you actually edited.

Limits

  • Do not put keys, tokens or personal information in these files; they are usually shared with the repository.
  • For GitHub Codex code-review rules, leave formatting and lint checks to CI. Ordinary project instructions may still require running npm run lint.
  • The documentation says Codex stops searching once it reaches your current directory, so place overrides in the directory where the specialized work happens rather than deeper.
  • A file whose name is not a default name or on the project_doc_fallback_filenames list is ignored and should not be expected to load.

Verification scope

Only the official AGENTS.md documentation was read; the example directories, rules and expected results are original. Login, installation, product API execution, file upload and generated output were not tested. Commands are kept as the documentation writes them, and actual behavior depends on your current version.

Article sources

Original editorial content; official descriptions and checking suggestions are distinguished.

  1. Codex · Official sources
    View details