You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature Request] Support custom pre-compact, compact, and post-compact commands to run instead of default after context window is 95% full #3349
I would personally love the option to define my own replacement for auto-compaction as a series of commands.
This could be generally automated as 3 customize-able commands with defaults:
Pre-compact (default: none)
Compact (default: /compact)
Post-compact (default: none)
Bonus: set the percentage remaining in context window at which it gets triggered.
I have had a lot of problems with the results of compacting. Quality of progress drops, and /compact takes a lot of time while burning a lot of tokens. My projects require a carefully-loaded github-issue-specific context to achieve good results with CC, so I just need to rebuild my context manually whenever I see an upcoming auto-compact event.
changed the title [-][Feature Request] Set custom commands to run instead of /compact after context window is 95% full[/-][+][Feature Request] Set custom pre-compact, compact, and post-compact commands to run instead of default after context window is 95% full[/+]on Jul 11, 2025
changed the title [-][Feature Request] Set custom pre-compact, compact, and post-compact commands to run instead of default after context window is 95% full[/-][+][Feature Request] Support custom pre-compact, compact, and post-compact commands to run instead of default after context window is 95% full[/+]on Jul 11, 2025
Hey, this is a really interesting and well-thought-out feature request. I've definitely had similar frustrations where /compact loses the specific context I've carefully built up for a task.
The good news is that the dev team seems to have anticipated this need, and there's a feature that gets you about 90% of the way there already: Hooks. Specifically, the PreCompact hook.
Based on the Hooks Guide and the Hooks Reference, you can set up a script to run automatically right before a compaction event happens.
Here’s how you could build the workflow you're describing:
Create a PreCompact Hook:
You'd register a custom script to run on the PreCompact event. The hook even receives a trigger field in its JSON input, so your script will know if it was triggered by "auto" (the 95% threshold) or "manual" (a user running /compact). This directly addresses your need to handle the auto-compact event specifically.
Build Your Custom "Compaction" Logic:
Your script would receive the path to the current conversation transcript. It could then:
Parse the transcript to identify the key context (like the GitHub issue IDs you mentioned).
Use the gh CLI or a GitHub MCP server to fetch the latest, most relevant details for those issues.
Generate a new, condensed summary prompt that preserves the exact context you need for your work.
Preventing the Default /compact:
This is the key part. While the docs aren't super explicit about this for PreCompact, the "Advanced JSON Output" section for hooks mentions a common field: {"continue": false}. My interpretation is that you could have your script output this JSON to stdout. This should, in theory, stop Claude Code from proceeding with the default, built-in compaction after your hook runs.
Injecting the New Context (Post-Compact):
While there isn't a PostCompact hook, you can build this into your PreCompact script. After generating the new context, your script could print a message to the user like:
Context window full. Default compaction stopped.
Run '/clear' and then paste the following to continue with a fresh context:
"Based on GitHub issue #1234, which is about [...], and the current state of files X and Y, let's continue working on the solution."
It's not fully automated, but it turns a manual, multi-step research process into a single-step "copy-paste" action prompted by your own custom logic.
Here's how you might configure it in your .claude/settings.json:
{
"hooks": {
"PreCompact": [
{
"matcher": "auto", // Only run for automatic compaction"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/rebuild-context.sh"
}
]
}
]
}
}
While you can't replace the /compact command itself or add a post-compact hook directly, the PreCompact hook seems powerful enough to achieve your goal of taking control over context management when the window gets full.
Hope this helps! Let us know if you get a script working, I'd be really interested to see it.
Agree with this 100%. We need much better context management options that the current auto-compact. I have not heard anyone do anything but complain about this feature! I understand it's a cost savings thing, and anthropic is having some serious problems sustaining the current paradigm.. as the ever shrinking quotas reveal.. but auto-compaction is not a good solution. This Enhancement Request and suggestions by @coygeek offers much more useful solution... putting tools into the the community's hands to find the solution to a problem that 100% of the users experience.
My current approach is to have claude output its progress on the current task to a current_task.md file. When it reaches 90% context, it tell it to update this, then I clear or compact, and have it read this as a first action afterwards.
If we could automate the flow so:
At 90%: update current_task.md, clear/compact with specific message
Post clear/compact: force read of current_task.md and continue with work
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.
This issue has been automatically closed due to 60 days of inactivity. If you're still experiencing this issue, please open a new issue with updated information.
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.
Activity
[-][Feature Request] Set custom commands to run instead of /compact after context window is 95% full[/-][+][Feature Request] Set custom pre-compact, compact, and post-compact commands to run instead of default after context window is 95% full[/+][-][Feature Request] Set custom pre-compact, compact, and post-compact commands to run instead of default after context window is 95% full[/-][+][Feature Request] Support custom pre-compact, compact, and post-compact commands to run instead of default after context window is 95% full[/+]coygeek commentedon Aug 24, 2025
Hey, this is a really interesting and well-thought-out feature request. I've definitely had similar frustrations where
/compactloses the specific context I've carefully built up for a task.The good news is that the dev team seems to have anticipated this need, and there's a feature that gets you about 90% of the way there already: Hooks. Specifically, the
PreCompacthook.Based on the Hooks Guide and the Hooks Reference, you can set up a script to run automatically right before a compaction event happens.
Here’s how you could build the workflow you're describing:
Create a
PreCompactHook:You'd register a custom script to run on the
PreCompactevent. The hook even receives atriggerfield in its JSON input, so your script will know if it was triggered by"auto"(the 95% threshold) or"manual"(a user running/compact). This directly addresses your need to handle the auto-compact event specifically.Build Your Custom "Compaction" Logic:
Your script would receive the path to the current conversation transcript. It could then:
ghCLI or a GitHub MCP server to fetch the latest, most relevant details for those issues.Preventing the Default
/compact:This is the key part. While the docs aren't super explicit about this for
PreCompact, the "Advanced JSON Output" section for hooks mentions a common field:{"continue": false}. My interpretation is that you could have your script output this JSON to stdout. This should, in theory, stop Claude Code from proceeding with the default, built-in compaction after your hook runs.Injecting the New Context (Post-Compact):
While there isn't a
PostCompacthook, you can build this into yourPreCompactscript. After generating the new context, your script could print a message to the user like:It's not fully automated, but it turns a manual, multi-step research process into a single-step "copy-paste" action prompted by your own custom logic.
Here's how you might configure it in your
.claude/settings.json:{ "hooks": { "PreCompact": [ { "matcher": "auto", // Only run for automatic compaction "hooks": [ { "type": "command", "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/rebuild-context.sh" } ] } ] } }While you can't replace the
/compactcommand itself or add apost-compacthook directly, thePreCompacthook seems powerful enough to achieve your goal of taking control over context management when the window gets full.Hope this helps! Let us know if you get a script working, I'd be really interested to see it.
DuaneNielsen commentedon Aug 27, 2025
Agree with this 100%. We need much better context management options that the current auto-compact. I have not heard anyone do anything but complain about this feature! I understand it's a cost savings thing, and anthropic is having some serious problems sustaining the current paradigm.. as the ever shrinking quotas reveal.. but auto-compaction is not a good solution. This Enhancement Request and suggestions by @coygeek offers much more useful solution... putting tools into the the community's hands to find the solution to a problem that 100% of the users experience.
BEN-50763 commentedon Sep 7, 2025
This is a brilliant suggestion.
My current approach is to have claude output its progress on the current task to a current_task.md file. When it reaches 90% context, it tell it to update this, then I clear or compact, and have it read this as a first action afterwards.
If we could automate the flow so:
At 90%: update current_task.md, clear/compact with specific message
Post clear/compact: force read of current_task.md and continue with work
github-actions commentedon Dec 5, 2025
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.
github-actions commentedon Jan 8, 2026
This issue has been automatically closed due to 60 days of inactivity. If you're still experiencing this issue, please open a new issue with updated information.
github-actions commentedon Jan 15, 2026
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.