会话越长越费 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 了。