@ai-sdk/anthropic 3.0.104 → 3.0.107

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -463,6 +463,11 @@ console.log(reasoning); // reasoning details including redacted reasoning
463
463
  console.log(text); // text response
464
464
  ```
465
465
 
466
+ Anthropic's `max_tokens` limit includes both thinking and final text. To reserve
467
+ room for both, the provider adds `budgetTokens` to `maxOutputTokens` when
468
+ setting `max_tokens`. For known models, the combined value is capped at the
469
+ model's maximum output token limit.
470
+
466
471
  See [AI SDK UI: Chatbot](/docs/ai-sdk-ui/chatbot#reasoning) for more details
467
472
  on how to integrate reasoning into your chatbot.
468
473
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "3.0.104",
3
+ "version": "3.0.107",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@ai-sdk/provider": "3.0.14",
40
- "@ai-sdk/provider-utils": "4.0.41"
40
+ "@ai-sdk/provider-utils": "4.0.42"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "20.17.24",
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  APICallError,
3
+ InvalidResponseDataError,
3
4
  type JSONObject,
4
5
  type LanguageModelV3,
5
6
  type LanguageModelV3CallOptions,
@@ -1536,6 +1537,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1536
1537
  let stopDetails: AnthropicMessageMetadata['stopDetails'] = undefined;
1537
1538
  let container: AnthropicMessageMetadata['container'] | null = null;
1538
1539
  let isJsonResponseFromTool = false;
1540
+ let isMessageOpen = false;
1541
+ let activeMessageId: string | null | undefined;
1542
+ let hasInvalidMessageSequence = false;
1539
1543
 
1540
1544
  let blockType:
1541
1545
  | 'text'
@@ -1567,6 +1571,10 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1567
1571
  },
1568
1572
 
1569
1573
  transform(chunk, controller) {
1574
+ if (hasInvalidMessageSequence) {
1575
+ return;
1576
+ }
1577
+
1570
1578
  if (options.includeRawChunks) {
1571
1579
  controller.enqueue({ type: 'raw', rawValue: chunk.rawValue });
1572
1580
  }
@@ -2353,6 +2361,27 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
2353
2361
  }
2354
2362
 
2355
2363
  case 'message_start': {
2364
+ if (isMessageOpen) {
2365
+ if (activeMessageId === value.message.id) {
2366
+ return;
2367
+ }
2368
+
2369
+ hasInvalidMessageSequence = true;
2370
+ controller.enqueue({
2371
+ type: 'error',
2372
+ error: new InvalidResponseDataError({
2373
+ data: value,
2374
+ message:
2375
+ `Received message_start for message ${JSON.stringify(value.message.id)} ` +
2376
+ `while message ${JSON.stringify(activeMessageId)} is still open.`,
2377
+ }),
2378
+ });
2379
+ return;
2380
+ }
2381
+
2382
+ isMessageOpen = true;
2383
+ activeMessageId = value.message.id;
2384
+
2356
2385
  usage.input_tokens = value.message.usage.input_tokens;
2357
2386
  usage.cache_read_input_tokens =
2358
2387
  value.message.usage.cache_read_input_tokens ?? 0;
@@ -2512,6 +2541,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
2512
2541
  }
2513
2542
 
2514
2543
  case 'message_stop': {
2544
+ isMessageOpen = false;
2545
+ activeMessageId = undefined;
2546
+
2515
2547
  const anthropicMetadata = {
2516
2548
  usage: (rawUsage as JSONObject) ?? null,
2517
2549
  cacheCreationInputTokens,
@@ -685,11 +685,12 @@ export async function convertToAnthropicMessagesPrompt({
685
685
  (part.input.type === 'bash_code_execution' ||
686
686
  part.input.type === 'text_editor_code_execution')
687
687
  ) {
688
+ const { type: subtoolName, ...input } = part.input;
688
689
  anthropicContent.push({
689
690
  type: 'server_tool_use',
690
691
  id: part.toolCallId,
691
- name: part.input.type, // map back to subtool name
692
- input: part.input,
692
+ name: subtoolName, // map back to subtool name
693
+ input,
693
694
  cache_control: cacheControl,
694
695
  });
695
696
  } else if (
@@ -958,7 +959,19 @@ export async function convertToAnthropicMessagesPrompt({
958
959
  type: 'bash_code_execution_tool_result',
959
960
  tool_use_id: part.toolCallId,
960
961
  cache_control: cacheControl,
961
- content: codeExecutionOutput,
962
+ // Prompt caching requires stable key ordering:
963
+ // https://platform.claude.com/docs/en/build-with-claude/prompt-caching#troubleshooting-common-issues
964
+ content:
965
+ codeExecutionOutput.type ===
966
+ 'bash_code_execution_result'
967
+ ? {
968
+ type: codeExecutionOutput.type,
969
+ stdout: codeExecutionOutput.stdout,
970
+ stderr: codeExecutionOutput.stderr,
971
+ return_code: codeExecutionOutput.return_code,
972
+ content: codeExecutionOutput.content,
973
+ }
974
+ : codeExecutionOutput,
962
975
  });
963
976
  } else {
964
977
  anthropicContent.push({