@ai-sdk/anthropic 3.0.110 → 3.0.112

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "3.0.110",
3
+ "version": "3.0.112",
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.15",
40
- "@ai-sdk/provider-utils": "4.0.45"
40
+ "@ai-sdk/provider-utils": "4.0.47"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "20.17.24",
@@ -178,6 +178,7 @@ export interface AnthropicServerToolUseContent {
178
178
  // advisor:
179
179
  | 'advisor';
180
180
  input: unknown;
181
+ caller?: AnthropicToolCallCaller;
181
182
  cache_control: AnthropicCacheControl | undefined;
182
183
  }
183
184
 
@@ -239,6 +240,7 @@ export interface AnthropicWebSearchToolResultContent {
239
240
  type: 'web_search_tool_result_error';
240
241
  error_code: string;
241
242
  };
243
+ caller?: AnthropicToolCallCaller;
242
244
  cache_control: AnthropicCacheControl | undefined;
243
245
  }
244
246
 
@@ -385,6 +387,7 @@ export interface AnthropicWebFetchToolResultContent {
385
387
  type: 'web_fetch_tool_result_error';
386
388
  error_code: string;
387
389
  };
390
+ caller?: AnthropicToolCallCaller;
388
391
  cache_control: AnthropicCacheControl | undefined;
389
392
  }
390
393
  export interface AnthropicMcpToolUseContent {
@@ -624,6 +627,20 @@ const anthropicStopDetailsSchema = z.object({
624
627
 
625
628
  export type AnthropicStopDetails = z.infer<typeof anthropicStopDetailsSchema>;
626
629
 
630
+ const anthropicToolCallCallerSchema = z.union([
631
+ z.object({
632
+ type: z.literal('code_execution_20250825'),
633
+ tool_id: z.string(),
634
+ }),
635
+ z.object({
636
+ type: z.literal('code_execution_20260120'),
637
+ tool_id: z.string(),
638
+ }),
639
+ z.object({
640
+ type: z.literal('direct'),
641
+ }),
642
+ ]);
643
+
627
644
  // limited version of the schema, focussed on what is needed for the implementation
628
645
  // this approach limits breakages when the API changes and increases efficiency
629
646
  export const anthropicMessagesResponseSchema = lazySchema(() =>
@@ -686,38 +703,14 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
686
703
  name: z.string(),
687
704
  input: z.unknown(),
688
705
  // Programmatic tool calling: caller info when triggered from code execution
689
- caller: z
690
- .union([
691
- z.object({
692
- type: z.literal('code_execution_20250825'),
693
- tool_id: z.string(),
694
- }),
695
- z.object({
696
- type: z.literal('code_execution_20260120'),
697
- tool_id: z.string(),
698
- }),
699
- z.object({
700
- type: z.literal('direct'),
701
- }),
702
- ])
703
- .optional(),
706
+ caller: anthropicToolCallCallerSchema.optional(),
704
707
  }),
705
708
  z.object({
706
709
  type: z.literal('server_tool_use'),
707
710
  id: z.string(),
708
711
  name: z.string(),
709
712
  input: z.record(z.string(), z.unknown()).nullish(),
710
- caller: z
711
- .union([
712
- z.object({
713
- type: z.literal('code_execution_20260120'),
714
- tool_id: z.string(),
715
- }),
716
- z.object({
717
- type: z.literal('direct'),
718
- }),
719
- ])
720
- .optional(),
713
+ caller: anthropicToolCallCallerSchema.optional(),
721
714
  }),
722
715
  z.object({
723
716
  type: z.literal('mcp_tool_use'),
@@ -740,6 +733,7 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
740
733
  z.object({
741
734
  type: z.literal('web_fetch_tool_result'),
742
735
  tool_use_id: z.string(),
736
+ caller: anthropicToolCallCallerSchema.optional(),
743
737
  content: z.union([
744
738
  z.object({
745
739
  type: z.literal('web_fetch_result'),
@@ -772,6 +766,7 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
772
766
  z.object({
773
767
  type: z.literal('web_search_tool_result'),
774
768
  tool_use_id: z.string(),
769
+ caller: anthropicToolCallCallerSchema.optional(),
775
770
  content: z.union([
776
771
  z.array(
777
772
  z.object({
@@ -1028,21 +1023,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
1028
1023
  id: z.string(),
1029
1024
  name: z.string(),
1030
1025
  input: z.unknown(),
1031
- caller: z
1032
- .union([
1033
- z.object({
1034
- type: z.literal('code_execution_20250825'),
1035
- tool_id: z.string(),
1036
- }),
1037
- z.object({
1038
- type: z.literal('code_execution_20260120'),
1039
- tool_id: z.string(),
1040
- }),
1041
- z.object({
1042
- type: z.literal('direct'),
1043
- }),
1044
- ])
1045
- .optional(),
1026
+ caller: anthropicToolCallCallerSchema.optional(),
1046
1027
  }),
1047
1028
  ]),
1048
1029
  )
@@ -1075,21 +1056,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
1075
1056
  // Programmatic tool calling: input may be present directly for deferred tool calls
1076
1057
  input: z.record(z.string(), z.unknown()).optional(),
1077
1058
  // Programmatic tool calling: caller info when triggered from code execution
1078
- caller: z
1079
- .union([
1080
- z.object({
1081
- type: z.literal('code_execution_20250825'),
1082
- tool_id: z.string(),
1083
- }),
1084
- z.object({
1085
- type: z.literal('code_execution_20260120'),
1086
- tool_id: z.string(),
1087
- }),
1088
- z.object({
1089
- type: z.literal('direct'),
1090
- }),
1091
- ])
1092
- .optional(),
1059
+ caller: anthropicToolCallCallerSchema.optional(),
1093
1060
  }),
1094
1061
  z.object({
1095
1062
  type: z.literal('redacted_thinking'),
@@ -1104,17 +1071,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
1104
1071
  id: z.string(),
1105
1072
  name: z.string(),
1106
1073
  input: z.record(z.string(), z.unknown()).nullish(),
1107
- caller: z
1108
- .union([
1109
- z.object({
1110
- type: z.literal('code_execution_20260120'),
1111
- tool_id: z.string(),
1112
- }),
1113
- z.object({
1114
- type: z.literal('direct'),
1115
- }),
1116
- ])
1117
- .optional(),
1074
+ caller: anthropicToolCallCallerSchema.optional(),
1118
1075
  }),
1119
1076
  z.object({
1120
1077
  type: z.literal('mcp_tool_use'),
@@ -1137,6 +1094,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
1137
1094
  z.object({
1138
1095
  type: z.literal('web_fetch_tool_result'),
1139
1096
  tool_use_id: z.string(),
1097
+ caller: anthropicToolCallCallerSchema.optional(),
1140
1098
  content: z.union([
1141
1099
  z.object({
1142
1100
  type: z.literal('web_fetch_result'),
@@ -1169,6 +1127,7 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
1169
1127
  z.object({
1170
1128
  type: z.literal('web_search_tool_result'),
1171
1129
  tool_use_id: z.string(),
1130
+ caller: anthropicToolCallCallerSchema.optional(),
1172
1131
  content: z.union([
1173
1132
  z.array(
1174
1133
  z.object({
@@ -44,6 +44,7 @@ import {
44
44
  type AnthropicResponseContextManagement,
45
45
  type AnthropicStopDetails,
46
46
  type AnthropicTool,
47
+ type AnthropicToolCallCaller,
47
48
  type Citation,
48
49
  } from './anthropic-messages-api';
49
50
  import {
@@ -119,6 +120,24 @@ function createCitationSource(
119
120
  };
120
121
  }
121
122
 
123
+ function getAnthropicCallerInfo(caller: AnthropicToolCallCaller | undefined) {
124
+ return caller == null
125
+ ? undefined
126
+ : {
127
+ type: caller.type,
128
+ toolId: 'tool_id' in caller ? caller.tool_id : undefined,
129
+ };
130
+ }
131
+
132
+ function getAnthropicCallerMetadata(
133
+ caller: AnthropicToolCallCaller | undefined,
134
+ ) {
135
+ const callerInfo = getAnthropicCallerInfo(caller);
136
+ return callerInfo == null
137
+ ? {}
138
+ : { providerMetadata: { anthropic: { caller: callerInfo } } };
139
+ }
140
+
122
141
  type AnthropicMessagesConfig = {
123
142
  provider: string;
124
143
  baseURL: string;
@@ -1007,26 +1026,12 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1007
1026
  text: JSON.stringify(part.input),
1008
1027
  });
1009
1028
  } else {
1010
- const caller = part.caller;
1011
- const callerInfo = caller
1012
- ? {
1013
- type: caller.type,
1014
- toolId: 'tool_id' in caller ? caller.tool_id : undefined,
1015
- }
1016
- : undefined;
1017
-
1018
1029
  content.push({
1019
1030
  type: 'tool-call',
1020
1031
  toolCallId: part.id,
1021
1032
  toolName: part.name,
1022
1033
  input: JSON.stringify(part.input),
1023
- ...(callerInfo && {
1024
- providerMetadata: {
1025
- anthropic: {
1026
- caller: callerInfo,
1027
- },
1028
- },
1029
- }),
1034
+ ...getAnthropicCallerMetadata(part.caller),
1030
1035
  });
1031
1036
  }
1032
1037
 
@@ -1055,6 +1060,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1055
1060
  providerToolName === 'code_execution'
1056
1061
  ? { dynamic: true }
1057
1062
  : {}),
1063
+ ...getAnthropicCallerMetadata(part.caller),
1058
1064
  });
1059
1065
  } else if (
1060
1066
  part.name === 'web_search' ||
@@ -1084,6 +1090,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1084
1090
  ...(markCodeExecutionDynamic && part.name === 'code_execution'
1085
1091
  ? { dynamic: true }
1086
1092
  : {}),
1093
+ ...getAnthropicCallerMetadata(part.caller),
1087
1094
  });
1088
1095
  } else if (
1089
1096
  part.name === 'tool_search_tool_regex' ||
@@ -1096,6 +1103,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1096
1103
  toolName: toolNameMapping.toCustomToolName(part.name),
1097
1104
  input: JSON.stringify(part.input),
1098
1105
  providerExecuted: true,
1106
+ ...getAnthropicCallerMetadata(part.caller),
1099
1107
  });
1100
1108
  } else if (part.name === 'advisor') {
1101
1109
  content.push({
@@ -1104,6 +1112,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1104
1112
  toolName: toolNameMapping.toCustomToolName('advisor'),
1105
1113
  input: JSON.stringify(part.input),
1106
1114
  providerExecuted: true,
1115
+ ...getAnthropicCallerMetadata(part.caller),
1107
1116
  });
1108
1117
  }
1109
1118
 
@@ -1164,6 +1173,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1164
1173
  },
1165
1174
  },
1166
1175
  },
1176
+ ...getAnthropicCallerMetadata(part.caller),
1167
1177
  });
1168
1178
  } else if (part.content.type === 'web_fetch_tool_result_error') {
1169
1179
  content.push({
@@ -1175,6 +1185,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1175
1185
  type: 'web_fetch_tool_result_error',
1176
1186
  errorCode: part.content.error_code,
1177
1187
  },
1188
+ ...getAnthropicCallerMetadata(part.caller),
1178
1189
  });
1179
1190
  }
1180
1191
  break;
@@ -1192,6 +1203,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1192
1203
  encryptedContent: result.encrypted_content,
1193
1204
  type: result.type,
1194
1205
  })),
1206
+ ...getAnthropicCallerMetadata(part.caller),
1195
1207
  });
1196
1208
 
1197
1209
  for (const result of part.content) {
@@ -1218,6 +1230,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1218
1230
  type: 'web_search_tool_result_error',
1219
1231
  errorCode: part.content.error_code,
1220
1232
  },
1233
+ ...getAnthropicCallerMetadata(part.caller),
1221
1234
  });
1222
1235
  }
1223
1236
  break;
@@ -1680,15 +1693,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1680
1693
  id: String(value.index),
1681
1694
  });
1682
1695
  } else {
1683
- // Extract caller info for type-safe access
1684
- const caller = part.caller;
1685
- const callerInfo = caller
1686
- ? {
1687
- type: caller.type,
1688
- toolId:
1689
- 'tool_id' in caller ? caller.tool_id : undefined,
1690
- }
1691
- : undefined;
1696
+ const callerInfo = getAnthropicCallerInfo(part.caller);
1692
1697
 
1693
1698
  // Programmatic tool calling: for deferred tool calls from code_execution,
1694
1699
  // input may be present directly in content_block_start.
@@ -1718,6 +1723,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1718
1723
  }
1719
1724
 
1720
1725
  case 'server_tool_use': {
1726
+ const callerInfo = getAnthropicCallerInfo(part.caller);
1727
+
1721
1728
  if (
1722
1729
  [
1723
1730
  'web_fetch',
@@ -1774,6 +1781,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1774
1781
  firstDelta: finalInput.length === 0,
1775
1782
  providerToolName,
1776
1783
  providerToolInputType,
1784
+ ...(callerInfo && { caller: callerInfo }),
1777
1785
  };
1778
1786
 
1779
1787
  controller.enqueue({
@@ -1807,6 +1815,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1807
1815
  providerExecuted: true,
1808
1816
  firstDelta: true,
1809
1817
  providerToolName: part.name,
1818
+ ...(callerInfo && { caller: callerInfo }),
1810
1819
  };
1811
1820
 
1812
1821
  controller.enqueue({
@@ -1827,6 +1836,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1827
1836
  providerExecuted: true,
1828
1837
  firstDelta: true,
1829
1838
  providerToolName: part.name,
1839
+ ...(callerInfo && { caller: callerInfo }),
1830
1840
  };
1831
1841
 
1832
1842
  controller.enqueue({
@@ -1865,6 +1875,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1865
1875
  },
1866
1876
  },
1867
1877
  },
1878
+ ...getAnthropicCallerMetadata(part.caller),
1868
1879
  });
1869
1880
  } else if (
1870
1881
  part.content.type === 'web_fetch_tool_result_error'
@@ -1878,6 +1889,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1878
1889
  type: 'web_fetch_tool_result_error',
1879
1890
  errorCode: part.content.error_code,
1880
1891
  },
1892
+ ...getAnthropicCallerMetadata(part.caller),
1881
1893
  });
1882
1894
  }
1883
1895
 
@@ -1897,6 +1909,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1897
1909
  encryptedContent: result.encrypted_content,
1898
1910
  type: result.type,
1899
1911
  })),
1912
+ ...getAnthropicCallerMetadata(part.caller),
1900
1913
  });
1901
1914
 
1902
1915
  for (const result of part.content) {
@@ -1923,6 +1936,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1923
1936
  type: 'web_search_tool_result_error',
1924
1937
  errorCode: part.content.error_code,
1925
1938
  },
1939
+ ...getAnthropicCallerMetadata(part.caller),
1926
1940
  });
1927
1941
  }
1928
1942
  return;
@@ -2429,14 +2443,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
2429
2443
  ) {
2430
2444
  const part = value.message.content[contentIndex];
2431
2445
  if (part.type === 'tool_use') {
2432
- const caller = part.caller;
2433
- const callerInfo = caller
2434
- ? {
2435
- type: caller.type,
2436
- toolId:
2437
- 'tool_id' in caller ? caller.tool_id : undefined,
2438
- }
2439
- : undefined;
2446
+ const callerInfo = getAnthropicCallerInfo(part.caller);
2440
2447
 
2441
2448
  controller.enqueue({
2442
2449
  type: 'tool-input-start',
@@ -23,6 +23,7 @@ import {
23
23
  type AnthropicSystemMessage,
24
24
  type AnthropicTextContent,
25
25
  type AnthropicToolChangeContent,
26
+ type AnthropicToolCallCaller,
26
27
  type AnthropicToolResultContent,
27
28
  type AnthropicUserMessage,
28
29
  type AnthropicWebFetchToolResultContent,
@@ -645,6 +646,8 @@ export async function convertToAnthropicMessagesPrompt({
645
646
  }
646
647
 
647
648
  case 'tool-call': {
649
+ const caller = getAnthropicCaller(part.providerOptions);
650
+
648
651
  if (part.providerExecuted) {
649
652
  const providerToolName = toolNameMapping.toProviderToolName(
650
653
  part.toolName,
@@ -691,6 +694,7 @@ export async function convertToAnthropicMessagesPrompt({
691
694
  id: part.toolCallId,
692
695
  name: subtoolName, // map back to subtool name
693
696
  input,
697
+ ...(caller && { caller }),
694
698
  cache_control: cacheControl,
695
699
  });
696
700
  } else if (
@@ -711,6 +715,7 @@ export async function convertToAnthropicMessagesPrompt({
711
715
  id: part.toolCallId,
712
716
  name: 'code_execution',
713
717
  input: inputWithoutType,
718
+ ...(caller && { caller }),
714
719
  cache_control: cacheControl,
715
720
  });
716
721
  } else {
@@ -724,6 +729,7 @@ export async function convertToAnthropicMessagesPrompt({
724
729
  id: part.toolCallId,
725
730
  name: providerToolName,
726
731
  input: part.input,
732
+ ...(caller && { caller }),
727
733
  cache_control: cacheControl,
728
734
  });
729
735
  } else if (
@@ -735,6 +741,7 @@ export async function convertToAnthropicMessagesPrompt({
735
741
  id: part.toolCallId,
736
742
  name: providerToolName,
737
743
  input: part.input,
744
+ ...(caller && { caller }),
738
745
  cache_control: cacheControl,
739
746
  });
740
747
  } else if (providerToolName === 'advisor') {
@@ -744,6 +751,7 @@ export async function convertToAnthropicMessagesPrompt({
744
751
  id: part.toolCallId,
745
752
  name: 'advisor',
746
753
  input: {},
754
+ ...(caller && { caller }),
747
755
  cache_control: cacheControl,
748
756
  });
749
757
  } else {
@@ -757,26 +765,6 @@ export async function convertToAnthropicMessagesPrompt({
757
765
  break;
758
766
  }
759
767
 
760
- // Extract caller info from provider options for programmatic tool calling
761
- const callerOptions = part.providerOptions?.anthropic as
762
- | { caller?: { type: string; toolId?: string } }
763
- | undefined;
764
- const caller = callerOptions?.caller
765
- ? (callerOptions.caller.type === 'code_execution_20250825' ||
766
- callerOptions.caller.type ===
767
- 'code_execution_20260120') &&
768
- callerOptions.caller.toolId
769
- ? {
770
- type: callerOptions.caller.type as
771
- | 'code_execution_20250825'
772
- | 'code_execution_20260120',
773
- tool_id: callerOptions.caller.toolId,
774
- }
775
- : callerOptions.caller.type === 'direct'
776
- ? { type: 'direct' as const }
777
- : undefined
778
- : undefined;
779
-
780
768
  anthropicContent.push({
781
769
  type: 'tool_use',
782
770
  id: part.toolCallId,
@@ -792,6 +780,7 @@ export async function convertToAnthropicMessagesPrompt({
792
780
  const providerToolName = toolNameMapping.toProviderToolName(
793
781
  part.toolName,
794
782
  );
783
+ const caller = getAnthropicCaller(part.providerOptions);
795
784
 
796
785
  if (mcpToolUseIds.has(part.toolCallId)) {
797
786
  const output = part.output;
@@ -998,6 +987,7 @@ export async function convertToAnthropicMessagesPrompt({
998
987
  (await extractErrorValue(output.value)).errorCode ??
999
988
  'unavailable',
1000
989
  },
990
+ ...(caller && { caller }),
1001
991
  cache_control: cacheControl,
1002
992
  });
1003
993
 
@@ -1042,6 +1032,7 @@ export async function convertToAnthropicMessagesPrompt({
1042
1032
  >['content']['source'],
1043
1033
  },
1044
1034
  },
1035
+ ...(caller && { caller }),
1045
1036
  cache_control: cacheControl,
1046
1037
  });
1047
1038
 
@@ -1061,6 +1052,7 @@ export async function convertToAnthropicMessagesPrompt({
1061
1052
  (await extractErrorValue(output.value)).errorCode ??
1062
1053
  'unavailable',
1063
1054
  },
1055
+ ...(caller && { caller }),
1064
1056
  cache_control: cacheControl,
1065
1057
  });
1066
1058
 
@@ -1094,6 +1086,7 @@ export async function convertToAnthropicMessagesPrompt({
1094
1086
  encrypted_content: result.encryptedContent,
1095
1087
  type: result.type,
1096
1088
  })),
1089
+ ...(caller && { caller }),
1097
1090
  cache_control: cacheControl,
1098
1091
  });
1099
1092
 
@@ -1319,3 +1312,26 @@ function moveToolUseBlocksToEnd(
1319
1312
 
1320
1313
  return result;
1321
1314
  }
1315
+
1316
+ function getAnthropicCaller(
1317
+ providerOptions: SharedV3ProviderMetadata | undefined,
1318
+ ): AnthropicToolCallCaller | undefined {
1319
+ const caller = (
1320
+ providerOptions?.anthropic as
1321
+ | { caller?: { type: string; toolId?: string } }
1322
+ | undefined
1323
+ )?.caller;
1324
+
1325
+ if (
1326
+ (caller?.type === 'code_execution_20250825' ||
1327
+ caller?.type === 'code_execution_20260120') &&
1328
+ caller.toolId
1329
+ ) {
1330
+ return {
1331
+ type: caller.type,
1332
+ tool_id: caller.toolId,
1333
+ };
1334
+ }
1335
+
1336
+ return caller?.type === 'direct' ? { type: 'direct' } : undefined;
1337
+ }