PreCompact hooks need output control: replaceCompactSummary and compactNumber
Problem
The PreCompact hook event fires before context compaction, but the hook's output has no way to control what gets injected into the post-compact context. Specifically:
- No way to replace the default summary. Custom extraction systems (external LLMs, database-backed state preservation) produce better-structured context than the default narrative summary, but their output is additive — the default summary still consumes tokens. There's no
replaceCompactSummaryorskipDefaultSummaryoutput field.
- No compact counter. There's no way to know how many times a session has compacted. This is critical for multi-compact sessions where you need to weight recent decisions higher and archive older ones. A simple
compactNumber: Nin the hook input or post-compact context would enable this.
- Duplicate of locked issue. This was previously requested in #13170, which was auto-closed as a duplicate of #3349, which is itself now closed and locked with zero Anthropic response. The feature request has nowhere to live.
Proposed Solution
1. replaceCompactSummary output field
{
"additionalContext": "...",
"replaceCompactSummary": true
}
When replaceCompactSummary: true, the hook's additionalContext becomes the only context injected post-compact, replacing the default auto-generated summary. This lets power users control their own compaction strategy.
2. compactNumber in hook input
{
"hook_event": "PreCompact",
"compactNumber": 3,
"transcript_path": "..."
}
Sequential compact counter for the current session. Trivial to implement (increment a session-scoped integer), transformative for multi-compact context management.
3. compactSummary in hook input
{
"hook_event": "PreCompact",
"compactSummary": "The default summary that would be injected..."
}
Give the hook access to the default summary so it can augment, restructure, or selectively include parts of it.
Why This Matters
I've built and maintained a production PreCompact system for 6 weeks (100+ sessions). The results are dramatic:
- Task continuity after compact: ~40% (default) → ~95% (structured extraction)
- CLAUDE.md rule retention: Degrades after 3+ compacts → 100% retention
- Recovery turns needed: 3-8 → 0-1
But the system wastes tokens because the default summary is always injected alongside our structured context. With replaceCompactSummary, we could save 2-4K tokens per compaction — significant in sessions that compact 3-5 times.
Accessibility Impact
Context loss after compaction disproportionately affects neuroatypical users (ADHD, executive function differences) who rely on Claude as a cognitive scaffold. When Claude forgets mid-task, these users bear the highest cognitive load to recover. Better compaction control makes Claude Code more accessible. (Related: #24949 on table rendering accessibility.)
Environment
- Claude Code version: latest (2026-02)
- OS: macOS 15.3
- Shell: zsh
Workaround
Currently using external LLM extraction + PostgreSQL persistence + additionalContext injection. Works but wastes tokens due to additive-only output.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗