Branchwell

Become a Pro / website · becomeapro.ai / content / lessons / knowledge-work / using-ai / agents-md-codex.mdx

1---
2title: "How to set up AGENTS.md for Codex (with examples)"
3description: "Give OpenAI Codex standing instructions with AGENTS.md: where the files go, how Codex combines them, overrides for one folder, size limits, and how to check what it loaded."
4level: Beginner
5minutes: 9
6updated: 2026-12-03
7order: 4
8sources:
9 - https://agents.md
10 - https://learn.chatgpt.com/docs/agent-configuration/agents-md
11 - https://developers.openai.com/codex/guides/agents-md
12 - https://learn.chatgpt.com/guides/best-practices
13 - https://code.claude.com/docs/en/memory
14---
15
16`AGENTS.md` is a plain Markdown file that tells a coding agent how to work in your project: how to install, build and test, the conventions to follow, and what to avoid. Codex reads it before it starts working, so you don't repeat the same instructions in every prompt.
17
18It's also an open format, not a Codex-only file. Codex, Cursor, GitHub Copilot's coding agent, Gemini CLI, Jules, Zed, Warp, VS Code and many other tools support it, and current versions of Claude Code read it too. The format is now stewarded by the Agentic AI Foundation under the Linux Foundation. There are no required fields: any Markdown works.
19
20This guide follows OpenAI's AGENTS.md guide for Codex.
21
22## Where Codex looks
23
24Codex builds its instructions from two places, every time it starts:
25
261. **Your global file.** In your Codex home folder, `~/.codex` unless you set `CODEX_HOME`, Codex reads `AGENTS.override.md` if it exists, otherwise `AGENTS.md`. Use this for preferences that apply to every project.
272. **Your project.** Starting at the project root, usually the Git root, Codex walks down folder by folder to the directory you're working in. In each folder it takes the first file it finds of `AGENTS.override.md`, then `AGENTS.md`. It reads at most one file per folder, and skips empty files.
28
29<Diagram caption="Codex reads your global file first, then one file per folder from the project root down to where you are working.">
30 <svg viewBox="0 0 600 260" role="img" aria-label="A chain of three boxes: the global file in the Codex home folder, the project root file, and a subfolder file, joined in order">
31 <rect x="30" y="30" width="250" height="50" rx="8" fill="none" stroke="currentColor" strokeWidth="2" />
32 <text x="155" y="61" fontSize="16" textAnchor="middle" fill="currentColor">~/.codex/AGENTS.md</text>
33 <rect x="30" y="110" width="250" height="50" rx="8" fill="none" stroke="currentColor" strokeWidth="2" />
34 <text x="155" y="141" fontSize="16" textAnchor="middle" fill="currentColor">repo/AGENTS.md</text>
35 <rect x="30" y="190" width="250" height="50" rx="8" className="soft" stroke="currentColor" strokeWidth="2" />
36 <text x="155" y="214" fontSize="15" textAnchor="middle" fill="currentColor">repo/services/payments/</text>
37 <text x="155" y="232" fontSize="15" textAnchor="middle" fill="currentColor">AGENTS.override.md</text>
38 <line x1="155" y1="80" x2="155" y2="108" stroke="currentColor" strokeWidth="2" />
39 <line x1="155" y1="160" x2="155" y2="188" stroke="currentColor" strokeWidth="2" />
40 <text x="310" y="61" fontSize="15" fill="currentColor">1. Global, read first</text>
41 <text x="310" y="141" fontSize="15" fill="currentColor">2. Project root</text>
42 <text x="310" y="214" fontSize="15" fill="currentColor">3. Closest folder, read last,</text>
43 <text x="310" y="234" fontSize="15" fill="currentColor">so it wins a disagreement</text>
44 </svg>
45</Diagram>
46
47Codex joins everything it found in that order: global first, then the project root, then each folder down to where you are. Files closer to your working directory come later, so their instructions win when two disagree.
48
49Two details catch people out:
50
51- Codex stops at your current directory. An `AGENTS.md` in a folder below it isn't read until you work from that folder.
52- The instructions are built once, when Codex starts. Edit an `AGENTS.md` and you need a new session to see the change.
53
54## Start one with /init
55
56In a project, start Codex and run `/init`. Codex scans the repo and writes a starting `AGENTS.md`. Treat it as a draft: OpenAI's advice is to keep the file short and add a rule only after you see Codex make the same mistake more than once.
57
58## A global AGENTS.md
59
60Start with the few things you want in every project. This is the example from OpenAI's guide:
61
62```markdown
63# ~/.codex/AGENTS.md
64
65## Working agreements
66
67- Always run `npm test` after modifying JavaScript files.
68- Prefer `pnpm` when installing dependencies.
69- Ask for confirmation before adding new production dependencies.
70```
71
72## A project AGENTS.md
73
74At the root of your repo, describe the project the way you'd brief a new teammate. Keep each line something Codex can act on or check:
75
76```markdown
77# AGENTS.md
78
79## Setup
80- Install dependencies: `pnpm install`
81- Start the dev server: `pnpm dev` (http://localhost:3000)
82- Run tests: `pnpm test`. Run them after every change.
83
84## Code style
85- TypeScript strict mode. No `any` without a comment saying why.
86- Server components by default; add "use client" only when needed.
87
88## Repository expectations
89- Run `pnpm lint` before opening a pull request.
90- Document public utilities in `docs/` when you change their behavior.
91- Never commit `.env` files or secrets.
92```
93
94OpenAI's advice is to keep the rules short and leave formatting and lint checks to CI, where they're enforced anyway.
95
96## Override one folder
97
98Some parts of a codebase need their own rules. Put an `AGENTS.override.md` in that folder and Codex uses it there instead of that folder's `AGENTS.md`. The root file still loads. OpenAI's example for a payments service:
99
100```markdown
101# services/payments/AGENTS.override.md
102
103## Payments service rules
104
105- Use `make test-payments` instead of `npm test`.
106- Never rotate API keys without notifying the security channel.
107```
108
109An override works globally too: `~/.codex/AGENTS.override.md` temporarily replaces your global `AGENTS.md` without deleting it. Delete the override to go back.
110
111## Size limits and other file names
112
113Codex stops adding files once the combined instructions reach 32 KiB, the default for `project_doc_max_bytes`. If your team already uses a different file name, Codex can read that when a folder has no `AGENTS.md`. Both settings go in `~/.codex/config.toml`:
114
115```toml
116# ~/.codex/config.toml
117project_doc_fallback_filenames = ["TEAM_GUIDE.md", ".agents.md"]
118project_doc_max_bytes = 65536
119```
120
121With that config, Codex checks each folder for `AGENTS.override.md`, then `AGENTS.md`, then `TEAM_GUIDE.md`, then `.agents.md`. Other names are ignored.
122
123## Check what Codex loaded
124
125Ask Codex directly. These commands from OpenAI's guide run without stopping for approvals:
126
127```bash
128# From the repo root
129codex --ask-for-approval never "Summarize the current instructions."
130
131# From a subfolder, to check overrides
132codex --cd services/payments --ask-for-approval never "List the instruction sources you loaded."
133```
134
135If the answer is missing something, check the file isn't empty, that it's between the project root and your working directory, and that the combined size is under the limit.
136
137## Share one file with Claude Code
138
139If your team also uses Claude Code, you don't need two copies. Claude Code reads `AGENTS.md` when the project has no `CLAUDE.md` or `CLAUDE.local.md`, and a `CLAUDE.md` that starts with `@AGENTS.md` imports it. Claude Code ignores `AGENTS.override.md`, so keep shared rules in the plain `AGENTS.md`. [How to write a CLAUDE.md that Claude Code follows](/knowledge-work/using-ai/claude-md-guide) covers the Claude side.