[FEATURE] Configurable autocompact thrashing threshold for long-running tasks in -p mode

Resolved 💬 4 comments Opened Apr 16, 2026 by developer-freed Closed May 25, 2026

Preflight Checklist

  • [x] I have searched existing requests and this feature hasn't been requested yet
  • [x] This is a single feature request (not multiple features)

Problem Statement

When running long tasks in -p (non-interactive) mode, Claude Code automatically terminates the task with the following error:
Autocompact is thrashing: the context refilled to the limit within 3 turns
of the previous compact, 3 times in a row. A file being read or a tool
output is likely too large for the context window. Try reading in smaller
chunks, or use /clear to start fresh

The thrashing threshold is currently hardcoded to 3 consecutive compactions, which is too aggressive for long-running tasks (e.g., large codebase analysis, multi-step code generation). This causes tasks to terminate prematurely with no way to resume automatically

Proposed Solution

Add a configurable environment variable AUTO_COMPACT_THRASHING_THRESHOLD to allow users to control the autocompact thrashing threshold:

Environment Variable

Set the maximum number of consecutive compactions before terminating

export AUTO_COMPACT_THRASHING_THRESHOLD=10

Then run the task

claude -p "your task"

Alternative Solutions

_No response_

Priority

Critical - Blocking my work

Feature Category

Configuration and settings

Use Case Example

_No response_

Additional Context

_No response_

View original on GitHub ↗

This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗