Visitar URL original
# GitHub Issue: SessionCompaction.compactIfNeeded never triggers for @ai-sdk/openai-compatible providers · Issue #53935 · anomalyco/opencode · GitHub
Skip to content

# GitHub Issue: SessionCompaction.compactIfNeeded never triggers for @ai-sdk/openai-compatible providers #53935

Description

@thorinduffin-dot

Description

Bug

Automatic context compaction (compaction.auto: true) never triggers for providers using @ai-sdk/openai-compatible. The SessionCompaction.compactIfNeeded check is not called in the provider adapter's iteration loop, so context grows unbounded until the user manually runs /compact.

Environment

  • OpenCode v1.18.0 (Homebrew)
  • Provider: alibaba-token-plan (uses @ai-sdk/openai-compatible)
  • Models: qwen3.8-max, deepseek-v4.1-flash, glm-5.2 (custom model limits configured via provider.alibaba-token-plan.models.*.limit)

Config

{
  "compaction": {
    "auto": true,
    "tail_turns": 15
  },
  "provider": {
    "alibaba-token-plan": {
      "models": {
        "qwen3.8-max": {
          "limit": { "context": 200000, "output": 16384 }
        }
      }
    }
  }
}

What happens

  1. Session starts, context grows normally.
  2. Context crosses the model's limit.context (200K tokens).
  3. compactIfNeeded is never called — the @ai-sdk/openai-compatible provider adapter's loop does not include the compaction check.
  4. Context keeps growing: 200K -> 400K -> 600K -> 800K+ tokens per message.
  5. time_compacting stays NULL for all sessions.
  6. Manual /compact works fine — the compaction mechanism itself is functional, just the automatic trigger is missing.

Evidence

  • 405 sessions over 12 weeks, all with time_compacting = NULL.
  • Only 1 session has compaction parts — created by manual /compact (part has auto: false).
  • A test session crossed 200K at message 53 and grew to 491K without triggering compaction. After manual /compact, context dropped to 77K.
  • The Anthropic SDK's BetaToolRunner has its own compactionControl check in its asyncIterator, but this code path is Anthropic-specific (this.client.beta.messages.create). The @ai-sdk/openai-compatible adapter uses a different loop.

Expected behaviour

SessionCompaction.compactIfNeeded should be called after each LLM response in all provider adapters, not just the Anthropic one. When compaction.auto: true and the model has limit.context configured, compaction should trigger when context approaches the limit.

Impact

This is causing significant token waste. Over 12 weeks:

  • 14.6B tokens consumed across 50K LLM calls.
  • 80.9% of tokens (11.8B) are "excess" from unbounded context growth.
  • Average tokens per message: 292K (should be <200K with compaction).
  • Output/input ratio: 1.7% (should be 5-15%).

Workaround

Manual /compact when a session crosses ~180K tokens. A monitoring script helps track this.

Plugins

No response

OpenCode version

No response

Steps to reproduce

No response

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Activity

  1. opencode-agent commented on Oct 8, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed write-up and the usage numbers.

    This was reported on OpenCode v1.18.0. v1 now only gets fixes for critical bugs (data loss, security problems, or crashes with no workaround). Auto-compaction not triggering is costly, but manual /compact works around it, so this is unlikely to be fixed in v1.

    We'd encourage you to try v2 (@opencode/cli, the opencode command). All active work happens there, including on compaction.

    A note on the analysis: as far as I can tell, BetaToolRunner/compactionControl are part of Anthropic's SDK, not OpenCode. OpenCode decides when to auto-compact from the token usage it records for each assistant message. If the provider's usage isn't recorded, it never sees the context getting close to the limit. It may be worth checking whether your messages show token counts at all.

    These issues are related, ordered from most to least likely:

    If you believe this is critical, or you can reproduce it on v2, please reply here with the v2 version (opencode --version) and steps to reproduce.

  2. opencode-agent commented on Oct 9, 2026

    @opencode-agent
    Contributor

    Closing this as not planned. It was reported on OpenCode v1.18.0, and v1 only gets fixes for critical bugs. Auto-compaction not triggering for @ai-sdk/openai-compatible providers is costly, but manual /compact works around it. We didn't hear back with a v2 reproduction, so there's nothing more to act on here.

    Please try v2 (@opencode/cli, the opencode command). If auto-compaction still doesn't trigger there, open a new issue with the output of opencode --version, your provider and model config, and the steps to reproduce. #50338 may also be relevant to what you saw. Thanks again for the detailed numbers.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions