Optimizing Claude Code Token Costs

Chase AIgo watch the original →

Claude Code token costs are driven by prompt caching behavior; understanding how to maintain cache hits and manage context bloat can reduce expenses by up to 20x.

The Breakthrough

Token costs in Claude Code are primarily determined by prompt caching efficiency, where maintaining a cache hit costs $1 per million tokens compared to $20 per million for a cache miss, representing a 20x cost difference.

Managing Cache and Context

  • Maintain Cache Continuity: The prompt cache persists for one hour of inactivity. Every message sent within that window resets the timer. Avoid actions that trigger a cache reset, such as switching models, changing effort levels, disconnecting MCP servers, or upgrading the Claude Code version.
  • Handle Cache Expiration: When a cache expires, avoid sending large context windows. Use /clear to start fresh if the codebase provides sufficient context, /compact to generate a summary of the conversation history, or custom handoff tools to save a markdown summary to disk that can be re-imported.
  • Optimize System Hygiene: Run the /doctor command to prune bloated claude.md files and remove unused skills or MCP servers. This reduces the baseline token usage that is injected into every new conversation.
  • Implement Model Routing: Use advisor patterns where a high-capability model (e.g., Claude 3.5 Sonnet or Opus) creates a plan, while smaller, cheaper models (e.g., GPT-4o-mini or local models) execute the tasks.

Before / After

  • Cache Hit Cost: $1 per million tokens.
  • Standard Input Cost (Cache Miss): $20 per million tokens.
  • Efficiency Gain: Maintaining a cache hit results in a 20x reduction in input token costs compared to a cold cache scenario.

Context

Users often inadvertently inflate costs by treating Claude Code as a stateless interface, failing to realize that every follow-up message re-sends the entire conversation history. By understanding that the system caches this history for one hour, developers can structure their workflow to keep the cache warm or use specific commands to manage context bloat when the cache inevitably resets.

Content References

  • tool: Claude Code, Anthropic, mentioned
  • tool: Ponytail, GitHub, mentioned
  • tool: Caveman, GitHub, mentioned
  • #tutorial
  • #ai
  • #dev-tooling

summary by google/gemini-3.1-flash-lite. probably wrong about something. check the source.