AI Gateway: non-streaming Anthropic requests with max_tokens > 21,333 rejected ("must be under 21K") since 2026-09-11, same request works with stream: true
Description
Summary
Since about 2026-09-11 05:00 UTC, AI Gateway returns HTTP 400 on every non-streaming request to any Anthropic model whose max_tokens exceeds 21,333:
{"error":{"message":"max output tokens plus reasoning effort tokens must be under 21K for anthropic/claude-opus-5","type":"invalid_request_error", ...}}
The identical request with stream: true succeeds. The identical non-streaming request sent directly to api.anthropic.com succeeds with max_tokens 64,000 and 128,000. GET /v1/models on the gateway advertises max_tokens: 128000 for these models.
In the AI SDK this means generateText / generateObject with maxOutputTokens above 21,333 on any anthropic/* model now throw GatewayInvalidRequestError, while streamText with the same value works.
Actual behavior
Verified with raw curl (so not SDK-specific), as of 2026-09-11 13:46 UTC:
| Endpoint | Model | max_tokens | reasoning | stream | Result |
|---|---|---|---|---|---|
gateway /v1/chat/completions |
claude-opus-5 | 21334 | default | no | 400 |
gateway /v1/chat/completions |
claude-opus-5 | 21334 | reasoning_effort: "none" |
no | 400 |
gateway /v1/chat/completions |
claude-opus-5 | 21333 | default | no | 200 |
gateway /v1/chat/completions |
claude-opus-5 | 21333 | reasoning_effort: "xhigh" |
no | 200 |
gateway /v1/chat/completions |
claude-opus-5 | 64000 | default | yes | 200 |
gateway /v1/messages (Anthropic format) |
claude-opus-5 | 64000 | thinking: {type: "disabled"} |
no | 400 |
gateway /v1/messages |
claude-opus-5 | 21333 / 21334 | default | no | 200 / 400 |
gateway /v1/messages |
claude-opus-5 | 64000 | default | yes | 200 |
gateway /v1/chat/completions |
claude-sonnet-5, claude-sonnet-4.6, claude-opus-4.6, claude-haiku-4.5 | 64000 | default | no | 400 |
gateway /v1/chat/completions |
claude-haiku-4.5 | 12000 | reasoning_effort: "high" |
no | 400 (mapped budget_tokens is added to the sum) |
gateway /v1/chat/completions |
openai/gpt-5, google/gemini-2.5-pro | 64000 | default | no | 200 |
api.anthropic.com/v1/messages (direct) |
claude-opus-5, claude-sonnet-4-6 | 64000 and 128000 | default | no | 200 |
Expected behavior
Non-streaming requests accept max_tokens up to the catalog's advertised max_tokens for the model, as they did before 2026-09-11 and as the upstream Anthropic API does.
If a non-streaming cap is intentional, then:
- document it on the Anthropic Messages API / model parameters pages and expose it in
GET /v1/models, and - make the error message state the actual rule (non-streaming
max_tokens+ thinking budget must be ≤ 21,333; usestream: truefor larger values). The current message blames "reasoning effort tokens", but the request fails with reasoning disabled and succeeds at 21,333 withxhigh.
Likely cause
21,333 = 128,000 × (10 min / 60 min). That is the exact threshold of the Anthropic SDKs' client-side guard calculateNonstreamingTimeout ("Streaming is strongly recommended for operations that may take longer than 10 minutes"; see anthropics/anthropic-sdk-python#874, anthropics/anthropic-sdk-go#194). It looks like the gateway now runs that SDK heuristic server-side and surfaces it as an invalid_request_error. Anthropic's own API does not enforce it.
Timeline
In our production job runner, non-streaming generateText calls with maxOutputTokens: 64000 against anthropic/claude-opus-5 completed several hundred thousand times between 2026-08-29 and 2026-09-10 with zero occurrences of this error. The first 400 appeared at 2026-09-11 ~05:00 UTC on the same deployed code version that had been succeeding an hour earlier, so this is a gateway-side change.
Existing issues
Searched vercel/ai for "under 21K", "reasoning effort tokens", and gateway/anthropic/max_tokens; no match.
Reproduction
Minimal curl, no SDK needed:
# 400
curl -s https://ai-gateway.vercel.sh/v1/chat/completions \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"model":"anthropic/claude-opus-5","max_tokens":21334,"messages":[{"role":"user","content":"Say hi"}]}'
# 200
curl -s https://ai-gateway.vercel.sh/v1/chat/completions \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"model":"anthropic/claude-opus-5","max_tokens":21333,"messages":[{"role":"user","content":"Say hi"}]}'
# 200: same as the failing request plus stream:true
curl -s https://ai-gateway.vercel.sh/v1/chat/completions \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"model":"anthropic/claude-opus-5","max_tokens":64000,"stream":true,"messages":[{"role":"user","content":"Say hi"}]}'
# 200: direct to Anthropic, non-streaming, 128K
curl -s https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" -H "anthropic-version: 2023-06-01" -H "content-type: application/json" \
-d '{"model":"claude-opus-5","max_tokens":128000,"messages":[{"role":"user","content":"Say hi"}]}'
AI SDK equivalent (Bun 1.4.0, AI_GATEWAY_API_KEY in env):
import { generateText, streamText } from 'ai'
// throws GatewayInvalidRequestError:
// max output tokens plus reasoning effort tokens must be under 21K for anthropic/claude-opus-5
await generateText({ model: 'anthropic/claude-opus-5', prompt: 'Say hi', maxOutputTokens: 21_334, maxRetries: 0 })
// ok
await generateText({ model: 'anthropic/claude-opus-5', prompt: 'Say hi', maxOutputTokens: 21_333, maxRetries: 0 })
// ok
await streamText({ model: 'anthropic/claude-opus-5', prompt: 'Say hi', maxOutputTokens: 64_000, maxRetries: 0 }).text
AI SDK Version
- ai: 7.0.83
- @ai-sdk/gateway: 4.0.67
- Also reproduced with raw curl against
/v1/chat/completionsand/v1/messages, so the SDK version is not a factor. - Auth: Gateway API key, no BYOK.
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Identify issue type
Identified as Bug
Confidence: High
Rationale
This is a concrete regression in existing Gateway behavior that contradicts the advertised model limit and causes valid generateText/generateObject calls to fail unexpectedly.
Evidence
- AI Gateway rejects non-streaming Anthropic requests above 21,333 output tokens even though its model catalog advertises up to 128,000 tokens.
- The same requests reportedly worked before a gateway-side change, work when streaming, and succeed non-streaming against Anthropic directly.
- The report includes reproducible curl and AI SDK cases showing a sharp 21,333/21,334 success-failure boundary.
Bug Reproduction - AI SDK 7
Bug is outside the AI SDK codebase scope.
Target branch: main
- Valid non-streaming
generateTextandgenerateObjectcalls are rejected above 21,333 tokens, forcing users to reduce the advertised output limit or switch to streaming. - Live AI Gateway requests returned HTTP 200 at 21,333 tokens and HTTP 400 at 21,334 tokens for
anthropic/claude-opus-5; a streaming request with 64,000 tokens returned HTTP 200. - The same rejection occurred through
/v1/messages, with reasoning disabled, across the reported Anthropic models, and when Haiku reasoning plus output exceeded the limit; the reported OpenAI and Gemini controls succeeded. - Direct Anthropic non-streaming requests to Claude Opus 5 and Sonnet 4.6 succeeded with 64,000 and 128,000 tokens, while AI Gateway's live model catalog advertises up to 128,000 tokens for the affected models.
- Current
mainversions[email protected]and@ai-sdk/[email protected]producedGatewayInvalidRequestErrorfor both non-streaming APIs whilestreamTextsucceeded, ruling out the reported older package versions as the cause. packages/gateway/src/gateway-language-model.tsforwardsmaxOutputTokensunchanged and only marks whether the request is streaming;packages/gateway/src/errors/create-gateway-error.tscorrectly maps the backend'sinvalid_request_errorresponse.- The raw API failure and successful direct-Anthropic control place the defect in the Vercel AI Gateway backend at
ai-gateway.vercel.sh, so a meaningful fix does not belong in thevercel/aiSDK adapter. - The backend response names
NonstreamingMaxTokensError, which is consistent with the reported timeout-guard hypothesis, but the backend implementation and the reported historical onset around September 11, 2026 at 05:00 UTC cannot be independently inspected from this repository.
Relevant Documentation
- https://vercel.com/docs/ai-gateway/openai-compat/rest-api
- https://vercel.com/docs/ai-gateway/models-and-providers
- https://vercel.com/docs/ai-gateway/sdks-and-apis
- https://platform.claude.com/docs/en/api/http/messages/create
- https://platform.claude.com/docs/en/models/opus-5/whats-new-opus-5
- https://platform.claude.com/docs/en/build-with-claude/streaming
- https://platform.claude.com/docs/en/cli-sdks-libraries/sdks/go
@ptempelman Interesting , direct Anthropic works but the gateway rejects the same request.
I’m building Badgr for exactly this kind of thing. Same OpenAI-compatible request, just point it at:
base_url="https://aibadgr.com/v1"
Badgr can fail over when a route breaks instead of killing the request. Happy to throw you some credits to test it.