/compact at logical intervals to preserve context through task phases, rather than auto-compacting at arbitrary points.
Why Manual Over Auto-Compact?
Timing Control
Auto-compact happens at arbitrary points, often mid-task
Context Preservation
Strategic compacting preserves context through logical phases
Phase Transitions
Compact after exploration, before execution
Milestone Boundaries
Compact after completing a milestone, before starting next
How It Works
The strategic compact hook tracks tool call count and suggests manual compaction at:- Threshold reached (default: 50 tool calls)
- Regular intervals after threshold (every 25 calls)
Tracking Tool Calls
Suggestion Logic
Hook Configuration
hooks.json
Environment Configuration
number
default:"50"
Number of tool calls before suggesting compaction (range: 1-10000)
string
default:"default"
Session identifier for tracking tool counts across sessions
Example
PreCompact Hook
Runs before context compaction to save state that might be lost during summarization.What It Does
Log Compaction Event
Log Compaction Event
Records the compaction event with timestamp in a compaction log file.
Mark Active Session
Mark Active Session
If there’s an active session file, adds a marker noting when compaction occurred.
Hook Configuration
hooks.json
Best Practices
1
Exploration Phase
Let tool count build up during exploration (reading files, searching code)
2
Before Execution
Run
/compact before writing code to free up context for implementation3
After Milestones
Compact after completing a feature or fixing a bug
4
Before Planning
Compact before starting a new feature or major refactoring
Compaction Log
Track all compaction events:Related
- Session Lifecycle — Session start and end hooks
- Hook Overview — Complete hook system reference