Claude Code Cost Optimization and Session Management Guide

A detailed guide on maximizing session value in Claude Code was shared, covering token price determinants, caching mechanisms, and context management. Token price is influenced by model choice, input vs output (output is ~5x more expensive), and prompt caching (cache reads at 0.1x, writes at up to 2x). Caching is broken by model, effort, fast mode, compact, or time expiry. Context accumulates all read files, command outputs, and tool definitions, and is re-sent every round. Strategies include: using @ mentions to skip file reads, adding quiet flags to noisy commands, employing subagents for isolated noisy tasks, and using /clear between tasks and /compact before leaving. A prioritized checklist ends with auditing context, setting model/effort early, and leveraging /rewind over /compact to preserve cache.
Key insights
Token Pricing Model Drives Optimization Priorities
Token cost is determined by model size, input vs output (output costs 5x due to decode per token), and thinking tokens controlled by /effort. Output includes thinking tokens, which can be disabled with MAX_THINKING_TOKENS=0 for routine tasks. Larger models amplify all other costs, so model selection is the first lever.
Caching Is Fragile: Switching Settings Mid-Session Breaks It
Prompt caching significantly reduces cost when the prefix is unchanged, but actions like changing /model, /effort, enabling fast mode, or /compact invalidate the cache, forcing a full-price prefill. Caching also expires after 1 hour (subscription) or 5 minutes (API key), though ENABLE_PROMPT_CACHING_1H=1 can extend it. Restoring old sessions almost always misses cache.
Context Accumulation Is the Hidden Cost Driver
Everything read into context—CLAUDE.md, MCP definitions, file reads, command outputs—is re-sent in every subsequent round until the session ends. Even with caching, accumulated context consumes model reasoning space and costs money. Auditing with /context, trimming CLAUDE.md, and disabling unused MCP tools reduce baseline load.
Command Outputs Under 30K Characters Are Silent Budget Killers
Outputs longer than 30,000 characters are automatically truncated to summaries, but moderate outputs like 400 lines of passing tests remain in context. The guide recommends adding quiet flags, redirecting to subagents, or using hooks to rewrite noisy commands.
Long Sessions Are More Expensive Than Splitting Tasks
Each round replays all previous rounds, making a 40-round session disproportionately expensive. Using /clear between tasks, /compact for mid-task compression, and /rewind to discard recent mistakes without breaking cache are cost-effective strategies.
Subagents Isolate Context for Noisy, High-Output Tasks
Subagents run with independent context, discarding everything except the final result. They are ideal for tasks that produce large outputs you don't need to retain, like log analysis. For fixed noisy tasks, defining a dedicated subagent with a smaller model (haiku/sonnet) is recommended.