拆解 OpenAI 官方 codex 倉庫真實在用的 AGENTS.md,學一個百萬行級專案如何把格式、測試、commit、模組大小、API 命名等「隊規」寫成 AI agent 看得懂、會照做的指令檔,並給你一份可直接套用到自己專案的繁中改寫範本。
# AGENTS.md
> 給 AI coding agent(Codex / Claude Code 等)讀的專案規則檔。放在 repo 根目錄(子目錄可放更貼近該層的覆寫版)。原則:寫成「明確、有立場、可機械執行」的條列,而不是抽象原則。
## Project Setup
- Toolchain / runtime: {{TOOLCHAIN}}(例:Node 20 + pnpm / Rust stable / Python 3.12)
- Required tools to install before starting: {{REQUIRED_TOOLS}}(例:`just`, `rg`, linter)
- DO NOT touch: {{DO_NOT_TOUCH}}(例:sandbox / 環境變數開關 / 生成檔;列出 agent 絕不能改的東西)
## Code Style
- Run `{{FORMAT_CMD}}` automatically after every change (e.g. `just fmt`, `npm run format`).
- Lint with `{{LINT_CMD}}`; fix all warnings before finishing.
- API design: prefer explicit enums/named methods over boolean or ambiguous optional params.
- Prefer private modules with explicit public exports; add doc comments to new public APIs.
## Testing
- Run `{{TEST_CMD}}` (NOT the raw test runner) — e.g. `just test`, `npm test`.
- Scope a single package: `{{TEST_PACKAGE_CMD}}`.
- After changes to {{CORE_AREAS}}, run the full suite.
- Test authoring: compare whole objects, not individual fields; don't write tests for static constants or removed logic.
- {{SNAPSHOT_RULE}}(若有快照測試:所有 UI/輸出變更都要附對應快照,並用 review→accept 流程)
## Module Size & Organization
- Target modules under {{SOFT_LOC}} LoC (excluding tests); split before {{HARD_LOC}} LoC.
- Don't grow large orchestration files; when extracting code, move its tests and docs together.
## Change Size Guidance
- General changes: max {{MAX_LINES}} lines per change.
- Complex logic: keep under {{COMPLEX_MAX_LINES}} lines; stage larger work into reviewable pieces.
- Build context incrementally; do not rewrite history.
## API / Naming Conventions(依專案調整或刪除)
- New surface goes in {{API_VERSION}} only; no additions to deprecated versions.
- Naming: `*Params` (requests), `*Response` (responses), `*Notification` (events).
- Serialization: {{SERIALIZATION_RULE}}(例:camelCase via serde rename / snake_case for config).
## Breaking Changes — check impact on
- Public APIs / CLI params / config loading / session or state resumption.
## Commits & PRs
- {{COMMIT_CONVENTION}}(例:Conventional Commits;一個邏輯變更一個 commit)
- 完成前自我檢查:format ✅ / lint ✅ / test ✅ / 變更行數在上限內 ✅不用離開網站,直接看這組 prompt 跑出來長怎樣(AI 即時生成,扣 1 點)。
不只複製貼上 — 下載後放到 ~/.codex/prompts/openai-codex-agents-md-real-world-example.md,之後輸入 /openai-codex-agents-md-real-world-example 一鍵呼叫。
mkdir -p ~/.codex/prompts && mv ~/Downloads/openai-codex-agents-md-real-world-example.md ~/.codex/prompts/openai-codex-agents-md-real-world-example.mdNew-Item -ItemType Directory -Force "$env:USERPROFILE\.codex\prompts" | Out-Null; Move-Item "$env:USERPROFILE\Downloads\openai-codex-agents-md-real-world-example.md" "$env:USERPROFILE\.codex\prompts\openai-codex-agents-md-real-world-example.md"## 這是什麼/解決什麼痛點 AGENTS.md 是放在程式倉庫根目錄、專門寫給 AI coding agent(Codex、Claude Code 等)讀的「機器版 README」。痛點很實際:當你叫 AI 改一個有規模的專案,它不知道「測試要用哪個指令跑、commit 訊息怎麼下、哪些檔案碰不得、單一檔案不能膨脹到幾行、API 要怎麼命名」——結果就是它寫出風格不一致、踩到地雷、或 PR 大到沒人想 review 的程式碼。AGENTS.md 就是把這些「團隊默契」白紙黑字寫成 agent 看得懂、會照做的指令。 ## 為什麼這個來源值得用 這篇策展的不是教學文,而是 **OpenAI 官方 codex 倉庫自己正在用的那份 AGENTS.md**——一個真實、大型、多 crate 的 Rust/TypeScript/Python 混合專案。它的價值在於「真的在跑」:你看到的每一條規則都是 OpenAI 團隊踩過坑後沉澱下來的,不是憑空想像。它示範了一份成熟 AGENTS.md 該涵蓋哪些面向。 ## 原檔涵蓋的重點(節錄觀察) - **格式與 lint**:改完自動跑 `just fmt`、內聯 `format!` 參數、`match` 要窮舉不留萬用分支。 - **測試紀律**:用 `just test` 不用 `cargo test`、比較「整個物件」而非單一欄位、UI 變更一律要附 insta 快照。 - **模組大小硬上限**:目標每個模組 500 LoC 以下、~800 LoC 就該拆;明令不要養大 `chatwidget.rs`/`app.rs` 這種 orchestration 巨檔。 - **變更大小**:一般變更上限 800 行、複雜邏輯 500 行以內,更大的要拆成可 review 的片段。 - **API 命名公約**:新介面只進 v2、`*Params`/`*Response`/`*Notification` 命名、serde camelCase 等——示範了如何把「團隊 API 慣例」也寫進 agent 規則。 - **碰不得清單**:明確標記與 sandbox 環境變數相關的程式碼「永遠不要改」。 ## 怎麼用 1. 在你的 repo 根目錄建 `AGENTS.md`(子目錄可放更貼近該層的覆寫版,agent 會就近採用)。 2. 套用右側 full_prompt 範本,把 `{{TOOLCHAIN}}`、`{{TEST_CMD}}`、`{{FORMAT_CMD}}`、行數上限等佔位符換成你專案的真實值。 3. 重點放在「可機械執行」的條列——指令、數字上限、命名規則——而不是「請寫乾淨的程式碼」這種抽象口號。 4. 把「絕對不要碰的檔案/設定」明確列出,這是省下最多災難的一條。 ## 何時用 任何你打算讓 AI agent 反覆動手的專案都該有一份。專案越大、貢獻者越多、慣例越多,AGENTS.md 的回報越高。 📎 來源:openai/codex 倉庫的 AGENTS.md(作者 OpenAI,Apache-2.0 授權)— 本篇為繁中改寫整理,內容經改寫、非逐字轉載,原始檔案見上方連結。
[TOOLCHAIN]專案的語言與工具鏈,例:Node 20 + pnpm / Rust stable / Python 3.12
[REQUIRED_TOOLS]開工前必裝的工具清單,例:just、ripgrep、linter
[DO_NOT_TOUCH]agent 絕對不能改的檔案或設定(sandbox 開關、生成檔等)
[FORMAT_CMD]格式化指令,例:just fmt 或 npm run format
[LINT_CMD]lint 指令
[TEST_CMD]填下面的欄位,上方 prompt 會即時替換 [方括號] 內容。填好後按「複製組好的 prompt」直接丟進工具。
# AGENTS.md
> 給 AI coding agent(Codex / Claude Code 等)讀的專案規則檔。放在 repo 根目錄(子目錄可放更貼近該層的覆寫版)。原則:寫成「明確、有立場、可機械執行」的條列,而不是抽象原則。
## Project Setup
- Toolchain / runtime: {{TOOLCHAIN}}(例:Node 20 + pnpm / Rust stable / Python 3.12)
- Required tools to install before starting: {{REQUIRED_TOOLS}}(例:`just`, `rg`, linter)
- DO NOT touch: {{DO_NOT_TOUCH}}(例:sandbox / 環境變數開關 / 生成檔;列出 agent 絕不能改的東西)
## Code Style
- Run `{{FORMAT_CMD}}` automatically after every change (e.g. `just fmt`, `npm run format`).
- Lint with `{{LINT_CMD}}`; fix all warnings before finishing.
- API design: prefer explicit enums/named methods over boolean or ambiguous optional params.
- Prefer private modules with explicit public exports; add doc comments to new public APIs.
## Testing
- Run `{{TEST_CMD}}` (NOT the raw test runner) — e.g. `just test`, `npm test`.
- Scope a single package: `{{TEST_PACKAGE_CMD}}`.
- After changes to {{CORE_AREAS}}, run the full suite.
- Test authoring: compare whole objects, not individual fields; don't write tests for static constants or removed logic.
- {{SNAPSHOT_RULE}}(若有快照測試:所有 UI/輸出變更都要附對應快照,並用 review→accept 流程)
## Module Size & Organization
- Target modules under {{SOFT_LOC}} LoC (excluding tests); split before {{HARD_LOC}} LoC.
- Don't grow large orchestration files; when extracting code, move its tests and docs together.
## Change Size Guidance
- General changes: max {{MAX_LINES}} lines per change.
- Complex logic: keep under {{COMPLEX_MAX_LINES}} lines; stage larger work into reviewable pieces.
- Build context incrementally; do not rewrite history.
## API / Naming Conventions(依專案調整或刪除)
- New surface goes in {{API_VERSION}} only; no additions to deprecated versions.
- Naming: `*Params` (requests), `*Response` (responses), `*Notification` (events).
- Serialization: {{SERIALIZATION_RULE}}(例:camelCase via serde rename / snake_case for config).
## Breaking Changes — check impact on
- Public APIs / CLI params / config loading / session or state resumption.
## Commits & PRs
- {{COMMIT_CONVENTION}}(例:Conventional Commits;一個邏輯變更一個 commit)
- 完成前自我檢查:format ✅ / lint ✅ / test ✅ / 變更行數在上限內 ✅這組 prompt 專為 OpenAI Codex 設計。把 prompt 內 16 個方括號 [變數] 換成你自己的內容,貼進 OpenAI Codex 執行即可。難度中等,照變數說明填好後即可上手。
完整 prompt 免費開放閱讀,不用註冊;登入後可一鍵複製、收藏與留言。
prompt 文字本身你可自由使用與修改。但 AI 生成物(圖/音樂/影片/文字)的商用授權,取決於你在 OpenAI Codex 使用的方案與其官方服務條款,請以該工具的授權規範為準。
Studio engineer 視角拆解 Suno 致命弱點(油炸 vocals、高頻 artifact)+ 4 步驟 DAW workflow + Suno Studio 修音 prompt
提案產生器 / 會議處理器 / 內容再利用 / 週五回顧 / 收工 reset — 試了 40 個只有這 5 個沒被丟掉、各省 30+ 分鐘 / 次。
適合:部落格、Medium、Notion 公開頁、Substack — 任何支援 iframe / HTML 嵌入的地方。對方點「看完整」會回到本站、是 prompt 庫的免費 backlink。
<iframe src="https://prompt.luvai.net/embed/openai-codex-agents-md-real-world-example" width="100%" height="380" frameborder="0" style="border:1px solid #e0dcd0;border-radius:4px;" loading="lazy" title="PromptCraft Embed"></iframe>
把方括號 [ ] 內的變數換成你的內容,丟進 OpenAI Codex。
跑測試的標準指令(用包好的指令而非裸 test runner)
[TEST_PACKAGE_CMD]只跑單一套件/模組的測試指令
[CORE_AREAS]改動後需要跑全套測試的核心區域
[SNAPSHOT_RULE]快照測試規則(若無可刪此行)
[SOFT_LOC]模組建議行數上限,例:500
[HARD_LOC]模組必須拆分的硬行數,例:800
[MAX_LINES]單次變更建議最大行數,例:800
[COMPLEX_MAX_LINES]複雜邏輯單次變更上限,例:500
[API_VERSION]新 API 應放入的版本,例:v2
[SERIALIZATION_RULE]序列化命名規則,例:camelCase via serde rename
[COMMIT_CONVENTION]commit / PR 慣例,例:Conventional Commits、一變更一 commit
六個月在 Claude / GPT-4 / Gemini 上用人工 rater A/B 測 200+ prompt 後寫的。包含 persona+constraint stacking / anti-example / role reversal QA / cognitive scaffold / emotional priming / uncertainty CoT / steelman first。
一年的 role-play system prompt + 14-step framework 後總結:真正改變品質的是 5 個單行 prompt。沒有 role、沒有 markdown、沒有「you are an expert」。