Optimize AGENTS.md and skills for Codex
PLOptimizing AGENTS.md and Agent Skills for Codex keeps the always-loaded instruction chain short and layered, moves multi-step procedures into on-demand SKILL.md directories, and verifies deterministically what reached the prompt. Because Codex joins project instruction files root-to-leaf inside a fixed byte budget, oversized root files silently cut your deepest module rules first.
Every repository owner hits the same mystery sooner or later: you add a sharp invariant to services/payments/AGENTS.md, yet Codex keeps violating it while faithfully obeying vague prose in the root AGENTS.md. Before blaming the model or adding louder capital letters, verify whether that module file survived the instruction loader at all.
How Codex resolves the AGENTS.md hierarchy
Section titled “How Codex resolves the AGENTS.md hierarchy”Codex builds its instruction block in a deterministic sequence across global and repository scopes (see developers.openai.com/codex for current config keys):
- Global personal file: Codex loads
~/.codex/AGENTS.override.mdif present, otherwise~/.codex/AGENTS.md. Global instructions sit outside the project byte budget. - Project root discovery: Starting from your working directory, Codex walks upward to the project root marker (default
.git). - One winner per directory: Walking root-to-leaf, Codex picks the first matching filename in each directory—
AGENTS.override.mdfirst, thenAGENTS.md, then any configured fallback filenames. - Root-to-leaf concatenation: Files join from root down to the deepest active directory so the most specific guidance lands last.
- Fixed project byte budget and trust gate: Combined project files share a byte cap (
project_doc_max_bytes, default 32 KiB). Untrusted repositories load zero project instruction files.
Two edge cases break teams regularly:
Keep each layer sharp and move enforcement down the stack
Section titled “Keep each layer sharp and move enforcement down the stack”Every instruction file should answer one narrow question. Anything outside that scope belongs elsewhere:
| Layer | File | Owns | Target size |
|---|---|---|---|
| Personal | ~/.codex/AGENTS.md | Cross-repo workflow preferences | Under 20 lines |
| Repository root | AGENTS.md | Exact build, lint, typecheck, and test commands plus repo-wide deviations from defaults | Under 50 lines |
| Service / package | services/payments/AGENTS.md | Invariants unique to that directory (custom error wrappers, local test command) | Under 30 lines |
Three categories waste instruction budget across every layer:
- Generic advice the model already follows (“write clean, maintainable code”, “handle errors gracefully”).
- Prose that duplicates code structure (hand-written directory trees and architecture essays that rot within weeks).
- Rare multi-step procedures (release checklists, migration runbooks, PR prep templates that load on every turn even when unused).
For empirical pruning methods and ablation checks, see pruning context files and AGENTS.md context architecture.
Choose the strongest enforcement layer
Section titled “Choose the strongest enforcement layer”AGENTS.md is advisory context the model reads. Move rules down to deterministic enforcement whenever a mistake would hurt production:
| Guidance type | Best home | Why |
|---|---|---|
| Short repo convention needed on most turns | AGENTS.md | Always present and cheap when concise |
| Multi-step procedure used occasionally | Agent Skill (SKILL.md) | Only name and description load until triggered |
| Runtime settings (sandbox, approvals, MCP) | .codex/config.toml | Applied deterministically by the runtime |
| Commands forbidden outside the sandbox | Codex command rules / requirements.toml | Enforced outside the model |
| Non-negotiable code invariant (no raw errors, no float money) | Lint rule, fitness test, or CI check + one-line pointer in AGENTS.md | Fails the build deterministically when violated |
That last row separates hope from proof: AGENTS.md raises the odds of compliance, whereas a lint rule or test turns every violation into a red CI gate nobody has to spot by eye.
Use AGENTS.override.md only for timed transitions
Section titled “Use AGENTS.override.md only for timed transitions”Because AGENTS.override.md completely shadows AGENTS.md in the same directory, use it strictly for temporary freezes or active migrations. Put an explicit owner and removal condition on line 1, copy forward any permanent rules that must remain active during the transition, and fail CI if an empty override exists.
Move procedures into progressive-disclosure Agent Skills
Section titled “Move procedures into progressive-disclosure Agent Skills”An Agent Skill is a directory containing a SKILL.md file (plus optional scripts/ and references/) following the open Agent Skills standard. Codex injects only each skill’s name and description into the session catalog, loading the full instructions only when invoked explicitly ($skill-name) or matched implicitly by the prompt.
.agents/skills/pr-ready/ SKILL.md # frontmatter (name + routing description) + procedure scripts/ # optional deterministic helpers references/ # optional domain specs loaded on demand---name: pr-readydescription: Prepare the current branch for a pull request. Use when asked to run pre-PR checks or draft a PR summary. Not for reviewing someone else's pull request.---
1. Run the project lint, typecheck, and test commands from AGENTS.md; stop on failures.2. Summarize changed files, verification commands with exit codes, and uncovered risks.Routing quality lives in the description. Name both when to use the skill and when not to use it so implicit activation stops misfiring on adjacent tasks. Commit shared skills under .agents/skills/ so teammates and cloud runs discover the exact same workflows. Browse curated skills at skills.wondel.ai and read cross-tool design guidance in our skills ecosystem guide.
Budget the skill catalog and MCP tool schemas
Section titled “Budget the skill catalog and MCP tool schemas”Skills and MCP servers are lighter than root AGENTS.md prose, yet both consume session context:
- Skill catalog budget: Skill names and descriptions share a context token cap (
skills.max_context_tokens). Installing dozens of overlapping skills crowds useful ones out of the catalog—disable unneeded skills inconfig.toml. - MCP schema overhead: Every connected MCP server appends its tool schemas to every turn. Disable unused servers per project and restrict noisy servers with
enabled_tools(MCP specification).
Raise project_doc_max_bytes only after cutting fluff and extracting procedures into skills—a larger cap hides bloat while taxing every turn.
Verify instructions actually load and hold
Section titled “Verify instructions actually load and hold”Replace guesswork (“did the agent read my rule?”) with four deterministic checks:
-
Inspect the exact rendered prompt: Run
codex debug prompt-inputfrom your real working directory (see developers.openai.com/codex for current flags) to inspect the rendered<INSTRUCTIONS>block rather than asking the model from memory. Confirm the deepest service file’s last line appears intact. -
Enforce a CI byte-budget gate: Add a lightweight CI script that walks every directory containing an
AGENTS.mdorAGENTS.override.md, sums the root-to-leaf chain byte size againstproject_doc_max_bytes, warns or fails on any zero-byteAGENTS.override.md, and fails the build before silent truncation can land onmain. -
Run an automated harness audit: Execute
npx @wondelai/harness-audit(Harness Audit, audit checks reference) to detect oversized instruction files, missing verification commands, and unbacked rules. -
Put context files under CODEOWNERS: Treat
AGENTS.md,AGENTS.override.md, and.agents/skills/as production harness code reviewed by a tech lead, because one sloppy edit degrades every subsequent agent session.
When AGENTS.md and skills silently fail in Codex
Section titled “When AGENTS.md and skills silently fail in Codex”- Deep module rules get ignored while root rules work: The root-to-leaf chain exceeded
project_doc_max_bytes. Measure byte totals from the affected directory, prune root fluff, and promote procedures into skills before considering a higher cap. - An entire directory’s guidance disappears: A forgotten or zero-byte
AGENTS.override.mdis shadowing the siblingAGENTS.md. Search forAGENTS.override.mdacross the repo and remove any file without an active owner. - Zero project instructions load at all: The repository is untrusted, or Codex launched outside the project root. Note that local prompt-debugging tools can render files even when interactive sessions suppress untrusted projects—confirm trust status in user config for repositories you control.
- Rules hold early in a session and fade later: Long-thread compaction summarized earlier context. Start a fresh session for the next milestone and restate critical invariants, or enforce them in lint and tests.
- A skill never activates—or triggers on the wrong prompts: Refine the
descriptionwith phrases your team actually uses plus an explicit “Not for…” boundary, invoke explicitly with$namewhile calibrating, and disable competing skills. - Cloud tasks ignore local rules: Hosted Codex cloud environments read only committed repository files; move shared rules out of
~/.codex/AGENTS.mdinto version control.
Official docs and next steps
Section titled “Official docs and next steps”- Official reference for instruction loading, skills, and debug commands: OpenAI Codex documentation · Agent Skills standard
- Cut existing files with evidence: pruning
CLAUDE.mdandAGENTS.md - Back instructions with deterministic gates: making a codebase agent-ready
- Pair with config layers: configure Codex with
config.toml - Working across Claude Code too? Read the Claude Code memory system
Did this guide help you?
One tap, no sign-in.
What will you do with it?
How did it read?
What got in the way? Pick any that apply
Your role?
Thanks. I read every answer. Michał Jaskólski
Did this guide help?
One tap each. Close whenever you like.
no sign-in
Frequently asked questions
What is the default byte budget for AGENTS.md in Codex?
Codex enforces a combined project instruction cap of 32 KiB (project_doc_max_bytes) across the root-to-leaf AGENTS.md chain. When the root file grows too large, the deepest module rules are truncated first.
How can I inspect which AGENTS.md files Codex actually loaded?
Run codex debug prompt-input from your target directory. It prints the model-visible instruction payload as JSON without invoking a model, revealing byte truncation, shadowing AGENTS.override.md files, or untrusted workspaces.
When should rules move from AGENTS.md into a SKILL.md skill?
Move any multi-step procedure that applies to only a subset of tasks (migrations, release checklists, incident triage) into a SKILL.md directory so it loads on demand instead of consuming the always-on AGENTS.md budget.