Claude Code 的 token budget 机制

当用户用特定格式显式指定一个输出 token 目标时,Claude Code 会把它当成最低工作量目标,持续工作并在接近该数量前尽量不提前停止。即使停止也会唤起一轮对话继续。

用户输入里如果写了 +500k、spend 2M tokens、use 1B tokens,就会被解析成 token budget。

具体来说,用的是正则匹配,所以需要特定格式。

特定格式指的是:

句首句尾检查是否是以 +数字(+)单位 比如 +500k

以及检查整句有没有 use/spend 500m 这样的字样

claude code 正则匹配到后,会加一段prompt,通过这个prompt就能很清楚的看到机制是什么

When the user specifies a token target (e.g., "+500k", "spend 2M tokens", "use 1B tokens"), your output token count will be shown each turn. Keep working until you approach the target — plan your work to fill it productively. The target is a hard minimum, not a suggestion. If you stop early, the system will automatically continue you.

判断条件是 COMPLETION_THRESHOLD = 0.9,只要使用的token到90%以上就算接近目标,可以停止自动续写。

如果继续下去收益很低,也会提前停止。如果连续几次新增token太少,会判定为diminishingReturns。