Maximizing Claude Code Session Value and Token Efficiency

Reducing Token Costs with Prompt Caching

Claude Code uses prompt caching to reduce the cost of repeated input tokens. When a request begins with the same tokens as a previous request, the server loads the state from the cache rather than recomputing it, reducing the cost of those tokens to 0.1x the standard input price. However, writing new tokens into the cache costs up to 2x the normal input price.

Avoiding Cache Misses

Because the cache is keyed from the start of the request forward, any change to the prefix of a request invalidates the entire subsequent cache. Users should avoid the following actions mid-conversation to prevent expensive full-context re-prefills:

  • Switching Models (/model): Each model maintains its own separate cache.
  • Changing Effort Levels (/effort): Effort levels are part of the cache key; changing them busts the cache.
  • Toggling Fast Mode: Enabling fast mode changes the cache key. It should be enabled at the start of a session.
  • Using /compact: This replaces the conversation with a shorter summary, meaning the previous conversation history no longer matches the cache.
  • Time-based Expiration: Caches expire after one hour for subscription users and five minutes for API key users (unless ENABLE_PROMPT_CACHING_1H=1 is set).

To minimize costs, users should perform model and effort changes at the start of a session or immediately after a /clear command. Using /rewind is more cost-effective than /compact for removing unwanted turns because it only cuts the end of the conversation, leaving the preceding cached history intact.

Strategic Context Management

Every file read or command output added to a session remains in the context for all subsequent turns, increasing the token load for every request. Managing what enters the context is critical for maintaining performance and reducing costs.

Optimizing Input and Tool Results

  • Use @-mentions: Mentioning a file (e.g., @utils.test.ts) attaches the file to the first request, skipping the need for a separate Read tool call and the subsequent turn it would require.
  • Quiet Command Flags: Command outputs are appended to the conversation. To prevent bloated contexts, users should add quiet flags to common commands in CLAUDE.md (e.g., using --reporter=dot for Vitest) to limit the volume of returned text.
  • Limit Startup Context: Use /context in a fresh session to identify unnecessary loaded items. Workflow-specific instructions should be moved from CLAUDE.md into skills that load only when needed, and unused MCP servers should be disabled via /mcp.

Managing Session Length

Long sessions are exponentially more expensive than multiple short sessions because each turn re-processes the entire preceding history. Users should use /clear when switching tasks and /compact when the early parts of a task are completed. For those on 1M context models, /autocompact 200k can be used to re-enable automatic compaction safety nets (available in Claude Code v2.1.221+).

Utilizing Subagents for Noisy Tasks

Subagents provide a way to execute tasks in a separate context window. A subagent has its own system prompt and tools but does not inherit the main session's conversation history. Only the final answer is returned to the main session, and all intermediate turns and tool outputs are discarded.

Subagents are ideal for "noisy" jobs, such as parsing large log files, where the intermediate output would otherwise bloat the main session's context. Users can explicitly request a subagent (e.g., "go through this log in a subagent") or define specific subagent definitions with a cheaper model like Haiku or Sonnet to further optimize costs.

Community Insights and Alternative Workflows

Users on Hacker News have suggested additional strategies to supplement the official guidance:

  • The /handoff Workflow: Some users prefer using the /handoff skill to create a portable context document. This allows them to start a fresh session with /continue [file], effectively resetting the cache and context while preserving essential project memory.
  • Verification Loops: High-efficiency users report success by having agents write tests, delete the covered code to verify the test fails (goes "red"), and then restore the code to verify it passes, ensuring tests are meaningful before human review.
  • Concerns over Manual Optimization: Some community members argue that requiring users to manually manage caches and context via commands like /clear and /compact is a regression toward formal languages and that the AI should handle these optimizations autonomously.

"I find [/handoff] much more useful than /compact or /clear because the context is saved in something portable instead of being tied to one session... I've seen better results doing this every 20 messages or so than running long sessions."

"Changing effort levels busts the cache... Couldn't effort levels be a decoding-only thing where they just change the probability of the token?"

Summary of High-Cost Areas

Priority Area Optimization Action
Highest Session Length Use /clear and /compact frequently
High Context Bloat Use @-mentions and quiet command flags
Medium Cache Misses Set /model and /effort at session start
Low Startup Load Review /context and disable unused MCP servers

Sources

Related