Log in

Codex field guide · September 17, 2026

AGENTS.md for Codex: Rules That Survive the Monorepo

Build a short repository map, add package-local deltas, stay clear of the size ceiling, and prove which rules Codex loaded before trusting the run.

TM
Tony MartinsSeptember 17, 2026
13 min read
Resolve the chain
codex://instruction-chainresolved
01
~/.codex/AGENTS.mdglobal
02
repo/AGENTS.mdshared
03
services/AGENTS.mdscoped
04
payments/AGENTS.override.mdwins
root → current working directory32 KiB default ceiling

Direct answer

Write a router. Load a delta. Verify the result.

For Codex, use AGENTS.md as a small instruction router—not a repository encyclopedia. Keep universal commands, hard boundaries, and source-of-truth links at the root. Put only real package differences in nested files.

Codex builds project guidance from the repository root to the current working directory, prefersAGENTS.override.md within each directory, and stops at a combined 32 KiB by default. Start in the directory whose rules you need, ask Codex to summarize the active chain, and restart after edits. For guided creation and audits, see Supercode Specs & Docs, available after you install Supercode for Codex.

01

Native resolution

How Codex resolves AGENTS.md

Codex builds the instruction chain once when a run starts. A launched TUI session normally counts as one run, so the working directory and the files present at startup are part of the result.

01Global
$CODEX_HOME

Override first, otherwise AGENTS.md. Only the first non-empty choice is used.

02Project root
repo/AGENTS.md

Broad repository map, shared commands, boundaries, and completion rules.

03Nested scope
root → CWD

One instruction file per directory, concatenated from broad to specific.

04Task
your prompt

Direct system, developer, and user instructions still outrank repository guidance.

Per directoryAGENTS.override.mdAGENTS.mdconfigured fallbacks

Codex loads at most one file from each directory. A same-directory override does not merge with the base file; it replaces that choice in the chain.

Project files are concatenated from root downward. A deeper rule appears later and wins when it conflicts with a broader one. Empty files are skipped, and project guidance stops when the combined size reaches project_doc_max_bytes—32 KiB by default.

A file elsewhere in the repository is not proof that the current run loaded it. In current Codex discovery, the active path runs from the project root to the startup working directory.

02

Information architecture

Use the root file as a map, not a manual

OpenAI's agent-first engineering team reports that one giant AGENTS.md consumed scarce context, made every rule look equally important, became stale, and was difficult to verify. Its replacement is a short entry point—roughly 100 lines in that repository—that routes the agent into structured, versioned documentation.

The exact line count is not a universal target. The useful separation is: the instruction file routes, repository documents explain, and executable tools prove. If a linter already enforces an import boundary, name its command and link the architecture rationale instead of duplicating the configuration in prose.

AGENTS.mdWhere to look · what to run · where to stop
docs/ scripts CI gates
03

Content routing

What belongs in the instruction file—and what does not

Choose the destination by function. Guidance belongs in the active chain only when Codex needs it to select the next action, boundary, authority, or proof.

Put in AGENTS.md

  • Repository map and package boundaries
  • Exact setup and validation commands
  • Files or systems the agent must not change
  • Required evidence before completion
  • Conditions that require asking first

Link to project truth

  • Architecture and domain explanations
  • Specifications, ADRs, and runbooks
  • Long API and schema references
  • Product terminology and rationale
  • Ownership and operational history

Enforce in tooling

  • Formatting and lint rules
  • Import and dependency boundaries
  • Generated-file drift
  • Types, tests, and coverage gates
  • Secret scanning and policy checks

Avoid payload that adds words without resolution

Full copies of READMEs, formatter rules, generated inventories, secrets, historical narrative, duplicated parent guidance, and aspirations such as “write clean code” all consume attention while leaving the decision ambiguous.

Prefer an observable rule: “Do not import from infrastructure/ into domain/; run npm run check:boundaries after moving modules.”

04

Copyable starting point

A concise root instructions template

The root should remain true for almost every package. Replace every example command with one that actually exists; a false command turns each task into a setup investigation.

Treat the sections below as responsibilities rather than a mandatory schema. Standard Markdown is enough. Keep the repository map stable, point to changing detail, and make the completion contract specific enough that a reviewer can distinguish a finished task from an unverified edit.

# Repository guide

## Repository map
- `apps/` contains deployable applications.
- `packages/` contains shared libraries.
- `docs/` is the source of truth for architecture and decisions.

## Read before editing
- Architecture: `docs/ARCHITECTURE.md`
- Current plans: `docs/plans/`
- Read the nearest nested `AGENTS.md`.

## Authoritative commands
- Install: `npm ci`
- Focused validation: run the changed package's test script.
- Full gate: `npm run check`

## Boundaries
- Do not edit generated files; update their source.
- Ask before adding a production dependency or changing a public API.
- Never commit credentials, local env files, or build output.

## Completion contract
- Add or update tests for changed behavior.
- Run focused checks, then the full gate once.
- Report changed files, validation, and unresolved risks.
05

Local precision

Write nested files as deltas

# Payments service delta

## Local scope
- Applies to `services/payments/`.
- Read `docs/payments/ledger-invariants.md` first.

## Different commands
- Focused tests: `make test-payments`
- Contract tests: `make test-payment-contracts`

## Local boundaries
- Never change ledger history in place.
- Ask before changing an external event schema.

## Completion evidence
- Name the invariant or contract exercised by each changed test.

Create a nested file when a subtree has a real difference: another package manager, stricter boundaries, different checks, a specialized runtime, or a separate source of truth. Everything else remains inherited.

Use AGENTS.override.md for an intentional replacement, not as a casual second file. An old override is a common reason edits to the same-directory AGENTS.md appear ignored.

06

Worked example

Two working directories, two resolved chains

repo/
  • AGENTS.mdshared
  • docs/ARCHITECTURE.mdtruth
  • apps/web/AGENTS.mdweb delta
  • services/AGENTS.mdservice delta
  • services/payments/AGENTS.override.mdlocal winner
codex --cd apps/webglobal → root → web

No ledger commands enter the startup chain.

codex --cd services/paymentsglobal → root → services → payments override

Frontend conventions stay out of the payments task.

If one task genuinely spans peer packages, start at their nearest shared ancestor and link each package's authoritative documents. Do not paste every local manual into the root merely to make one broad session possible.

07

Preflight

Audit the chain before starting Codex

Locate candidates
find . -type f \
+  \( -name 'AGENTS.md' -o -name 'AGENTS.override.md' \) \
+  -print
Measure the intended path
wc -c AGENTS.md \
+  services/AGENTS.md \
+  services/payments/AGENTS.override.md

The byte total is a preflight estimate; global guidance and configuration still matter. Use it to catch accidental growth, not to optimize toward the 32 KiB ceiling.

  1. 01

    Does every command exist and run from the directory implied by the rule?

  2. 02

    Does a deeper file replace a broad rule explicitly, or merely contradict it?

  3. 03

    Is any paragraph duplicated from an inherited file?

  4. 04

    Could a source document or executable check replace this prose?

Alternate filenames require configuration
# ~/.codex/config.toml
project_doc_fallback_filenames = ["TEAM_GUIDE.md", ".agents.md"]

Raising the byte ceiling can prevent truncation; it cannot turn bloated guidance into useful guidance.

08

Proof, not presence

Verify what Codex actually loaded

Repository root
codex --ask-for-approval never
"Summarize the current instructions."
Nested package
codex --cd services/payments
--ask-for-approval never
"Show which instruction files are active."

Check the answer for shared rules, the expected local delta, and the absence of unrelated package guidance. When the summary remains ambiguous, enable a plaintext log withcodex -c log_dir=./.codex-log or inspect the latest session JSONL when session logging is enabled. Keep logs out of version control.

Then test one small representative task. Confirm that Codex chooses the documented command, respects the boundary, and reports the required evidence. A file can load correctly and still contain weak instructions.

09

Failure isolation

When Codex appears to ignore repository guidance

Eliminate structural causes before adding prose. More text cannot repair a file that was shadowed, truncated, outside the startup path, or stale for the active run.

  1. 01
    Confirm the root and CWD

    The expected nested file must be on the active root-to-CWD path.

  2. 02
    Look for an override

    A same-directory AGENTS.override.md wins over AGENTS.md.

  3. 03
    Check the filename

    Custom names are ignored unless configured as fallbacks.

  4. 04
    Check content and size

    Empty files are skipped; the combined chain can hit its byte ceiling.

  5. 05
    Restart the run

    The current session does not continuously rebuild instructions.

  6. 06
    Ask for a summary

    Separate a discovery failure from vague or conflicting guidance.

  7. 07
    Test one behavior

    Use a command, boundary, example, and proof—not a general aspiration.

If discovery succeeds but behavior remains inconsistent, name the scope, trigger, required action, and proof. “After changing database migrations, run make test-migrations” is testable; “test database changes carefully” is not.

10

Adjacent product layers

Where Supercode fits

Native Codex owns instruction discovery, precedence, fallback filenames, and the combined byte limit. Supercode does not change those rules.

If those layers match the repository, install Supercode for Codex. Keep the contract explicit: AGENTS.md routes persistent working agreements, documentation holds durable knowledge, and executable checks prove compliance.

11

Keep it trustworthy

Instruction maintenance checklist

  • The root file is a map and working agreement, not a copied manual.
  • Every command exists and runs from the documented directory.
  • Every nested file contains local differences rather than inherited text.
  • Overrides are intentional, visible, and removed when temporary work ends.
  • Long explanations point to versioned source documents.
  • Mechanical rules are enforced by tools and name their validation command.
  • The relevant root-to-CWD chain stays comfortably below its byte ceiling.
  • A new Codex run can summarize the expected chain and local overrides.
  • A representative task proves the guidance changes observable behavior.
  • Rules likely to drift have an owner or review trigger.

The goal is not to tell Codex everything. Give the current task the smallest reliable path to the right knowledge, commands, boundaries, and proof.

Primary mechanics and supporting evidence

Sources and freshness

Codex discovery, precedence, size defaults, and verification commands were reviewed on September 17, 2026. The GitHub analysis concerns adjacent custom-agent files and is supporting editorial evidence, not a Codex outcome study.

The next useful action

Make repository knowledge easier to create and find

Use guided documentation workflows and bounded repository orientation around the native Codex instruction chain. Discovery and precedence remain under Codex control.

Install Supercode for Codex
TM

About the research desk

Tony Martins

Research on context engineering, agent workflows, and reliable coding-agent operations.
Back to top ↑