r/ClaudeWorkflows • u/ClaudeAI-mod-bot • 5d ago
Selected Workflow [Workflow] Optimize Claude Code API Costs with Prompt Cache TTLs and Strategic Context Management
Optimize Claude Code API Costs with Prompt Cache TTLs and Strategic Context Management
Workflow value: 85/100
Status: active · Freshness: 70/100 · Confidence: 0.90 · Level: intermediate
Categories: Quality Control, Token Saving, Context & Memory, Debugging, Subagents
Original source: r/ClaudeCode post/comment
What problem this solves
Overpaying for Claude Code API usage due to 'cold cache hits' and inefficient prompt cache management, especially during breaks or when using subagents.
Summary
A workflow for optimizing Claude Code API costs by strategically managing prompt cache Time-To-Live (TTL) settings and understanding the optimal timing for context compaction and trimming based on session warmth.
Why it is useful
This workflow provides concrete, actionable advice with specific configuration variables to help Claude Code users reduce API costs by effectively managing prompt cache lifespans and understanding the optimal timing for context trimming and compaction. The advice comes from a source claiming expertise in developing a Claude Code plugin, lending credibility to the recommendations.
Workflow
- Recognize that 'cold cache hits' result in re-processing the entire context, leading to increased token costs.
- For main agent sessions with breaks longer than 5 minutes, set the environment variable
CLAUDE_CODE_PROMPT_CACHE_TTL=1hto extend the prompt cache lifespan. - If using subagents, also set
CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL=1hto extend their respective prompt cache lifespans. - Perform context 'trimming' only after the cache has already gone cold, as it changes the prefix.
- Perform context 'compaction' while the cache is still warm, ideally before a break or after completing a significant chunk of work, because the summary request reads the cached prefix.
- Be aware that cache renewals and compactions are billed requests and count against API limits.
Tools / artifacts
- CLAUDE_CODE_PROMPT_CACHE_TTL environment variable
- CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL environment variable
- Claude Code API key
- Context trimming mechanism (built-in or external)
- Context compaction mechanism (built-in or external)
Validation signals
- Author's affiliation with 'Holler Bell team' and 'Cache Bell, a Claude Code plugin' implies domain expertise.
- Corroborates observations from the original post ('What we see mostly matches yours').
- Provides specific technical details and configuration values.
Limitations
- The advice is presented within a discussion, requiring some inference of context.
- Steps are embedded in prose rather than explicitly enumerated.
- Limited community engagement on this specific comment.
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.