AI Learn
OpenAI · Codex Tutorial

Write layered AGENTS.md instructions for Codex

Use global and repository-level AGENTS.md files to give Codex durable working agreements, with narrower instructions closer to the code they govern.

Reviewed September 20, 2026Independent tutorialFeatures may vary
Not official support. AI Learn is an independent education site and is not affiliated with OpenAI. Check the provider’s current documentation for plan-specific limits, pricing and feature availability.
Before you start

Codex reads AGENTS.md before work and can combine instructions from global and nested project locations. Keep each layer concise and put narrow rules near the code they affect.

1. Create a global baseline only if useful

Put cross-repository preferences in ~/.codex/AGENTS.md: preferred package manager, general testing discipline or approval expectations. Avoid project-specific paths.

2. Add repository agreements

At the repository root, document install/test commands, architectural boundaries, generated files and the definition of done for common changes.

3. Use nested instructions for special areas

Place a closer AGENTS.md or supported override near a subsystem that has different tooling or constraints. The more local instruction should be the more specific one.

4. Keep the total instruction chain small

Long instruction chains dilute important rules and can hit configured size limits. Link to documentation when detail is reference material rather than an instruction.

5. Ask Codex to summarize the active rules

Start a fresh session in a nested directory and ask which instructions apply. Confirm the merged behavior matches your intended hierarchy.

Try this prompt

Before starting this task, summarize the AGENTS.md instruction chain that applies from global scope to the current directory. Highlight any local rule that overrides a broader rule.

Common mistakes

Putting repository paths in global instructions; duplicating the same rule at every level; making nested files contradict each other accidentally; letting instructions grow beyond what a human can review.

Finish check

Codex can explain the active instruction hierarchy and follows the narrow local rule without losing the repository-wide baseline.

开始之前

Codex 会在工作前读取 AGENTS.md,并可以组合全局和项目目录中的多层指令。每层保持精简,把窄规则放到它真正影响的代码附近。

1. 只有确实需要时才写全局基线

把跨仓库都适用的偏好放到 ~/.codex/AGENTS.md,例如首选包管理器、通用测试纪律或审批原则,不要写项目专属路径。

2. 在仓库根目录写项目约定

记录安装/测试命令、架构边界、生成文件和常见修改的完成定义。

3. 特殊子系统用嵌套指令

某个子目录如果使用不同工具或约束,可以在更靠近它的位置放 AGENTS.md 或支持的 override,让更局部的规则承担更具体的要求。

4. 控制整个指令链的长度

过长的指令链会稀释重要规则,也可能碰到配置的大小上限。纯参考资料更适合链接出去,而不是全部塞成指令。

5. 让 Codex 总结当前实际生效规则

在某个嵌套目录重新启动会话,让 Codex 说出当前适用的指令,确认最终合并结果与设计层级一致。

可以直接套用的提示词

开始任务前,请总结从全局到当前目录实际生效的 AGENTS.md 指令链,并特别指出哪些更局部规则覆盖了上层规则。

常见错误

在全局指令里写项目专属路径;每一层重复同样规则;嵌套文件无意互相冲突;指令增长到人类自己都无法审查。

完成检查

完成后,Codex 能解释当前指令层级,并在保留仓库通用基线的同时遵守更局部的窄规则。

開始之前

Codex 會在工作前讀取 AGENTS.md,並可以組合全局和專案目錄中的多層指令。每層保持精簡,把窄規則放到它真正影響的代碼附近。

1. 只有確實需要時才寫全局基線

把跨倉庫都適用的偏好放到 ~/.codex/AGENTS.md,例如首選包管理器、通用測試紀律或審批原則,不要寫專案專屬路徑。

2. 在倉庫根目錄寫專案約定

記錄安裝/測試指令、架構邊界、生成檔案和常見修改的完成定義。

3. 特殊子系統用嵌套指令

某個子目錄如果使用不同工具或約束,可以在更靠近它的位置放 AGENTS.md 或支持的 override,讓更局部的規則承擔更具體的要求。

4. 控制整個指令鏈的長度

過長的指令鏈會稀釋重要規則,也可能碰到配置的大小上限。純參考資料更適合連結出去,而不是全部塞成指令。

5. 讓 Codex 總結當前實際生效規則

在某個嵌套目錄重新啓動會話,讓 Codex 說出當前適用的指令,確認最終合併結果與設計層級一致。

可以直接套用的提示詞

開始任務前,請總結從全局到當前目錄實際生效的 AGENTS.md 指令鏈,並特別指出哪些更局部規則覆蓋了上層規則。

常見錯誤

在全局指令里寫專案專屬路徑;每一層重復同樣規則;嵌套檔案無意互相衝突;指令增長到人類自己都無法審查。

完成檢查

完成後,Codex 能解釋當前指令層級,並在保留倉庫通用基線的同時遵守更局部的窄規則。

Official references

Check current product details at the source

Related practical tutorials

Continue with another single-task workflow.

All tutorials →