Skip to content

Session compaction fails with "context exceeds model limit" error #17340

Description

@he-who-is-not-him

Description

Description

Sessions can trigger the error:

"Session too large to compact - context exceeds model limit even after stripping media"

In my case, using a model with a 128k context limit, the session grew to 145,882 tokens. Notably, there hadn't been a user message in a while - only responses through the question tool accumulating in the conversation.

What I Think Is Happening

The overflow detection in isOverflow() (packages/opencode/src/session/compaction.ts:32-48) appears to use token counts from the previous turn's completed API response (lastFinished.tokens). This may not account for content that will be added in the next API call:

  1. New user message
  2. System prompts
  3. Reminders
  4. Tool schemas

This could create a timing gap where sessions exceed the safe threshold before compaction is triggered. However, I haven't fully verified this hypothesis.

Observations

  • Error occurred after extended use of the question tool without intervening user messages
  • Session reached 145,882 tokens (exceeds 128k limit)
  • Compaction failed even with stripMedia: true

Relevant Code Paths

  • compaction.ts:44-47 - isOverflow logic
  • compaction.ts:224-232 - error when compaction fails
  • prompt.ts:543-555 - pre-send overflow check
  • processor.ts:359-360 - error detection

Environment

  • Model: github-copilot/claude-opus-4.6 (128k context, 64k output)

Plugins

@franlol/[email protected], @tarquinen/[email protected], [email protected]; disabling opencode-dcp doesn't help

OpenCode version

1.2.25

Steps to reproduce

Steps to Reproduce

Create a minimal reproduction by configuring the model to continuously generate long responses:

  1. Create an AGENTS.md with instructions to never end responses and always continue using the question tool:
# Never-Ending Response Protocol

After every response, you MUST use the question tool to ask a follow-up question. Never provide a final answer. Always expand on your previous response with additional details, examples, and explanations before asking the next question.

This continues indefinitely until the session hits context limits.
  1. Start a session with a model with limited context (e.g., 128k)
  2. The model will continuously generate responses and accumulate context
  3. Eventually the session will exceed the context limit and trigger the compaction failure

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Activity

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