Strategic Compaction
Overview
Strategic compaction suggests manual /compact at logical workflow boundaries rather than relying on arbitrary auto-compaction. This preserves critical context through task phases and prevents mid-task context loss during complex multi-step operations.
How It Works
The suggest-compact.sh script runs on PreToolUse (Edit/Write) and:
- Tracks tool calls — counts invocations in session
- Threshold detection — suggests at configurable threshold (default: 50 calls)
- Periodic reminders — reminds every 25 calls after threshold
Hook Setup
{
"hooks": {
"PreToolUse": [{
"matcher": "tool == \"Edit\" || tool == \"Write\"",
"hooks": [{
"type": "command",
"command": "~/.claude/skills/strategic-compact/suggest-compact.sh"
}]
}]
}
}
Configuration
COMPACT_THRESHOLDenv var — tool calls before first suggestion (default: 50)
When to Compact
| Timing | Reason |
|---|---|
| After planning phase | Plan is finalized, start fresh for implementation |
| After debugging session | Clear error-resolution context |
| After completing a milestone | Free context space for next phase |
| Before task type switch | Avoid irrelevant context interfering |
| Never mid-implementation | Would lose critical working context |
Best Practices
- Compact after planning — once plan is finalized, compact to start fresh
- Compact after debugging — clear error-resolution context before continuing
- Don't compact mid-implementation — preserve context for related changes
- Read the suggestion — the hook tells you when, you decide if