[BUG] No postCompact hook
Resolved 💬 18 comments Opened Jul 15, 2025 by outdooricon Closed Apr 23, 2026
💡 Likely answer: A maintainer (claude[bot], contributor)
responded on this thread — see the highlighted reply below.
Environment
- Platform (select one):
- [x] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other: <!-- specify -->
- Claude CLI version: 1.0.51
- Operating System: macOS 15.5
- Terminal: ghostty
Bug Description
We need a postCompact hook. With preCompact i'm able to save relevant data to graphiti, but I need to tell claude to reference it post compact. I also need to tell claude to read it's claude md file post compact as well
Steps to Reproduce
- run
/compact - <!-- Second step -->
- <!-- And so on... -->
Expected Behavior
Makes a call to postCompact hook file
Actual Behavior
It doesn't
Additional Context
<!-- Add any other context about the problem here, such as screenshots, logs, etc. -->
18 Comments
Related to https://github.com/anthropics/claude-code/issues/3447.
I would like this so that I can inject session ID after compact.
I'd like this to get Claude to re-read certain essential rules.
Claude Code tends to start violating important instructions from CLAUDE.md (or instructions in files linked from CLAUDE.md) after a compact, so I'd like to get Claude to re-read a list of essential rules immediately after a compact completes, to ensure said rules survive in Claude's memory.
✅ Workaround Solution + Strong Support for Native PostCompact Hook
I've successfully implemented a workaround using the SessionStart hook, but I'm adding my strong support for a native PostCompact hook implementation.
🛠️ Current Workaround (SessionStart Hook)
What I implemented:
SessionStart(executes when resuming a session after auto-compact)Code:
Settings.json:
Does it work? Yes, but...
❌ Why This Workaround is Not Ideal
| Issue | Impact |
|-------|--------|
| SessionStart fires on every session start | Unnecessary overhead when no compact occurred |
| No way to detect if compact actually happened | Can't conditionally restore context |
| Stdout injection happens for normal starts | Clutters context unnecessarily |
| No environment variables about compact | Can't adapt script behavior |
| Not semantically correct | Using wrong hook for the job |
✅ Why Native PostCompact Hook is Essential
The Problem (Real-World Impact):
sedwhenRead→Editis required)The Solution (Native PostCompact):
Use Cases Beyond My Implementation:
📊 Community Demand
This issue (#3537) + duplicate #3612 show:
🎯 Implementation Proposal
Minimal viable implementation:
Required behavior:
CLAUDE_COMPACT_TYPE,CLAUDE_COMPACT_TIMESTAMP,CLAUDE_PROJECT_PATH🚀 Impact on Claude Code Adoption
With current workaround: "Claude Code works but loses context frequently"
With native PostCompact: "Claude Code maintains context seamlessly"
This single feature would:
📢 Strong +1 from Professional User
As someone who's implemented the workaround and uses Claude Code professionally, I strongly support native PostCompact hook implementation.
Priority: HIGH (affects all users who hit auto-compact)
Implementation effort: LOW (PreCompact already exists, same pattern)
User impact: MASSIVE (transforms workflow quality)
---
My setup:
Thank you Anthropic team for considering this feature! 🙏
Use case: External memory system for context recovery
I've built a "context map" system - a machine-optimized markdown file (
.claude/context-map.md) that serves as Claude's external memory. It contains compressed project state: recent git commits, active tasks, solved problems, code paths, and anti-patterns.Current setup:
SessionStarthook: generates context-map AND cats it into Claude's contextPreCompacthook: regenerates context-map (captures current state before it's lost)The gap:
PreCompact fires before compaction, so if I cat the file during PreCompact, that output gets compacted away - defeating the purpose. The regenerated file exists on disk but Claude can't see it after waking up from compaction.
SessionStart works great for fresh sessions, but after mid-session compaction, Claude loses context and has no automatic way to reload the context-map.
Workaround:
Currently relying on the conversation summary to mention "read context-map.md" - fragile and depends on summary quality.
What PostCompact would enable:
After compaction completes, Claude would automatically receive the freshly-generated context-map contents, restoring project awareness without manual intervention.
Broader point: CLAUDE.md re-reads
This same mechanism should probably be built into Claude Code itself - automatically re-reading CLAUDE.md after compaction. Reddit is full of users frustrated that Claude "forgets" instructions from CLAUDE.md after compaction. A PostCompact hook would let power users solve this themselves, but automatic CLAUDE.md re-injection after compaction seems like it should be default behavior.
This seems like a straightforward addition given PreCompact already exists - just fire the same hook mechanism after compaction instead of before.
Supporting this feature request with a comprehensive workaround example
I've built a more elaborate system to solve this exact problem. Here's what works (and what doesn't):
My Current Setup
1. SessionStart Hook (similar to yours):
2. The hook script detects the current project and injects:
~/.claude/POST-COMPACT-README.md(global rules - 400+ lines).claude/POST-COMPACT.md(project-specific rules).claude/CLAUDE.md(project config)3. Skills auto-loading (
~/.claude/skills/*/Skill.md):seo-specialist,no-sed-awk,curl-verificationWhat Works
✅ Fresh sessions: Claude starts with full context (rules, skills, project state)
✅ Git as memory: Commits serve as recoverable context (
git log -5)✅ Structured POST-COMPACT files: Checklists, rules, and "read this first" instructions
What Doesn't Work (The Gap)
❌ Mid-session /compact: Everything gets lost. The SessionStart hook doesn't fire.
❌ Skills disappear: Even registered skills vanish after compact
❌ Relying on summary: As you said, fragile and depends on summary quality
The Real Problem
The only way Claude sees the context is if the summary happens to mention "read POST-COMPACT.md" - which is unreliable.
Why PostCompact Would Solve This
With a PostCompact hook, we could:
This would guarantee context restoration after every compact, not just on fresh sessions.
Agreeing with the Broader Point
100% agree. The number of times I've had to tell Claude "read CLAUDE.md again" after a compact is frustrating. This should be automatic.
Suggested Implementation
~/.claude/skills/)This would make Claude Code significantly more reliable for long sessions and complex projects.
---
TL;DR: I have a working workaround with SessionStart + POST-COMPACT files + skills, but it all breaks on mid-session /compact. PostCompact hook would elegantly solve this for everyone.
Update: Improved PreCompact Hook Strategy
Following up on my previous comment, I've implemented additional improvements that make the workaround more robust:
New Multi-Layer Recovery System
Layer 1: Enhanced PreCompact Hook
The PreCompact hook now does two things:
.claude/last-context.md) with recent git activityThe theory: By repeating the instruction multiple times AND explicitly telling the summarizer to include it, we increase the probability it survives in the compact summary.
Layer 2: Prominent CLAUDE.md Recovery Section
Added a highly visible section at the TOP of project CLAUDE.md:
Since CLAUDE.md is automatically re-read, Claude sees this instruction first.
Layer 3: SessionStart Hook (unchanged)
Still works for fresh sessions and resumed sessions.
Results
| Scenario | Before | After |
|----------|--------|-------|
| Fresh session | ✅ Full context | ✅ Full context |
| Resume session | ✅ Full context | ✅ Full context |
| Mid-session /compact | ❌ Lost context | ⚠️ Better chance of recovery |
Still Not Perfect
The mid-session compact is still problematic because:
Conclusion
This multi-layer approach is the most robust workaround possible with current hooks, but it's still a workaround. A native PostCompact hook would eliminate all this complexity and provide guaranteed context restoration.
The workaround code is available at: my claude-config repo (if others want to adapt it)
---
TL;DR: Improved PreCompact hook + prominent CLAUDE.md section = better odds of surviving mid-session compact, but still not guaranteed. Native PostCompact hook remains the proper solution.
I also would like this feature. I like the workaround, but it's definitely not guaranteed that it will be triggered
This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.
I'd like to hear an argument from the dev team against having the postCompact hook.
It's really just a logical addition that makes the set of hooks available to us complete and is a huge win for fine tuning and control during loops to inject additional context or run scripts post compact.
FWIW, I am developing a simple utility that keeps watch of Claude Code activity (a notification/tracking system) and the lack of a PostCompact hook is proving to be a blocker for some functionality I'm working on.
Consider this a vote in favor of this functionality.
Please add a "PostCompact" hook. This is absolutely needed. I cannot believe we don't have this for some reason. It would be incredibly useful to run a hook after a conversation is compacted.
For example, I have a skills file on how I like to do certain things, every time the conversation is compacted that skills file is removed from context and I have to reload it manually.
Still this issue. i wonder how hard could be to trigger a hook after compact?!
+1 .Just stumbled across this, thanks for reporting and also the workarounds. A
postCompacthook would be a wonderful addition to customize Claude Code by a user.Its almost they want you to spend more tokens trying to fix halucinations.... well well
in case these issue reviews are automated, I'll restate the case for our reader:
Feature request:
Add a PostCompact hook event that fires after context compaction completes.
Motivation
Currently, PreCompact is the only compaction-related hook. It fires before summarization, so hook output gets fed into the summarizer as input. The summarizer then decides what to retain — and behavioral
instructions injected via PreCompact often get compressed or dropped.
My use case: I inject workflow context (task tracker commands, behavioral rules) via a PreCompact hook. After compaction, the agent frequently "forgets" these instructions because the summarizer didn't
prioritize them. I end up manually re-stating the instructions.
A PostCompact hook would inject content after the summary, so it appears at full fidelity in the new context window — exactly like SessionStart does for fresh sessions.
Proposed Behavior
Workarounds Considered
PostCompact would close the gap for dynamic context that needs to survive compaction reliably.
This issue was fixed as of version 2.1.76.
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.