Skip to content

[Bug]: Context window limit hardcoded to 200K — ignores Claude Opus 4.6 native 1M support unless env var is set #1753

@ChoKhoOu

Description

@ChoKhoOu

Bug Description

Claude Opus 4.6 natively supports 1M context window, but oh-my-opencode hardcodes 200_000 as the default Anthropic context limit across three separate files. The 1M limit is only activated when the user manually sets ANTHROPIC_1M_CONTEXT=true environment variable — a setting most users are unaware of.

This causes premature compaction at ~156K tokens (78% of 200K), wastes the model's actual capacity, and contributes to the double-compaction race condition described in #1752.

Affected Code

The 200K limit is hardcoded in three places:

1. src/hooks/preemptive-compaction.ts (lines 1-7)

const DEFAULT_ACTUAL_LIMIT = 200_000

const ANTHROPIC_ACTUAL_LIMIT =
  process.env.ANTHROPIC_1M_CONTEXT === "true" ||
  process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
    ? 1_000_000
    : DEFAULT_ACTUAL_LIMIT  // ← 200K without env var

2. src/hooks/context-window-monitor.ts (lines 5-9)

const ANTHROPIC_ACTUAL_LIMIT =
  process.env.ANTHROPIC_1M_CONTEXT === "true" ||
  process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
    ? 1_000_000
    : 200_000

3. src/shared/dynamic-truncator.ts (lines 4-7)

process.env.ANTHROPIC_1M_CONTEXT === "true" ||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
  ? 1_000_000
  : 200_000;

Existing Detection Logic (Unused)

There is already a runtime detection mechanism in src/plugin-handlers/provider-config-handler.ts:

const anthropicBeta = providers?.anthropic?.options?.headers?.["anthropic-beta"];
params.modelCacheState.anthropicContext1MEnabled =
  anthropicBeta?.includes("context-1m") ?? false;

And plugin-state.ts stores this:

export interface ModelCacheState {
  modelContextLimitsCache: Map<string, number>;
  anthropicContext1MEnabled: boolean;  // ← computed at init, but never read by the three hooks above
}

The anthropicContext1MEnabled flag is correctly computed from provider config but is never used by preemptive-compaction, context-window-monitor, or dynamic-truncator. These three hooks only check the ANTHROPIC_1M_CONTEXT environment variable.

Impact

Consequence Detail
Premature compaction Compaction triggers at ~156K tokens instead of ~780K
Wasted context capacity Only 20% of Opus 4.6's actual capacity is used
Double compaction crash The low limit makes it far more likely to trigger the race condition in #1752
Poor user experience Users see "Auto Compact Failed" and must restart sessions unnecessarily

Expected Behavior

  • The plugin should auto-detect the model's actual context limit from OpenCode's provider config or the model metadata
  • At minimum, the three hooks should read modelCacheState.anthropicContext1MEnabled (which already exists) instead of relying solely on env vars
  • For Claude Opus 4.6, the default limit should be 1M without requiring manual env var configuration

Suggested Fix

  1. Pass modelCacheState to preemptive-compaction, context-window-monitor, and dynamic-truncator
  2. Use modelCacheState.anthropicContext1MEnabled as an additional condition (alongside the existing env var check)
  3. Consider auto-detecting context limits from the actual model being used (via modelCacheState.modelContextLimitsCache)

Workaround

Add to your shell config (~/.zshrc or ~/.bashrc):

export ANTHROPIC_1M_CONTEXT=true

Then restart OpenCode. This sets the limit to 1M and the 78% threshold becomes ~780K.

Environment

  • oh-my-opencode: v3.4.0+ (all versions)
  • Model: Claude Opus 4.6 (supports 1M natively)
  • OpenCode: latest

Related

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions