Feature: Auto-compact and continue when context limit reached
Problem
When working on long tasks, Claude Code hits the context limit and stops with "Context limit reached · /compact and continue". This requires manual intervention to run /compact and then re-prompt Claude to continue.
For autonomous workflows or long-running tasks, this interruption breaks the flow and requires babysitting.
Proposed Solution
Add an option to automatically compact and continue when the context limit is reached:
Option A: CLI flag
claude --auto-compact
Option B: Settings config
{
"autoCompact": true
}
Option C: Environment variable
CLAUDE_AUTO_COMPACT=1 claude
Behavior
When enabled and context limit is reached:
- Automatically trigger compaction (equivalent to
/compact) - Resume the current task without user intervention
- Optionally log that compaction occurred
Use Cases
- Long implementation tasks that span many files
- Autonomous agent workflows
- CI/CD pipelines using Claude Code
- Any scenario where human-in-the-loop for compaction is undesirable
Alternatives Considered
- Hooks: Current
PreCompactandSessionStarthooks fire around compaction but cannot trigger it - Wrapper scripts: Fragile, don't integrate well with interactive mode
- Larger context: Doesn't solve the fundamental issue, just delays it
Additional Context
This would complement the existing hook system nicely - users could still use SessionStart + "compact" matcher to restore critical context after auto-compaction.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗