Skip to content

[BUG]: Auto compaction not triggering reliably with Anthropic models #6068

Closed as not planned

Description

@ramarivera

Description

Auto compaction seems to have stopped working reliably with Anthropic models. Previously, when context tokens approached the model's limit, opencode would automatically trigger compaction to summarize the session. Now, we're getting 'prompt is too long' errors more frequently without auto compaction kicking in.

Steps To Reproduce

  1. Start a session with an Anthropic model (e.g., claude-sonnet-4-20250514)
  2. Have an extended conversation with multiple tool calls and file edits
  3. Continue working until context approaches the model's token limit (e.g., 131072 or 200000)
  4. Observe that instead of auto compaction running, you receive errors like:
    This model's maximum context length is 131072 tokens. However, you requested N tokens...
    
    or
    prompt is too long: X tokens > Y maximum
    

Expected Behavior

Opencode should automatically trigger session compaction BEFORE hitting the model's context limit, summarizing previous messages to free up context for continuing the conversation.

Actual Behavior

Auto compaction is not triggering automatically, leading to 'prompt too long' errors that stop the session. Manual compaction (via keybind) still works.

Relevant Log Output

# Running with verbose logging can provide more details:
OPENCODE_LOG=* opencode run <project>

Additional Context

Questions

  1. Did anything change in the auto compaction logic recently?
  2. Are there any new configuration options affecting compaction behavior?
  3. Is there any way to adjust the threshold or make compaction more aggressive?

Thanks for investigating this!

Activity

  1. github-actions commented on Dec 23, 2025

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Feel free to ignore if none of these address your specific case.

    For keybind-related issues, please also check our pinned keybinds documentation: #4997

  2. added a commit that references this issue on Jan 1, 2026
    5707d22
  3. Leksat commented on Jan 5, 2026

    @Leksat

    Same for me.

    I use opencode v1.1.2 connected to Claude Max plan. Right pane shows

    Context
    107,981 tokens
    54% used
    

    But get prompt is too long: 203185 tokens > 200000 maximum anyways. Confusing 😕

  4. github-actions commented on Mar 25, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.

  5. alario-tang commented on Mar 31, 2026

    @alario-tang

    I think this issue is partly about trigger timing, not only model/provider behavior.

    From reading the current code, auto-compaction seems to be driven mostly by:

    • post-step token usage crossing threshold
    • provider-side overflow / context rejection

    That means the system is still often reacting after a request was already too large, instead of shrinking context before the next risky request is sent.

    A useful improvement here may be a lightweight pre-request pass before normal generation, for example:

    • prune older completed tool output earlier
    • reserve a stricter output budget
    • compact proactively before provider rejection, not after

    That would likely reduce the “prompt too long first, compaction later” failure mode this issue describes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions