Skip to content

MODEL_CACHE silently overwrites context windows on provider conflicts (last-write-wins) #11629

@fraction12

Description

@fraction12

Bug

MODEL_CACHE uses a last-write-wins strategy when multiple providers register the same model ID with different context windows. There is no warning or conflict resolution — the last provider to load silently overwrites the previous value.

Example

The built-in anthropic provider correctly registers claude-opus-4-6 with a 1M context window. The opencode provider (from @mariozechner/pi-coding-agent) then overwrites it with 200K — and the values for Opus 4.6 and Sonnet 4.5 are actually swapped in that provider.

Because MODEL_CACHE silently accepts the last write, the wrong values propagate to session context limit calculations, causing:

  1. Opus 4.6 sessions to compact prematurely (200K limit instead of 1M)
  2. Sonnet 4.5 sessions to overflow (1M belief instead of ~200K, API rejects at ~195K)

Relevant Code

// reply-B_4pVbIX.js ~line 4188
if (typeof m.contextWindow === "number" && m.contextWindow > 0) 
  MODEL_CACHE.set(m.id, m.contextWindow);

No conflict check, no logging, no provider priority.

Suggestion

When a model ID is already in MODEL_CACHE with a different contextWindow, OpenClaw should either:

  1. Log a warning — so users can diagnose context window mismatches
  2. Prefer the larger value — safer default (premature compaction is worse than slightly delayed compaction)
  3. Use provider priority — e.g., the native provider for a model should win over third-party providers

Workaround

Using modelOverrides in the conflicting provider's models.json entry to patch values post-load:

{
  "providers": {
    "opencode": {
      "modelOverrides": {
        "claude-opus-4-6": { "contextWindow": 1000000 },
        "claude-sonnet-4-5": { "contextWindow": 200000 }
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions