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
- Session starts, context grows normally.
- Context crosses the model's
limit.context (200K tokens).
compactIfNeeded is never called — the @ai-sdk/openai-compatible provider adapter's loop does not include the compaction check.
- Context keeps growing: 200K -> 400K -> 600K -> 800K+ tokens per message.
time_compacting stays NULL for all sessions.
- 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
Description
Bug
Automatic context compaction (
compaction.auto: true) never triggers for providers using@ai-sdk/openai-compatible. TheSessionCompaction.compactIfNeededcheck is not called in the provider adapter's iteration loop, so context grows unbounded until the user manually runs/compact.Environment
alibaba-token-plan(uses@ai-sdk/openai-compatible)qwen3.8-max,deepseek-v4.1-flash,glm-5.2(custom model limits configured viaprovider.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
limit.context(200K tokens).compactIfNeededis never called — the@ai-sdk/openai-compatibleprovider adapter's loop does not include the compaction check.time_compactingstays NULL for all sessions./compactworks fine — the compaction mechanism itself is functional, just the automatic trigger is missing.Evidence
time_compacting = NULL./compact(part hasauto: false)./compact, context dropped to 77K.BetaToolRunnerhas its owncompactionControlcheck in itsasyncIterator, but this code path is Anthropic-specific (this.client.beta.messages.create). The@ai-sdk/openai-compatibleadapter uses a different loop.Expected behaviour
SessionCompaction.compactIfNeededshould be called after each LLM response in all provider adapters, not just the Anthropic one. Whencompaction.auto: trueand the model haslimit.contextconfigured, compaction should trigger when context approaches the limit.Impact
This is causing significant token waste. Over 12 weeks:
Workaround
Manual
/compactwhen 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