Feature Request: Add PreCompact and PostCompact hooks for custom context management
Description
I'd like to request hooks that fire before and after Claude Code's autocompact process, allowing users to run custom logic when context is being summarized.
Use Case
When building systems that need long-term memory across sessions (personal knowledge bases, project documentation, ops tooling), valuable structured data gets lost during autocompact summarization. Things like:
- Key decisions made during the session
- IP addresses, device info, or other structured data discovered
- Code snippets worth preserving
- Conclusions and root causes identified
Currently there's no way to extract or preserve this data before it gets summarized into prose.
Proposed Solution
Add PreCompact and PostCompact hooks similar to existing PreToolUse/PostToolUse hooks:
{
"hooks": {
"PreCompact": [
{
"command": "/path/to/extract-before-compact.sh"
}
],
"PostCompact": [
{
"command": "/path/to/after-compact.sh"
}
]
}
}
PreCompact hook input (JSON via stdin):
{
"messages_to_compact": [...],
"current_summary": "...",
"session_id": "...",
"cwd": "..."
}
PreCompact hook output (optional, via stdout):
{
"inject_into_summary": "Additional context to preserve: ...",
"extracted_data": {...}
}
Benefits
- Users can extract structured data before summarization loses it
- Enables integration with external memory/knowledge systems
- Allows project-specific context preservation (security ops extracts IPs/MACs, dev projects extract function signatures)
- Gives users control over what context survives compaction
- Consistent with existing hook architecture
Workaround
Currently using SessionEnd hooks to parse transcripts after the fact, but this misses data already lost to earlier autocompacts during long sessions.
Showing cached comments. Read the full discussion on GitHub ↗
8 Comments
What i think would be really helpful would be to have a CompactPrompt hook, for injecting content into the compacted context..
We actually need a more general version of this request, a way to see and manage the contents of the context window.. not just at the point of compaction, but at any point in time.
See my request: https://github.com/anthropics/claude-code/issues/20652
Building a persistent memory system for Claude Code. Compaction destroys structured data before I can extract it.
Current workaround: SessionStart:compact hook re-injects context AFTER - but too late, data's already gone.
PreCompact would let me save what matters before summarization.
(This comment drafted with Claude Code - yes, I use it to manage my own GitHub issues 🙂)
+1 for PostCompact. My use case is simple: sending a push notification (via ntfy.sh) when compaction finishes so I know I can come back to the session. Compaction takes a long time and there's no way to get notified when it's done.
The current workaround is chaining PreCompact (to set a flag file) with a Stop hook (to check the flag and send the notification), but the timing is off — Stop fires after Claude's next response, not when compaction actually finishes.
A PostCompact hook would make this trivial.
Confirming: PreCompact + SessionStart(compact) is insufficient — Feb 17, 2026
We've implemented exactly the PreCompact/PostCompact pattern described in this issue using the available hooks. Full pipeline:
Result: Claude ignores the SessionStart output. The compaction summary momentum wins. Detailed evidence in #9796.
The core ask here — proper PostCompact hooks with system-level injection — is the right solution. The current SessionStart(compact) hook injects as user context, which Claude deprioritizes relative to the compaction summary.
Until official hooks land, I built cozempic which does something similar via file-system watching — it monitors session JSONL files and proactively cleans bloat before compaction triggers. The guard daemon uses kqueue on macOS (polling fallback elsewhere) to detect writes and apply cleaning strategies automatically. It also has agent team state preservation so subagent context survives the prune. Might be useful as a bridge until native hooks arrive.
+1 on this. I use an MCP-based memory system (Serena) to persist session context across conversations. Today I have to manually run a save routine (/bye) before context fills up — a PreCompact hook would let this happen automatically. The gap between "auto-compaction is automatic" and "no automatic way to save before it fires" is the exact pain point.
Related: #29957 proposes a proactive solution to this — topic-aware fork prompts to prevent context bloat, overflow-to-file recovery instead of data loss on compaction, and graduated warnings before the emergency. If this problem matters to you, a thumbs-up there would help consolidate the signal.