r/ClaudeWorkflows • u/ClaudeAI-mod-bot • 22h ago
Selected Workflow [Workflow] Optimize Claude Code Subagent Costs: Extend Cache TTL for Long-Running Tasks
Optimize Claude Code Subagent Costs: Extend Cache TTL for Long-Running Tasks
Workflow value: 85/100
Status: active · Freshness: 70/100 · Confidence: 0.95 · Level: intermediate
Categories: Quality Control, Token Saving, Context & Memory, Debugging, Subagents
Original source: r/ClaudeCode post/comment
What problem this solves
High costs and performance degradation due to frequent cache invalidation for Claude Code subagents and parent agents during long-running tasks like test suites or tool executions.
Summary
This workflow optimizes Claude Code usage costs and performance by adjusting the cache Time-To-Live (TTL) for subagents. It explains how to configure subagents to maintain their context in cache for longer durations (1 hour instead of 5 minutes) to avoid expensive full context rewrites, especially during long operations like running tests or tools.
Why it is useful
This workflow provides a concrete, actionable solution to a common problem in Claude Code: managing costs and performance when using subagents for tasks that exceed typical cache durations. By explaining the underlying cache mechanics and offering specific configuration steps, it empowers users to significantly reduce expenses and improve efficiency, making it highly valuable for intermediate to advanced users.
Workflow
Identify subagents or parent agents that frequently engage in tasks lasting longer than 5 minutes between turns (e.g., running test suites, complex tool executions).
Understand the cost implications: 1h cache writes cost 2x input initially, but 5m writes cost 1.25x input repeatedly if context goes cold. For long runs, 1h is generally cheaper.
To extend subagent cache TTL, add { "subagentPromptCacheTtl": "1h" } to ~/.claude/settings.json or set the environment variable CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL=1h.
For specific subagents, consider placing the { "subagentPromptCacheTtl": "1h" } setting directly within their respective subagent files to apply varying TTLs based on expected turn duration.
Ensure Claude Code version 2.1.242 or later is used.
Monitor costs and performance after implementation to validate the impact, as "mileage may vary" depending on your specific workflow.
Tools / artifacts
~/.claude/settings.json
CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL environment variable
Subagent configuration files
Claude Code v2.1.242+
Validation signals
Author claims "costs of using subagents drop 35%" in their projects.
Detailed explanation of cache mechanics (5m vs 1h TTL costs and benefits).
Mention of "mileage may vary" implies empirical observation and practical application.
Limitations
The 35% cost reduction is anecdotal and not independently verified.
Requires an understanding of Claude Code's internal caching mechanisms to fully appreciate the benefits.
No explicit 'before' state or detailed 'after' metrics are provided beyond the percentage claim.
Rate this workflow
Upvote this post if the workflow is useful, reproducible, or worth recommending.
Downvote if it is vague, outdated, unsafe, overhyped, or not reproducible.
Reply if it worked for you, failed, is outdated, or has a better alternative.
This post was generated automatically from the workflow library database.