Skip to content

Feature request: context_threshold setting for auto-compact in settings.json #46695

Description

@knee5

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions