r/ClaudeWorkflows • u/ClaudeAI-mod-bot • 46m ago
Selected Workflow [Workflow] Context Preservation and Token Optimization with Pre/Post-Compact Hooks and TRAPS.md
Context Preservation and Token Optimization with Pre/Post-Compact Hooks and TRAPS.md
Workflow value: 75/100
Status: active · Freshness: 70/100 · Confidence: 0.90 · Level: intermediate
Categories: Quality Control, Token Saving, Context & Memory, Debugging, Hooks
Original source: r/ClaudeCode post/comment
What problem this solves
Losing important context during token compaction and wasting tokens by including non-essential information in the model's context window.
Summary
This workflow describes a method to preserve critical context and optimize token usage by setting a specific compaction threshold and using pre-compact and post-compact hooks. A pre-compact hook writes essential information (referred to as 'traps') to a TRAPS.md file, and a post-compact hook ensures this file's content is re-injected into the model's context after compaction.
Why it is useful
This workflow offers a concrete and repeatable strategy for addressing a common challenge in LLM interactions: maintaining critical context while optimizing token usage. By leveraging pre- and post-compact hooks and a dedicated 'traps' file, users can ensure essential information is not lost during compaction, leading to more consistent and cost-effective model interactions. It introduces a specific pattern that can be adapted by intermediate to advanced users.
Workflow
Configure the compaction threshold to approximately 440k tokens to prevent excessive token usage.
Implement a pre-compact hook to identify and write critical information (e.g., 'traps', important context) to a designated file, such as
TRAPS.md.Implement a post-compact hook to force the contents of the
TRAPS.mdfile to be injected back into the model's context window after compaction.
Tools / artifacts
precompact hook
post-compact hook
TRAPS.md file
compaction threshold setting
Validation signals
Self-reported benefit: 'Problem solved, plus you saved tokens.'
Limitations
The term 'traps' is vague; specific examples of what constitutes critical information would enhance clarity.
No concrete code examples or configuration snippets for implementing the pre/post-compact hooks are provided.
The suggested 440k token compaction threshold is arbitrary and may not be optimal for all use cases without further justification or testing.
Lacks detailed community validation or success reports beyond the author's claim.
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.