Claude Code 子代理缓存优化与 Opus 5.5 自动压缩技巧
Claude Code tip: if a subagent has to wait on tests or builds partway through, g…
内容结构清晰,通过对比不同场景下的缓存成本与性能,给出了具体的配置建议和代码示例,对开发者有较高的实操参考价值。
Claude Code tip: if a subagent has to wait on tests or builds partway through, give it 𝗶𝘁𝘀 𝗼𝘄𝗻 𝟭-𝗵𝗼𝘂𝗿 𝗰𝗮𝗰𝗵𝗲.
Claude Code 技巧:如果子智能体在中间阶段需要等待测试或构建,请为其设置 𝗶𝘁𝘀 𝗼𝘄𝗻 𝟭-𝗵𝗼𝘂𝗿 𝗰𝗮𝗰𝗵𝗲。
By default, a subagent's cache expires after 5 minutes with no activity. If it runs a test suite or waits on a build and sits idle for more than 5 minutes, its next step has to reprocess the whole context from scratch. That's slower than reading from the cache and counts more against your usage limits.
默认情况下,子智能体的缓存会在无活动 5 分钟后过期。如果它运行测试套件或等待构建,并且空闲超过 5 分钟,其下一步操作必须从头重新处理整个上下文。这比从缓存读取更慢,并且会消耗更多的使用额度限制。
But 𝗱𝗼𝗻'𝘁 𝘀𝘄𝗶𝘁𝗰𝗵 𝗲𝘃𝗲𝗿𝘆 𝘀𝘂𝗯𝗮𝗴𝗲𝗻𝘁 𝘁𝗼 𝟭 𝗵𝗼𝘂𝗿. The official docs say 1-hour cache writes cost more (at API prices, 2x the base input price; 5-minute writes are 1.25x). Subagents that work straight through without pausing get nothing from the longer cache and just cost more.
但 𝗱𝗼𝗻'𝘁 𝘀𝘄𝗶𝘁𝗰𝗵 𝗲𝘃𝗲𝗿𝘆 𝘀𝘂𝗯𝗮𝗴𝗲𝗻𝘁 𝘁𝗼 𝟭 𝗵𝗼𝘂𝗿。官方文档指出,1 小时缓存写入的成本更高(按 API 价格计算,是基础输入价格的 2 倍;5 分钟写入为 1.25 倍)。那些无需暂停即可连续工作的子智能体无法从更长的缓存中获益,只会增加成本。
Set it by job: → Subagents that sit idle for more than 5 minutes partway through, or that you'll resume later: add two lines at the top of the subagent's file, experimental: with cacheTtl: 1h indented under it → Ones that run straight through and finish in a few minutes: keep the default 5 minutes → If you already added subagentPromptCacheTtl to your settings: it 𝗼𝘃𝗲𝗿𝗿𝗶𝗱𝗲𝘀 𝗲𝘃𝗲𝗿𝘆 𝘀𝘂𝗯𝗮𝗴𝗲𝗻𝘁'𝘀 𝗼𝘄𝗻 𝘀𝗲𝘁𝘁𝗶𝗻𝗴 (I tested it), and it changes workflows and compaction too. To set them one by one, delete it first → On an API key, a cloud provider, or a subscription that's past its limit and drawing on usage credits: the main session only gets 5 minutes too. If you often step away mid-session, set promptCacheTtl to 1h → On a 1M-context model like Opus 5.5: by default it doesn't compact until about 967K tokens, and until then every message carries the whole conversation. Run /autocompact 400k once and it compacts at 400K
按任务进行设置: → 中途空闲超过 5 分钟或稍后恢复的子智能体:在子智能体文件的顶部添加两行,experimental: with cacheTtl: 1h 缩进在其下方 → 连续运行并在几分钟内完成的子智能体:保持默认的 5 分钟 → 如果你已经在设置中添加了 subagentPromptCacheTtl:它会 𝗼𝘃𝗲𝗿𝗿𝗶𝗱𝗲𝘀 𝗲𝘃𝗲𝗿𝘆 𝘀𝘂𝗯𝗮𝗴𝗲𝗻𝘁'𝘀 𝗼𝘄𝗻 𝘀𝗲𝘁𝘁𝗶𝗻𝗴(我测试过),同时也会影响工作流和压缩。若要逐个设置,请先删除它 → 在使用 API 密钥、云服务提供商或已超过限额并使用用量信用的订阅时:主会话也仅获得 5 分钟。如果你在会话中途经常离开,请将 promptCacheTtl 设置为 1h → 对于像 Opus 5.5 这样支持 1M 上下文的模型:默认情况下,直到约 967K token 才会进行压缩,在此之前每条消息都携带整个对话内容。运行 /autocompact 400k 一次,它将在 400K 处进行压缩
Send this prompt and the docs link below to Opus 5.5 in Claude Code 👇
将此提示词和下面的文档链接发送给 Claude Code 中的 Opus 5.5 👇
"Read this doc, plus the subagent docs it links to, and set up my cache lifetimes by job: 1. First run claude --version. Setting cacheTtl on a single subagent needs 2.1.248 or later. If mine is older, tell me. Don't upgrade it yourself. 2. List every subagent in ~/.claude/agents and .claude/agents. For ones that might sit idle for more than 5 minutes partway through (running tests, builds, waiting on CI), or that I'll resume later, add these two lines at the top of the file: experimental: cacheTtl: 1h If there's already an experimental block, add cacheTtl under it. Don't write a second one. Leave the ones that run straight through and finish in a few minutes on the default. Give one line of reasoning for each. 3. Check ~/.claude/settings.json, .claude/settings.json and .claude/settings.local.json (including their env blocks) and my current environment variables for subagentPromptCacheTtl, CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL or FORCE_PROMPT_CACHING_5M. If any of them is set, tell me it overrides the per-subagent settings and ask whether to remove it. 4. Ask me whether I'm on a subscription, an API key or a cloud provider. Don't read any keys yourself. Unless I'm on a subscription within its plan usage, ask whether I often step away for more than 5 minutes mid-session, and if I do, set promptCacheTtl to 1h. Show me what you'll change first, and don't write anything until I confirm."
"阅读本文档及其链接的子代理文档,并按作业设置我的缓存生命周期: 1. 首先运行 claude --version。在单个子代理上设置 cacheTtl 需要版本 2.1.248 或更高。如果我的版本较旧,请告诉我。不要自行升级。 2. 列出 ~/.claude/agents 和 .claude/agents 中的每个子代理。对于那些在运行中途可能闲置超过 5 分钟的(例如运行测试、构建、等待 CI),或者我稍后会恢复使用的,请在文件顶部添加以下两行: experimental: cacheTtl: 1h 如果已存在 experimental 块,请将 cacheTtl 添加在其下方。不要编写第二个 block。让那些直接运行并在几分钟内完成的保持默认值。为每个子代理提供一行推理说明。 3. 检查 ~/.claude/settings.json、.claude/settings.json 和 .claude/settings.local.json(包括它们的 env 块)以及我的当前环境变量中关于 subagentPromptCacheTtl、CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL 或 FORCE_PROMPT_CACHING_5M 的设置。如果其中任何一项已设置,请告诉我它覆盖了每个子代理的设置,并询问是否移除它。 4. 询问我是使用订阅、API 密钥还是云提供商。不要自行读取任何密钥。除非我使用的是计划用量内的订阅,否则请询问我是否在会话中途经常离开超过 5 分钟,如果是,则将 promptCacheTtl 设置为 1h。 先向我展示你将要更改的内容,在我确认之前不要写入任何内容。"
https://code.claude.com/docs/en/prompt-caching
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力