CLAUDE.md 和 AGENTS.md 每輪占多少 token,寫多長才適合?
沒有固定數字:檔案寫多長就占多少,而且這個工作階段裡每則訊息都帶著它。Anthropic 建議每個 CLAUDE.md 控制在 200 行以內;Codex 的 AGENTS.md 合計預設上限 32 KiB,超出的部分不會讀進去。只留每次都用得上的規則,其餘移走。
Codex
AGENTS.md 在啟動時讀一次,之後整個工作階段都帶著;合計預設上限 32 KiB,OpenAI 明確建議把它寫短。
Codex 啟動時先讀 ~/.codex/AGENTS.md,再從專案根目錄一路讀到你目前所在的目錄,每層最多一份,依序拼成一段放進提示裡。拼到 32 KiB(設定項 project_doc_max_bytes)就不再往裡加,後面的規則等於沒寫。大型專案的瘦身方法是分層:根目錄只留全專案通用的幾條,模組專屬的規則放進該模組目錄自己的 AGENTS.md,只有在那個目錄裡啟動 Codex 才會帶上。
官方原文
Reduce the size of your AGENTS.md. If you work on a larger project, you can control how much context you inject through AGENTS.md files by nesting them within your repository.Codex builds an instruction chain when it starts (once per run; in the TUI this usually means once per launched session).Codex skips empty files and stops adding files once the combined size reaches the limit defined by project_doc_max_bytes (32 KiB by default).Codex concatenates files from the root down, joining them with blank lines. Files closer to your current directory override earlier guidance because they appear later in the combined prompt.Claude
CLAUDE.md 每個工作階段一開始就整份讀進上下文,官方建議每個檔案 200 行以內;用 /context 看它實際占了多少。
Claude Code 啟動時會讀 ~/.claude/CLAUDE.md、專案裡的 CLAUDE.md(或 .claude/CLAUDE.md)、CLAUDE.local.md,以及上層目錄裡的同名檔案;子目錄裡的要等 Claude 讀到那個目錄的檔案時才載入。Claude Code 每次請求都會送出整段對話,這份內容也就跟著每則訊息再算一遍。Anthropic 的原話是:檔案越長,占的上下文越多,Claude 照著做的程度反而下降。
官方原文
CLAUDE.md files are loaded into the context window at the start of every session, consuming tokens alongside your conversation.Size: target under 200 lines per CLAUDE.md file. Longer files consume more context and reduce adherence.Claude Code sends your full conversation with every request從 CLAUDE.md 裡移出去的內容放哪裡
Claude Code 裡只有兩種方法是用到才載入的,其餘的(包括用 @ 引入的檔案)都在啟動時讀進去。
只在某類流程裡用到的長說明(發布、審查、資料庫遷移):移進 skill,用到才載入。
只跟某些檔案有關的規則:放進 .claude/rules/,並在檔案開頭寫 paths:,Claude 讀到符合的檔案才載入。
寫給人看的維護備註:用 HTML 註解包起來,Claude Code 會在讀進上下文之前把整塊註解拿掉,不占 token。
官方原文
If it contains detailed instructions for specific workflows (like PR reviews or database migrations), those tokens are present even when you're doing unrelated work. Skills load on-demand only when invoked, so moving specialized instructions into skills keeps your base context smaller. Aim to keep CLAUDE.md under 200 lines by including only essentials.Splitting into @path imports helps organization but doesn't reduce context, since imported files load at launch.Block-level HTML comments (<!-- maintainer notes -->) in CLAUDE.md files are stripped before the content is injected into Claude's context. Use them to leave notes for human maintainers without spending context tokens on them.常見誤解
- CLAUDE.md 寫得越細,Claude 越聽話
不是。Anthropic 的說法正好相反:檔案越長,占的上下文越多,照著做的程度越低;指令越具體、越簡短,執行得越穩。寫長了就是每則訊息都在付的固定開銷。
- 把 CLAUDE.md 拆成幾個檔案用 @ 引入,就能省 token
省不了。@ 引入的檔案在啟動時一樣會全部讀進上下文,只是方便整理。真正能省的是兩種用到才載入的方法:帶 paths: 的規則,和 skill。
- CLAUDE.md 超過 200 行的部分不會被讀
不是。200 行是建議,不是截斷線:CLAUDE.md 只要不超過 4 MiB 都會整份讀進去,多寫的每一行都在占 token。「只讀前 200 行或 25KB」說的是自動記憶的 MEMORY.md,不是 CLAUDE.md。真正會截斷的是 Codex:AGENTS.md 合計超過 32 KiB 後就不再讀入。
小提醒
- Claude Code:輸入 /context,看 Memory files 這一類占了多少、實際載入了哪些檔案。
- Claude Code:輸入 /memory 直接打開這些檔案來刪改;改動從下一個工作階段起生效,/compact 之後 Claude Code 也會從磁碟重讀專案根目錄的 CLAUDE.md。
- 逐條看過一遍:不是每次都用得上的規則就刪掉或移走,只留建置指令、慣例、目錄結構、「永遠要這樣做」這類內容。
- Codex:根目錄的 AGENTS.md 只留通用規則,模組規則放進模組目錄自己的 AGENTS.md。
- Codex:執行 codex --ask-for-approval never "Summarize the current instructions.",確認讀進去的就是你想要的那幾條。
- 兩個工具都用:只維護一份 AGENTS.md。專案裡沒有 CLAUDE.md 時,新版 Claude Code 會直接讀它;同時有 CLAUDE.md 的話,在裡面寫一行 @AGENTS.md。引入的內容照樣算 token,所以 AGENTS.md 本身要短。
