Reduce Rewrite Costs in Claude Code by Configuring Sub-agent Cache TTL
Key point
An optimization method has been released to reduce context rewrite costs after long tool calls by changing the default 5-minute cache TTL for Claude Code sub-agents to 1 hour.
Details
In Claude Code subscription plans, the prompt cache TTL for sub-agents is set to 5 minutes by default, causing the cache to expire after tool calls that take longer than 5 minutes, such as builds or tests. This forces the entire context to be rewritten (cache-write) at a higher cost for the next request, rapidly consuming the 5-hour usage window.
Cost Structure and Root Cause
According to Anthropic's pricing policy, a 5-minute cache write costs 1.25x standard input, while a 1-hour cache write costs 2x. If the cache expires while a sub-agent is waiting for a long tool call, the context—often hundreds of thousands of tokens—must be rewritten, causing costs to surge. In a real-world case, one sub-agent performed 8 rewrites per day, resulting in approximately 5.4 million tokens of cache writes.
Optimization Settings and Effects
The solution is to add "subagentPromptCacheTtl": "1h" to ~/.claude/settings.json or set the environment variable CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL=1h. (Requires Claude Code v2.1.242 or later)
- Cost Reduction: After the configuration change, cache writes for 5 sub-agents remained at the level of 3 million tokens, and most agents did not experience large-scale rewrites after the initial request.
- Usage Improvement: Usage of the 5-hour window was previously consumed to 100% by 4 sub-agents, but after the fix, it only reached 22%, representing a significant decrease.
- Rewrite Prevention: A review sub-agent performed 147 turns over 80 minutes with 0 rewrites recorded.
Caveats and Limitations
Even with a 1-hour TTL, the cache can expire during long tasks. Therefore, long tasks should be run in the background, with results saved to files and checked periodically to keep the cache warm. Additionally, short sub-agents may incur an additional cost equivalent to approximately 0.75 context units due to the 1-hour setting, making it beneficial only if more than 2 rewrites are expected.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.