Repository navigation
fix: map anthropic context window exceeded stop reason - #3254
Conversation
Anthropic models emit stop_reason "model_context_window_exceeded" when generation hits the model's context window before max_tokens. The gateway did not recognize it: getUnifiedFinishReason logged a spurious "Unknown finish reason encountered" error and clients received finish_reason "stop" instead of "length". Map it uniformly (direct API, Vertex, Bedrock) to the LENGTH_LIMIT unified reason and the OpenAI-canonical "length" finish reason. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LxoFcM6Btfm7x2MR56wDXs
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
WalkthroughThe gateway now classifies ChangesFinish Reason Normalization
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Problem
Production gateway pods log spurious errors like:
Anthropic models emit the stop reason
model_context_window_exceededwhen generation hits the model's context window before reachingmax_tokens. The gateway didn't recognize it, so:getUnifiedFinishReasonclassified it asUNKNOWNand logged anERROR-level "Unknown finish reason encountered" alert on every occurrence.mapFinishReasonToOpenaisilently converted it to"stop", so OpenAI-compatible clients saw a normal completion instead of"length".isLengthLimitFinishReasondidn't treat these responses as length-limited.Fix
Map
model_context_window_exceededas a length limit uniformly. Like the existing cross-providerrefusalhandling, this stop reason surfaces across the Anthropic direct API, Vertex, and Bedrock, so it's handled at the top level of both mappers:apps/gateway/src/lib/logs.ts—getUnifiedFinishReasonreturnsLENGTH_LIMIT(also fixesisLengthLimitFinishReason, which derives from it), eliminating the spurious error log.apps/gateway/src/chat/tools/map-finish-reason-to-openai.ts— clients now receive the OpenAI-canonical"length"finish reason (streaming and non-streaming).apps/gateway/src/chat/tools/parse-provider-response.tsandtransform-streaming-to-openai.ts— the Bedrock Converse branches map the raw stop reason to"length"alongsidemax_tokens.Testing
anthropic,vertex-anthropic, andaws-bedrockinlogs.spec.ts(65 tests passing in affected files).turbo run build --filter=gatewaypasses.🤖 Generated with Claude Code
https://claude.ai/code/session_01LxoFcM6Btfm7x2MR56wDXs
Generated by Claude Code
Summary by CodeRabbit