Problem
Long Claude Code sessions require manual compaction via /compact. Users must:
- Notice they're running low on context (no indicator)
- Manually prepare (save work, update memory files)
- Type
/compact
Missing step 1 or 2 results in lost context and wasted work. There's currently no way for hooks or the agent to programmatically trigger compaction.
Proposed Solution
Add a context_threshold setting in .claude/settings.json that automatically triggers compaction (or a pre-compact hook) when context usage reaches a threshold:
{
"context": {
"auto_compact_threshold": 0.8,
"pre_compact_hook": {
"type": "command",
"command": "bash scripts/compact-prep.sh",
"timeout": 30000
}
}
}
Behavior
- At
auto_compact_threshold (e.g., 80%), fire the pre_compact_hook first (save state, update memory, commit work)
- Then automatically run compaction
- Alternatively: expose a
Compact tool the agent can invoke, or a PreCompact hook type
Current Workaround
We built a context-monitor.sh that runs on every PreToolUse hook, counting tool calls as a proxy for context usage. At estimated 80%, it prints a warning that the agent's AGENTS.md rules tell it to act on (run compact prep). But the actual /compact still requires user input.
This works but is fragile — the tool-call-to-context-% mapping is a rough heuristic, and the agent can't actually trigger compaction.
Use Case
Power users running 8+ hour sessions with:
- Multiple background processes (transcription, imports)
- Telegram integration receiving messages throughout
- Heavy tool use (500+ calls per session)
- Memory files that must be updated before compaction
These sessions routinely hit context limits. Auto-compact with a pre-hook would make long sessions seamless.
Environment
- Claude Code 2.1.101
- macOS, Opus 4.6 (1M context)
Problem
Long Claude Code sessions require manual compaction via
/compact. Users must:/compactMissing step 1 or 2 results in lost context and wasted work. There's currently no way for hooks or the agent to programmatically trigger compaction.
Proposed Solution
Add a
context_thresholdsetting in.claude/settings.jsonthat automatically triggers compaction (or a pre-compact hook) when context usage reaches a threshold:{ "context": { "auto_compact_threshold": 0.8, "pre_compact_hook": { "type": "command", "command": "bash scripts/compact-prep.sh", "timeout": 30000 } } }Behavior
auto_compact_threshold(e.g., 80%), fire thepre_compact_hookfirst (save state, update memory, commit work)Compacttool the agent can invoke, or aPreCompacthook typeCurrent Workaround
We built a
context-monitor.shthat runs on everyPreToolUsehook, counting tool calls as a proxy for context usage. At estimated 80%, it prints a warning that the agent's AGENTS.md rules tell it to act on (run compact prep). But the actual/compactstill requires user input.This works but is fragile — the tool-call-to-context-% mapping is a rough heuristic, and the agent can't actually trigger compaction.
Use Case
Power users running 8+ hour sessions with:
These sessions routinely hit context limits. Auto-compact with a pre-hook would make long sessions seamless.
Environment