Skip to content

fix(anthropic): map model_context_window_exceeded to a length finish - #212

Merged
flore2003 merged 3 commits into
agdevhq:mainfrom
erzaehlsalex:fix/anthropic-context-window-stop-reason
Oct 2, 2026
Merged

flore2003 merged 3 commits into
agdevhq:mainfrom
erzaehlsalex:fix/anthropic-context-window-stop-reason

Conversation

@erzaehlsalex

Copy link
Copy Markdown
Contributor

What

  • mapStopReason maps model_context_window_exceeded to finishReason: 'length'. It was 'unknown'.
  • Upgrades @anthropic-ai/sdk from ^0.110.0 to ^0.114.0. 0.114.0 is the first version whose StopReason type includes the value, so the mapping needs no hand-written union or beta type.

Why

On Claude 4.5 and newer, the API accepts requests where input plus max_tokens exceeds the context window. If generation reaches the window limit, it stops with model_context_window_exceeded. Anthropic's docs say to treat that response as truncated.

#211 makes this stop reason routine: an omitted maxTokens becomes the model ceiling, so a long prompt on a 200K model (Haiku 4.5, Sonnet 4.5, Opus 4.5) no longer ends with max_tokens. With 'unknown':

  • callers checking finishReason === 'length' miss the truncation
  • generateObject() skips its truncation error and throws StructuredOutputParseError on the cut-off JSON

The fix is independent of #211 and applies to main as is. It was split out of #211 so that PR can release first.

Notes

  • package-lock.json also picks up the workspace package versions (0.25.0 → 0.28.1), which were stale in the lockfile.
  • Not reproduced against the live API. The behaviour rests on Anthropic's context-windows and stop-reasons docs.

Test plan

  • npm run release:check
  • npm run test
  • New unit test: a stream ending with model_context_window_exceeded finishes with 'length'

🤖 Generated with Claude Code

Alex Augustin and others added 3 commits October 2, 2026 09:10
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@flore2003
flore2003 merged commit 842a0d4 into agdevhq:main Oct 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants