You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(anthropic): default max_tokens to the model's output ceiling (#849) (#853)
* fix(anthropic): default max_tokens to the model's output ceiling (#849)
Anthropic's Messages API requires `max_tokens`, so the text adapter must
always send a value. It previously hard-coded `?? 1024` when the caller
didn't pass one, silently truncating any non-trivial generation mid-stream
with `stop_reason: "max_tokens"`.
Now default to the resolved model's real `max_output_tokens` from model-meta
(e.g. 64K Sonnet, 128K Opus), falling back to 64K for unrecognized ids.
`max_tokens` is a ceiling, not a reservation, so this costs nothing extra.
Also log a warning when a response is truncated while using the defaulted
cap, so it isn't silently read as the model "doing nothing"; callers that set
`max_tokens` explicitly are unaffected.
The new id -> max_output_tokens map is kept in lockstep with ANTHROPIC_MODELS
by `scripts/sync-provider-models.ts`, so a freshly-synced model resolves to
its real ceiling rather than the fallback.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(anthropic): clamp non-streaming structured-output max_tokens default (#849)
The #849 default of the model's full output ceiling broke the non-streaming
`structuredOutput()` path: the Anthropic SDK refuses a non-streaming request
whose `max_tokens` could exceed its 10-minute timeout (~21,333 tokens), so
`chat({ outputSchema })` on any fallback-path model threw "Streaming is
required for operations that may take longer than 10 minutes".
`getAnthropicDefaultMaxTokens(model, { stream })` now clamps the default to
`ANTHROPIC_MAX_NONSTREAMING_TOKENS` when `stream: false`; the streaming chat
path keeps the model's full ceiling. An explicit oversized `max_tokens` still
surfaces the SDK's "use streaming" error.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(anthropic): align #849 max_tokens map and tests with synced model metadata
Main's OpenRouter metadata sync (e14586b) retired claude-3.x / claude-4 /
-fast model ids and added Fable 5 / Sonnet 5, so after rebasing:
- ANTHROPIC_MODEL_MAX_OUTPUT_TOKENS again mirrors ANTHROPIC_MODELS exactly
(removed retired entries, added claude-fable-5 / claude-sonnet-5).
- Tests use current ids (claude-opus-4-8, claude-opus-4-1, claude-sonnet-4-5,
claude-opus-4-5 for the 32K ceiling case).
- The 'ceiling below the non-streaming limit' test relied on claude-3-haiku's
4K ceiling; no current model sits under 21K, so it now asserts every model
in the lineup clamps on the non-streaming path instead.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: docs/adapters/anthropic.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,6 +137,12 @@ const stream = chat({
137
137
138
138
> If you previously passed `temperature` / `topP` / `maxTokens` at the root of `chat()`, see [Moving Sampling Options into modelOptions](../migration/sampling-options-to-model-options).
139
139
140
+
#### `max_tokens` default
141
+
142
+
Anthropic's Messages API _requires_`max_tokens` on every request, so the adapter always sends a value. When you don't set `modelOptions.max_tokens`, it defaults to the selected model's full output ceiling (`max_output_tokens` from the model metadata — e.g. 64K for Sonnet, 128K for Opus), falling back to a safe constant for unrecognized models. `max_tokens` is a ceiling, not a reservation — billing is on tokens actually generated — so this default costs nothing extra and avoids the silent mid-response truncation (`stop_reason: "max_tokens"`) that a low default would cause. Set `max_tokens` explicitly only when you want to _cap_ output below the model ceiling. If a response is truncated while using the default cap, the adapter logs a warning (visible with [debug logging](../advanced/debug-logging) enabled).
143
+
144
+
One exception: structured output (`chat({ outputSchema })`) on models that use the non-streaming finalization path clamps this default to ~21K tokens. The Anthropic SDK rejects a non-streaming request whose `max_tokens` could exceed its 10-minute timeout, so the full ceiling can't be used there. Streaming chat is unaffected. To raise the structured-output ceiling toward a model's true max, stream the response.
145
+
140
146
### Thinking (Extended Thinking)
141
147
142
148
Enable extended thinking with a token budget. This allows Claude to show its reasoning process, which is streamed as `thinking` chunks:
`anthropic response truncated at the default max_tokens (${defaultedMaxTokens}) for model=${model}; pass maxTokens (or modelOptions.max_tokens) to raise the output ceiling`,
0 commit comments