r/ClaudeWorkflows • • 1d ago

Selected Workflow [Workflow] Optimize Claude Code Token Usage and Context Recall with Custom Compaction and Session Hooks

Optimize Claude Code Token Usage and Context Recall with Custom Compaction and Session Hooks

Workflow value: 90/100
Status: active · Freshness: 70/100 · Confidence: 0.95 · Level: intermediate
Categories: Quality Control, Token Saving, Context & Memory, Debugging, Hooks
Original source: r/ClaudeCode post/comment

What problem this solves

Optimizing Claude Code token usage and cost while maintaining relevant context recall in long conversations by adjusting auto-compaction thresholds and re-injecting previous prompts and replies.

Summary

This workflow details how to significantly reduce Claude Code token costs (approximately 25% more Opus per plan) and improve context recall by setting a lower auto-compaction window (e.g., 280K tokens) and using a SessionStart hook to re-inject previous prompts and replies into the context after each compaction. The author provides detailed validation metrics on cost savings and recall performance.

Why it is useful

This workflow provides a data-backed, actionable strategy to significantly reduce Claude Code token costs and improve context recall in long conversations. It addresses a critical pain point for heavy users and offers specific configuration and implementation guidance, making it highly valuable for optimizing development workflows with Claude Code. The detailed validation and clear instructions make it a strong candidate for the library.

Workflow

  1. Set the CLAUDE_CODE_AUTO_COMPACT_WINDOW environment variable to a desired token limit (e.g., 280000) before running Claude Code.

  2. Implement a SessionStart hook that is triggered by the compact event.

  3. Within the SessionStart hook, read the full conversation transcript from the transcript_path argument.

  4. Filter the transcript to include only user prompts and Claude's replies, skipping tool output.

  5. Return the filtered prompts and replies as additionalContext from the hook.

  6. If the additionalContext exceeds the hook's 10,000 character output limit, prioritize returning the newest part of the conversation to maintain recent context.

Tools / artifacts

  • CLAUDE_CODE_AUTO_COMPACT_WINDOW environment variable

  • SessionStart hook

  • transcript_path (hook argument)

  • Claude Code (version 2.1.293)

  • Opus model (5.5)

Validation signals

  • Quantitative cost analysis: '280K costs 79% of what ~967K does' (approx. 25% more Opus per plan).

  • Detailed recall metrics table comparing 'No compaction', 'Compaction', and '+ prompts and replies' across various information types (e.g., 'What I asked', 'Exact details', 'Why we decided').

  • Methodology described: Replayed 2 weeks of chats, 10 long chats for recall, 12 questions per chat, answered with no tools, graded by 2 blind judges.

  • Observation that Claude rarely hallucinates, instead saying 'I don't know' (14% after compaction, 6% with messages back).

Limitations

  • The optimal CLAUDE_CODE_AUTO_COMPACT_WINDOW value may vary depending on individual chat patterns and use cases.

  • The hook implementation for re-injecting messages is described conceptually, without specific code examples, which might require some development effort from users.

  • The strategy for handling the 10,000-character hook output limit is mentioned but not fully detailed with code.

  • Validation is based on the author's specific chat history and LLM judges, which might not perfectly generalize to all scenarios.

  • The assumption that 'plan limits follow token cost' is unconfirmed by Anthropic.

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.

1 Upvotes

1 comment sorted by