Strategic Compaction

中级 Intermediate 纪律型 Discipline ⚡ Claude Code 专属 ⚡ Claude Code Optimized
1 min read · 73 lines

Compact context at logical boundaries to preserve critical information across task phases

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:

  1. Tracks tool calls — counts invocations in session
  2. Threshold detection — suggests at configurable threshold (default: 50 calls)
  3. 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_THRESHOLD env 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

  1. Compact after planning — once plan is finalized, compact to start fresh
  2. Compact after debugging — clear error-resolution context before continuing
  3. Don't compact mid-implementation — preserve context for related changes
  4. Read the suggestion — the hook tells you when, you decide if

相关技能 Related Skills