claude-code - 💡(How to fix) Fix Feature request: PreCompact hook fired at configurable context threshold

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Add a PreCompact hook (or a context-threshold hook) that fires when the context window reaches a configurable percentage (e.g. 90%), giving agents and users a chance to save state before auto-compaction discards mid-session work.

Root Cause

Add a PreCompact hook (or a context-threshold hook) that fires when the context window reaches a configurable percentage (e.g. 90%), giving agents and users a chance to save state before auto-compaction discards mid-session work.

Fix Action

Fix / Workaround

Workaround today

Code Example

{
  "hooks": {
    "PreCompact": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "chezmoi apply && echo 'Memory checkpoint saved'"
          }
        ]
      }
    ]
  }
}

---

{
  "contextThreshold": 90
}
RAW_BUFFERClick to expand / collapse

Summary

Add a PreCompact hook (or a context-threshold hook) that fires when the context window reaches a configurable percentage (e.g. 90%), giving agents and users a chance to save state before auto-compaction discards mid-session work.

Problem

When Claude Code auto-compacts a long session, any in-flight state — memory file writes, task updates, checkpoint notes — that hasn't been committed yet is lost. There is currently no way to detect "I'm near the context limit" from within a hook, so nothing can be automated to save state before the compaction happens.

The existing hooks (PreToolUse, PostToolUse, Stop) don't expose context window usage, so there's no way to trigger behavior at a specific threshold.

Proposed solution

A new hook event, PreCompact, that fires before auto-compaction begins. Configuration could look like:

{
  "hooks": {
    "PreCompact": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "chezmoi apply && echo 'Memory checkpoint saved'"
          }
        ]
      }
    ]
  }
}

Optionally, a contextThreshold setting (0–100) could control when the hook fires, e.g.:

{
  "contextThreshold": 90
}

Use cases

  • Auto-save memory/notes files to git before context is summarized
  • Notify the user that context is near limit so they can run /compact manually at a good stopping point
  • Allow agents doing long multi-step tasks to write a checkpoint before state is lost

Workaround today

A Stop hook running chezmoi apply partially mitigates this — it flushes any memory files written during the turn to git after each response. But it doesn't help if the agent is mid-turn when compaction is triggered, and it doesn't give the agent itself a chance to write a checkpoint summary first.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Feature request: PreCompact hook fired at configurable context threshold