fix(ai): recognize LiteLLM context overflow errors

Merged
RE
1mo ago
+24-0

Issue for this PR

Closes https://github.com/anomalyco/opencode/issues/44888

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

LiteLLM's own size check runs before it calls the model, and its error wasn't recognized as overflow. The session stopped on a generic error instead of compacting.

"litellm.ContextWindowExceededError: ... Context Window exceeded for given call."
before: generic error     after: compacts

This adds /context window exceeded/i to the overflow phrases. Which statuses are checked is unchanged.

I also checked Xiaomi MiMo, which doesn't reject an oversized prompt. I sent mimo-v2.6-flash about 1.1M tokens against its 1,048,576 window. It returned HTTP 200 with finish length, 0 output tokens and prompt_tokens 1,048,570, both streaming and not. opencode already handles this: the next step's auto compaction sees the reported size over its threshold and compacts. I added a runner test so that stays true.

Out of scope: HTTP 413 in a normal step is still not recovered. https://github.com/anomalyco/opencode/pull/53108 covers that.

How did you verify your code works?

  • bun test test/provider-error.test.ts in packages/ai: 42 pass. The new case failed before the change.
  • bun run script/test.ts test/session-runner.test.ts in packages/core: 243 pass.
  • bun typecheck in both packages.
  • The Xiaomi responses above came from real API calls.

Visual

<svg xmlns="http://www.w3.org/2000/svg" width="240" height="72" viewBox="0 0 240 72" fill="none">
  <style>
    .err { animation: err 3s ease-in-out infinite; }
    .ok { animation: ok 3s ease-in-out infinite; }
    @keyframes err { 0%, 35% { opacity: 1; } 50%, 100% { opacity: 0; } }
    @keyframes ok { 0%, 40% { opacity: 0; } 55%, 100% { opacity: 1; } }
    @media (prefers-reduced-motion: reduce) { .err, .ok { animation: none; } .err { opacity: 0; } .ok { opacity: 1; } }
  </style>
  <rect x="12" y="22" width="96" height="28" rx="6" stroke="currentColor" stroke-opacity="0.5" stroke-width="1.5"/>
  <text x="60" y="40" font-family="sans-serif" font-size="10" text-anchor="middle" fill="currentColor">LiteLLM 400</text>
  <path d="M114 36h22" stroke="currentColor" stroke-opacity="0.6" stroke-width="1.5"/>
  <path d="M132 32l4 4-4 4" stroke="currentColor" stroke-opacity="0.6" stroke-width="1.5"/>
  <g class="err">
    <rect x="144" y="22" width="84" height="28" rx="6" stroke="currentColor" stroke-opacity="0.3" stroke-dasharray="3 3"/>
    <text x="186" y="40" font-family="sans-serif" font-size="10" text-anchor="middle" fill="currentColor" fill-opacity="0.5">error</text>
  </g>
  <g class="ok">
    <rect x="144" y="22" width="84" height="28" rx="6" fill="currentColor" fill-opacity="0.15" stroke="currentColor" stroke-width="1.5"/>
    <text x="186" y="40" font-family="sans-serif" font-size="10" text-anchor="middle" fill="currentColor">compact</text>
  </g>
</svg>

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR