claude-code - 💡(How to fix) Fix [FEATURE] Configurable autocompact thrashing threshold for long-running tasks in -p mode [3 comments, 4 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#48983Fetched 2026-04-17 08:54:03
View on GitHub
Comments
3
Participants
4
Timeline
6
Reactions
0
Timeline (top)
commented ×3labeled ×3

Error Message

When running long tasks in -p (non-interactive) mode, Claude Code automatically terminates the task with the following error:

RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing requests and this feature hasn't been requested yet
  • 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

extent analysis

TL;DR

Set the AUTO_COMPACT_THRASHING_THRESHOLD environment variable to a higher value to increase the threshold for autocompact thrashing.

Guidance

  • To mitigate the issue, try setting AUTO_COMPACT_THRASHING_THRESHOLD to a value higher than the current hardcoded threshold of 3, such as 10, as proposed in the issue.
  • Verify that the environment variable is set correctly before running the task with claude -p.
  • Test the task with the new threshold value to see if it completes without terminating prematurely.
  • Consider experimenting with different threshold values to find the optimal setting for your specific use case.

Example

export AUTO_COMPACT_THRASHING_THRESHOLD=10
claude -p "your task"

Notes

The proposed solution assumes that the AUTO_COMPACT_THRASHING_THRESHOLD environment variable will be implemented and respected by the Claude Code application. If this variable is not implemented, alternative solutions may need to be explored.

Recommendation

Apply workaround: Set the AUTO_COMPACT_THRASHING_THRESHOLD environment variable to a higher value, as this allows users to control the autocompact thrashing threshold and potentially prevent premature task termination.

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