claude-code - 💡(How to fix) Fix Feature request: context_threshold setting for auto-compact in settings.json [1 comments, 2 participants]

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…
GitHub stats
anthropics/claude-code#46695Fetched 2026-04-12 13:35:29
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Author
Timeline (top)
labeled ×4commented ×1

Fix Action

Fix / Workaround

Current Workaround

Code Example

{
  "context": {
    "auto_compact_threshold": 0.8,
    "pre_compact_hook": {
      "type": "command",
      "command": "bash scripts/compact-prep.sh",
      "timeout": 30000
    }
  }
}
RAW_BUFFERClick to expand / collapse

Problem

Long Claude Code sessions require manual compaction via /compact. Users must:

  1. Notice they're running low on context (no indicator)
  2. Manually prepare (save work, update memory files)
  3. 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)

extent analysis

TL;DR

Implementing an auto_compact_threshold setting in .claude/settings.json to automatically trigger compaction when context usage reaches a specified threshold can help mitigate manual compaction issues.

Guidance

  • Introduce a context_threshold setting to trigger automatic compaction, allowing for a pre-compact hook to prepare the environment before compaction.
  • Define a pre_compact_hook in the settings to execute a command (e.g., bash scripts/compact-prep.sh) before compaction, ensuring work is saved and memory files are updated.
  • Consider exposing a Compact tool or PreCompact hook type for the agent to invoke, enabling programmatic compaction triggering.
  • Test the auto_compact_threshold setting with different values to determine the optimal threshold for automatic compaction.

Example

{
  "context": {
    "auto_compact_threshold": 0.8,
    "pre_compact_hook": {
      "type": "command",
      "command": "bash scripts/compact-prep.sh",
      "timeout": 30000
    }
  }
}

Notes

The proposed solution relies on the implementation of the auto_compact_threshold setting and the pre_compact_hook in the Claude Code application. The effectiveness of this solution may depend on the specific use case and environment.

Recommendation

Apply the proposed workaround by introducing the auto_compact_threshold setting and pre_compact_hook to automate compaction and prepare the environment before compaction, as this approach addresses the manual compaction issue and provides a more seamless experience for power users.

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