Token 知识库

额度什么时候重置、怎么省 token?按目录查阅各个主题,Codex 和 Claude 分开说明,附官方原文和链接。

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.
OpenAI Developers · Codex pricing
Codex builds an instruction chain when it starts (once per run; in the TUI this usually means once per launched session).
OpenAI docs · Custom instructions with AGENTS.md
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).
OpenAI docs · Custom instructions with AGENTS.md
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.
OpenAI docs · Custom instructions with AGENTS.md

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.
Claude Code docs · How Claude remembers your project
Size: target under 200 lines per CLAUDE.md file. Longer files consume more context and reduce adherence.
Claude Code docs · How Claude remembers your project
Claude Code sends your full conversation with every request
Claude Code docs · Manage costs effectively

从 CLAUDE.md 里挪出去的内容放哪

Claude Code 里只有两种办法是用到才加载的,其余的(包括用 @ 引入的文件)都在启动时读进去。

  1. 只在某类流程里用到的长说明(发版、评审、数据库迁移):挪进 skill,用到才加载。

  2. 只跟某些文件有关的规则:放进 .claude/rules/,并在文件开头写 paths:,Claude 读到匹配的文件才加载。

  3. 写给人看的维护备注:用 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.
Claude Code docs · Manage costs effectively
Splitting into @path imports helps organization but doesn't reduce context, since imported files load at launch.
Claude Code docs · How Claude remembers your project
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 Code docs · How Claude remembers your project

常见误解

  • 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 本身要短。

Codex 重置公告与记录 · Claude 重置公告与记录