對話越長越耗 token:該 /compact 還是 /clear?
換任務就 /clear,同一個任務做到一半才 /compact。每則訊息都要把整段對話重讀一遍,所以工作階段越長,每一句越貴;/clear 不花 token,/compact 本身是一次大請求。
Codex
換任務用 /new 開新對話,同一個任務太長了用 /compact。
Codex 的用量跟著上下文大小走,輸入越大扣得越多;快取過的輸入依一般輸入的十分之一計算,寫入快取不收費,但對話越長這一份也越大。換任務時輸入 /new(或 /clear)開一個新對話,上下文從零開始;同一個任務還要接著做、但已經很長了,就用 /compact 把對話壓成摘要。/status 能看到目前的 token 用量和上下文還剩多少,舊對話用 /resume 找回。
官方原文
Model choice, context, reasoning, tool use, retrieval, and caching all affect usage, so prompt length alone isn't a reliable estimate.Charges are based on the model used and the actual input, cached input, and output tokens consumed.Codex does not charge for cache writes./new: Start a new chat inside the same CLI session./compact: Summarize the visible chat to free tokens./status: Display session configuration and token usage.Claude
換任務就 /clear,不花 token;同一個任務做到一半才 /compact,它本身是一次大請求。
Claude Code 每次請求都會帶上整段對話,每呼叫一次工具又是一次請求;有快取時依快取費率(約為一般輸入的十分之一)重讀,所以在開了一整天的工作階段裡問一句話,也要為整段歷史付一次。/usage 面板上的原話是:長工作階段即使有快取也比較貴,任務做到一半用 /compact,換任務用 /clear(Longer sessions are more expensive even when cached. /compact mid-task, /clear when switching to new tasks.)。訂閱方案下快取保留 1 小時,動用用量點數時只有 5 分鐘;停得比這更久,回來的第一則訊息會把全部上下文依原價重新處理,這是最貴的一則。/clear 之前先用 /rename 取個名字,之後用 /resume 找回。/compact 後面可以加一句要保留什麼,例如:/compact 只保留測試輸出和程式碼改動;也可以在 CLAUDE.md 裡加一節 # Compact instructions,寫成固定規則。
官方原文
With prompt caching, Claude Code re-reads that history at the cached token rate, so a one-line question in a session that has been open all day still draws usage for the whole conversation.Cache misses: your first message after a break longer than the cache lifetime misses the cache and reprocesses your full context. The lifetime is an hour on a subscription and drops to five minutes once you're drawing on usage creditsCompaction: /compact reads the conversation it summarizes, so compacting a large context is itself a large request. When you want a fresh start instead of continuity, /clear costs nothingUse /clear to start fresh when switching to unrelated work. Stale context wastes tokens on every subsequent message. Use /rename before clearing so you can easily find the session later, then /resume to return to it.cache_read_input_tokens — Tokens served from cache on this turn, billed at roughly 10% of the standard input rate網路搜尋、讀大檔案、長日誌:別整塊塞進上下文
搜尋一次不貴,貴的是把整頁網頁、整個檔案、整段日誌放進上下文——放進去以後,後面每一則訊息都要為它再付一次。
只要一個事實就讓它搜尋,別讓它擷取整頁;真的需要全文時才擷取。Claude 的 web search 和 web fetch 都會計入額度,摘要一篇萬字長文比搜尋一次貴得多;Research 會連續搜尋很多次,更耗。
讀檔案時點名檔案和函式,能給行號範圍就給。「改 auth.ts 裡的 login 函式」只讀一個檔案;「最佳化一下程式碼」會觸發大範圍掃描。
日誌和測試輸出先過濾再給它。Claude Code 可以用 PreToolUse hook 讓測試指令只回傳失敗的那幾行,官方 costs 頁有現成的腳本;最簡單的做法是自己先 grep 出錯誤的那幾行再貼進去。
跑測試、抓文件、處理日誌這類輸出很長的工作交給 subagent:長輸出留在 subagent 的上下文裡,主工作階段只收摘要。這省的是主工作階段的上下文,總 token 不一定比較少。
Codex:只給相關的檔案,能縮小來源範圍就縮小。
官方原文
Usage of web search and web fetch counts toward your usage limits.asking Claude to summarize a 10,000-word article will use substantially more of your context window than a regular web search query.Instead of Claude reading a 10,000-line log file to find errors, a hook can grep for ERROR and return only matching lines, reducing context from tens of thousands of tokens to hundreds.Vague requests like "improve this codebase" trigger broad scanning. Specific requests like "add input validation to the login function in auth.ts" let Claude work efficiently with minimal file reads.Limit source material. Provide only relevant files and, when possible, narrow the sources or date range.常見誤解
- /compact 能省 token,沒事就壓一下
不是。/compact 要先把整段對話讀一遍才能寫摘要,上下文越大,這一次請求越大。它適合同一個任務做到一半、還要接著做的時候;換任務直接 /clear,那個不花 token。
- 有快取,工作階段開多久都不太花 token
不對。快取只是把重讀的單價降到約十分之一,整段歷史每則訊息仍然要讀一遍,歷史越長,這一份越大。而且訂閱方案下快取只保留 1 小時,停久了再回來,第一則訊息會依原價重算全部上下文。
- 網路搜尋很耗 token,能不搜就不搜
不是搜尋在耗。一次搜尋只帶回幾段摘要;耗的是擷取整頁、讀整個檔案、貼整段日誌,這些內容留在上下文裡,後面每一輪都跟著重讀。
小提醒
- 換任務前先用 /rename 取個名字,再 /clear;要回去就用 /resume。Codex 對應的是 /new 和 /resume。
- 同一個任務做到一半才 /compact,後面加一句要保留什麼,例如:/compact 只保留測試輸出和程式碼改動。
- 要離開 1 小時以上,走之前先把手上的事收尾;在 Pro 和 Max 上,回來時 Claude Code 提示從摘要繼續就接受,後面的訊息不用再帶完整歷史。
- Claude Code 輸入 /context、Codex 輸入 /status,隨時看上下文占了多少。
- 用完就關掉 /loop 定時任務和還沒結束的 teammate;工作階段閒置時,它們每次觸發都會帶上整段上下文。
- Claude Code 的 /usage 會在長上下文或快取未命中占近期用量 10% 以上時標出來;看到這條提示,就是該 /clear 了。