@ai-sdk/anthropic 4.0.83 → 4.0.85

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.
@@ -1,17 +1,61 @@
1
1
  ---
2
2
  title: Anthropic
3
- description: Learn how to use the Anthropic provider for the AI SDK.
3
+ description: Learn how to use Anthropic with AI Gateway and the direct provider.
4
4
  ---
5
5
 
6
6
  # Anthropic Provider
7
7
 
8
8
  The [Anthropic](https://www.anthropic.com/) provider contains language model support for the [Anthropic Messages API](https://docs.anthropic.com/claude/reference/messages_post).
9
+ You can use Anthropic models through [AI Gateway](/providers/ai-sdk-providers/ai-gateway) (recommended) or connect directly with `@ai-sdk/anthropic`.
9
10
 
10
- ## Setup
11
+ ```ts provider="anthropic" model="anthropic/claude-sonnet-5.5"
12
+ import { generateText } from 'ai';
13
+ __PROVIDER_IMPORT__;
14
+
15
+ const { text } = await generateText({
16
+ model: __MODEL__,
17
+ prompt: 'Write a vegetarian lasagna recipe for 4 people.',
18
+ });
19
+
20
+ console.log(text);
21
+ ```
22
+
23
+ ## Using AI Gateway
24
+
25
+ The AI SDK uses AI Gateway by default for model strings. Gateway provides access to models across providers through a single API key, with usage and cost tracking. Install `ai`:
26
+
27
+ <InstallPackages packages="ai" />
28
+
29
+ Set an [AI Gateway API key](https://vercel.com/docs/ai-gateway/getting-started) in your environment:
11
30
 
12
- The Anthropic provider is available in the `@ai-sdk/anthropic` module. You can install it with
31
+ ```bash
32
+ export AI_GATEWAY_API_KEY=your_api_key_here
33
+ ```
34
+
35
+ Vercel deployments can also use [OIDC authentication](/providers/ai-sdk-providers/ai-gateway#oidc-authentication-vercel-deployments).
36
+ See the [AI Gateway provider documentation](/providers/ai-sdk-providers/ai-gateway) for configuration and the [model catalog](https://vercel.com/ai-gateway/models) for available models.
37
+
38
+ Gateway feature and provider-option support depends on the model and the provider serving the request. See [Gateway provider options](https://vercel.com/docs/ai-gateway/models-and-providers/provider-options).
39
+
40
+ ## Using `@ai-sdk/anthropic`
41
+
42
+ For direct access to Anthropic, install `ai` and `@ai-sdk/anthropic`:
43
+
44
+ <InstallPackages packages="ai @ai-sdk/anthropic" />
13
45
 
14
- <InstallPackages packages="@ai-sdk/anthropic" />
46
+ Set your Anthropic API credential in the `ANTHROPIC_API_KEY` environment variable:
47
+
48
+ ```bash
49
+ export ANTHROPIC_API_KEY=your_api_key_here
50
+ ```
51
+
52
+ AI Gateway does not expose every API in `@ai-sdk/anthropic`. Use the direct provider for provider-specific APIs or custom connection settings.
53
+
54
+ <Note>
55
+ The following sections document the direct `@ai-sdk/anthropic` provider,
56
+ including its model factories and settings. Gateway model and feature
57
+ availability can differ.
58
+ </Note>
15
59
 
16
60
  ## Provider Instance
17
61
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "4.0.83",
3
+ "version": "4.0.85",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -35,13 +35,14 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@ai-sdk/provider": "4.0.30",
39
- "@ai-sdk/provider-utils": "5.0.63"
38
+ "@ai-sdk/provider": "4.0.31",
39
+ "@ai-sdk/provider-utils": "5.0.65"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@ai-sdk/test-server": "2.0.4",
43
43
  "@types/node": "22.19.19",
44
44
  "@vercel/ai-tsconfig": "0.0.0",
45
+ "@vercel/ai-tsdown-config": "0.0.0",
45
46
  "tsdown": "^0.23.0",
46
47
  "typescript": "6.0.3",
47
48
  "zod": "3.25.76"
@@ -48,6 +48,7 @@ export interface AnthropicUserMessage {
48
48
  content: Array<
49
49
  | AnthropicTextContent
50
50
  | AnthropicImageContent
51
+ | AnthropicVideoContent
51
52
  | AnthropicDocumentContent
52
53
  | AnthropicContainerUploadContent
53
54
  | AnthropicToolResultContent
@@ -76,14 +77,18 @@ export interface AnthropicAssistantMessage {
76
77
  >;
77
78
  }
78
79
 
79
- export const anthropicFallbackContentSchema = z.object({
80
- type: z.literal('fallback'),
81
- from: z.object({ model: z.string() }),
82
- to: z.object({ model: z.string() }),
83
- });
80
+ export const anthropicFallbackContentSchema = () =>
81
+ z.object({
82
+ type: z.literal('fallback'),
83
+ from: z.object({ model: z.string() }),
84
+ to: z.object({ model: z.string() }),
85
+ });
86
+ export const anthropicFallbackContentSchemaValidator = lazySchema(() =>
87
+ zodSchema(anthropicFallbackContentSchema()),
88
+ );
84
89
 
85
90
  export type AnthropicFallbackContent = InferSchema<
86
- typeof anthropicFallbackContentSchema
91
+ ReturnType<typeof anthropicFallbackContentSchema>
87
92
  > & {
88
93
  cache_control?: never;
89
94
  };
@@ -145,6 +150,13 @@ export interface AnthropicImageContent {
145
150
  cache_control: AnthropicCacheControl | undefined;
146
151
  }
147
152
 
153
+ /** Video blocks supplied by compatible providers through custom media conversion. */
154
+ export interface AnthropicVideoContent {
155
+ type: 'video';
156
+ source: AnthropicContentSource;
157
+ cache_control: AnthropicCacheControl | undefined;
158
+ }
159
+
148
160
  export interface AnthropicDocumentContent {
149
161
  type: 'document';
150
162
  source: AnthropicContentSource;
@@ -681,117 +693,125 @@ export type AnthropicResponseContextManagement = {
681
693
  applied_edits: AnthropicResponseContextManagementEdit[];
682
694
  };
683
695
 
684
- const anthropicStopDetailsSchema = z.object({
685
- type: z.string(),
686
- category: z.string().nullish(),
687
- explanation: z.string().nullish(),
688
- recommended_model: z.string().nullish(),
689
- });
696
+ const anthropicStopDetailsSchema = () =>
697
+ z.object({
698
+ type: z.string(),
699
+ category: z.string().nullish(),
700
+ explanation: z.string().nullish(),
701
+ recommended_model: z.string().nullish(),
702
+ });
690
703
 
691
- export type AnthropicStopDetails = z.infer<typeof anthropicStopDetailsSchema>;
704
+ export type AnthropicStopDetails = z.infer<
705
+ ReturnType<typeof anthropicStopDetailsSchema>
706
+ >;
692
707
 
693
- const anthropicSafeguardResultSchema = z.object({
694
- type: z.string(),
695
- status: z.object({
708
+ const anthropicSafeguardResultSchema = () =>
709
+ z.object({
696
710
  type: z.string(),
697
- tool_uses: z
698
- .record(
699
- z.string(),
700
- z.object({
701
- type: z.string(),
702
- outcome: z.string().nullish(),
703
- explanation: z.string().nullish(),
704
- }),
705
- )
706
- .nullish(),
707
- }),
708
- });
711
+ status: z.object({
712
+ type: z.string(),
713
+ tool_uses: z
714
+ .record(
715
+ z.string(),
716
+ z.object({
717
+ type: z.string(),
718
+ outcome: z.string().nullish(),
719
+ explanation: z.string().nullish(),
720
+ }),
721
+ )
722
+ .nullish(),
723
+ }),
724
+ });
709
725
 
710
726
  export type AnthropicSafeguardResult = z.infer<
711
- typeof anthropicSafeguardResultSchema
727
+ ReturnType<typeof anthropicSafeguardResultSchema>
712
728
  >;
713
729
 
714
- const anthropicToolCallCallerSchema = z.union([
715
- z.object({
716
- type: z.literal('code_execution_20250825'),
717
- tool_id: z.string(),
718
- }),
719
- z.object({
720
- type: z.literal('code_execution_20260120'),
721
- tool_id: z.string(),
722
- }),
723
- z.object({
724
- type: z.literal('direct'),
725
- }),
726
- ]);
730
+ const anthropicToolCallCallerSchema = () =>
731
+ z.union([
732
+ z.object({
733
+ type: z.literal('code_execution_20250825'),
734
+ tool_id: z.string(),
735
+ }),
736
+ z.object({
737
+ type: z.literal('code_execution_20260120'),
738
+ tool_id: z.string(),
739
+ }),
740
+ z.object({
741
+ type: z.literal('direct'),
742
+ }),
743
+ ]);
727
744
 
728
- const anthropicCitationSchema = z.discriminatedUnion('type', [
729
- z.object({
730
- type: z.literal('web_search_result_location'),
731
- cited_text: z.string(),
732
- url: z.string(),
733
- title: z.string().nullable(),
734
- encrypted_index: z.string(),
735
- }),
736
- z.object({
737
- type: z.literal('page_location'),
738
- cited_text: z.string(),
739
- document_index: z.number(),
740
- document_title: z.string().nullable(),
741
- start_page_number: z.number(),
742
- end_page_number: z.number(),
743
- file_id: z.string().nullish(),
744
- }),
745
- z.object({
746
- type: z.literal('char_location'),
747
- cited_text: z.string(),
748
- document_index: z.number(),
749
- document_title: z.string().nullable(),
750
- start_char_index: z.number(),
751
- end_char_index: z.number(),
752
- file_id: z.string().nullish(),
753
- }),
754
- z.object({
755
- type: z.literal('content_block_location'),
756
- cited_text: z.string(),
757
- document_index: z.number(),
758
- document_title: z.string().nullable(),
759
- start_block_index: z.number(),
760
- end_block_index: z.number(),
761
- file_id: z.string().nullable(),
762
- }),
763
- z.object({
764
- type: z.literal('search_result_location'),
765
- cited_text: z.string(),
766
- search_result_index: z.number(),
767
- source: z.string(),
768
- title: z.string().nullable(),
769
- start_block_index: z.number(),
770
- end_block_index: z.number(),
771
- }),
772
- ]);
773
-
774
- const anthropicMcpToolResultContentSchema = z.union([
775
- z.string(),
776
- z.array(
777
- z.union([
778
- z.string(),
779
- z.object({
780
- type: z.literal('text'),
781
- text: z.string(),
782
- // MCP tool-result citations are opaque third-party pass-through data.
783
- // Keep them permissive so new shapes do not reject whole responses.
784
- citations: z.array(z.json()).nullable().optional(),
785
- }),
786
- ]),
787
- ),
788
- ]);
745
+ const anthropicCitationSchema = () =>
746
+ z.discriminatedUnion('type', [
747
+ z.object({
748
+ type: z.literal('web_search_result_location'),
749
+ cited_text: z.string(),
750
+ url: z.string(),
751
+ title: z.string().nullable(),
752
+ encrypted_index: z.string(),
753
+ }),
754
+ z.object({
755
+ type: z.literal('page_location'),
756
+ cited_text: z.string(),
757
+ document_index: z.number(),
758
+ document_title: z.string().nullable(),
759
+ start_page_number: z.number(),
760
+ end_page_number: z.number(),
761
+ file_id: z.string().nullish(),
762
+ }),
763
+ z.object({
764
+ type: z.literal('char_location'),
765
+ cited_text: z.string(),
766
+ document_index: z.number(),
767
+ document_title: z.string().nullable(),
768
+ start_char_index: z.number(),
769
+ end_char_index: z.number(),
770
+ file_id: z.string().nullish(),
771
+ }),
772
+ z.object({
773
+ type: z.literal('content_block_location'),
774
+ cited_text: z.string(),
775
+ document_index: z.number(),
776
+ document_title: z.string().nullable(),
777
+ start_block_index: z.number(),
778
+ end_block_index: z.number(),
779
+ file_id: z.string().nullable(),
780
+ }),
781
+ z.object({
782
+ type: z.literal('search_result_location'),
783
+ cited_text: z.string(),
784
+ search_result_index: z.number(),
785
+ source: z.string(),
786
+ title: z.string().nullable(),
787
+ start_block_index: z.number(),
788
+ end_block_index: z.number(),
789
+ }),
790
+ ]);
789
791
 
790
- const anthropicInputTransformationSchema = z.object({
791
- type: z.string(),
792
- path: z.string(),
793
- reason: z.string(),
794
- });
792
+ const anthropicMcpToolResultContentSchema = () =>
793
+ z.union([
794
+ z.string(),
795
+ z.array(
796
+ z.union([
797
+ z.string(),
798
+ z.object({
799
+ type: z.literal('text'),
800
+ text: z.string(),
801
+ // MCP tool-result citations are opaque third-party pass-through data.
802
+ // Keep them permissive so new shapes do not reject whole responses.
803
+ citations: z.array(z.json()).nullable().optional(),
804
+ }),
805
+ ]),
806
+ ),
807
+ ]);
808
+
809
+ const anthropicInputTransformationSchema = () =>
810
+ z.object({
811
+ type: z.string(),
812
+ path: z.string(),
813
+ reason: z.string(),
814
+ });
795
815
 
796
816
  // limited version of the schema, focussed on what is needed for the implementation
797
817
  // this approach limits breakages when the API changes and increases efficiency
@@ -806,7 +826,7 @@ export const anthropicResponseSchema = lazySchema(() =>
806
826
  z.object({
807
827
  type: z.literal('text'),
808
828
  text: z.string(),
809
- citations: z.array(anthropicCitationSchema).nullable().optional(),
829
+ citations: z.array(anthropicCitationSchema()).nullable().optional(),
810
830
  }),
811
831
  z.object({
812
832
  type: z.literal('thinking'),
@@ -828,7 +848,7 @@ export const anthropicResponseSchema = lazySchema(() =>
828
848
  name: z.string(),
829
849
  input: z.unknown(),
830
850
  // Programmatic tool calling: caller info when triggered from code execution
831
- caller: anthropicToolCallCallerSchema.optional(),
851
+ caller: anthropicToolCallCallerSchema().optional(),
832
852
  // Toolsets (e.g. computer toolset): name of the toolset this member call belongs to
833
853
  toolset_name: z.string().nullish(),
834
854
  }),
@@ -837,7 +857,7 @@ export const anthropicResponseSchema = lazySchema(() =>
837
857
  id: z.string(),
838
858
  name: z.string(),
839
859
  input: z.record(z.string(), z.unknown()).nullish(),
840
- caller: anthropicToolCallCallerSchema.optional(),
860
+ caller: anthropicToolCallCallerSchema().optional(),
841
861
  }),
842
862
  z.object({
843
863
  type: z.literal('mcp_tool_use'),
@@ -850,12 +870,12 @@ export const anthropicResponseSchema = lazySchema(() =>
850
870
  type: z.literal('mcp_tool_result'),
851
871
  tool_use_id: z.string(),
852
872
  is_error: z.boolean(),
853
- content: anthropicMcpToolResultContentSchema,
873
+ content: anthropicMcpToolResultContentSchema(),
854
874
  }),
855
875
  z.object({
856
876
  type: z.literal('web_fetch_tool_result'),
857
877
  tool_use_id: z.string(),
858
- caller: anthropicToolCallCallerSchema.optional(),
878
+ caller: anthropicToolCallCallerSchema().optional(),
859
879
  content: z.union([
860
880
  z.object({
861
881
  type: z.literal('web_fetch_result'),
@@ -888,7 +908,7 @@ export const anthropicResponseSchema = lazySchema(() =>
888
908
  z.object({
889
909
  type: z.literal('web_search_tool_result'),
890
910
  tool_use_id: z.string(),
891
- caller: anthropicToolCallCallerSchema.optional(),
911
+ caller: anthropicToolCallCallerSchema().optional(),
892
912
  content: z.union([
893
913
  z.array(
894
914
  z.object({
@@ -1047,16 +1067,16 @@ export const anthropicResponseSchema = lazySchema(() =>
1047
1067
  }),
1048
1068
  ]),
1049
1069
  }),
1050
- anthropicFallbackContentSchema,
1070
+ anthropicFallbackContentSchema(),
1051
1071
  ]),
1052
1072
  ),
1053
1073
  stop_reason: z.string().nullish(),
1054
1074
  stop_sequence: z.string().nullish(),
1055
- stop_details: anthropicStopDetailsSchema.nullish(),
1075
+ stop_details: anthropicStopDetailsSchema().nullish(),
1056
1076
  input_transformations: z
1057
- .array(anthropicInputTransformationSchema)
1077
+ .array(anthropicInputTransformationSchema())
1058
1078
  .nullish(),
1059
- safeguard_results: z.array(anthropicSafeguardResultSchema).nullish(),
1079
+ safeguard_results: z.array(anthropicSafeguardResultSchema()).nullish(),
1060
1080
  usage: z.looseObject({
1061
1081
  input_tokens: z.number(),
1062
1082
  output_tokens: z.number(),
@@ -1150,7 +1170,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1150
1170
  id: z.string(),
1151
1171
  name: z.string(),
1152
1172
  input: z.unknown(),
1153
- caller: anthropicToolCallCallerSchema.optional(),
1173
+ caller: anthropicToolCallCallerSchema().optional(),
1154
1174
  toolset_name: z.string().nullish(),
1155
1175
  }),
1156
1176
  ]),
@@ -1158,7 +1178,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1158
1178
  .nullish(),
1159
1179
  stop_reason: z.string().nullish(),
1160
1180
  input_transformations: z
1161
- .array(anthropicInputTransformationSchema)
1181
+ .array(anthropicInputTransformationSchema())
1162
1182
  .nullish(),
1163
1183
  container: z
1164
1184
  .object({
@@ -1187,7 +1207,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1187
1207
  // Programmatic tool calling: input may be present directly for deferred tool calls
1188
1208
  input: z.record(z.string(), z.unknown()).optional(),
1189
1209
  // Programmatic tool calling: caller info when triggered from code execution
1190
- caller: anthropicToolCallCallerSchema.optional(),
1210
+ caller: anthropicToolCallCallerSchema().optional(),
1191
1211
  // Toolsets (e.g. computer toolset): name of the toolset this member call belongs to
1192
1212
  toolset_name: z.string().nullish(),
1193
1213
  }),
@@ -1205,7 +1225,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1205
1225
  id: z.string(),
1206
1226
  name: z.string(),
1207
1227
  input: z.record(z.string(), z.unknown()).nullish(),
1208
- caller: anthropicToolCallCallerSchema.optional(),
1228
+ caller: anthropicToolCallCallerSchema().optional(),
1209
1229
  }),
1210
1230
  z.object({
1211
1231
  type: z.literal('mcp_tool_use'),
@@ -1218,12 +1238,12 @@ export const anthropicChunkSchema = lazySchema(() =>
1218
1238
  type: z.literal('mcp_tool_result'),
1219
1239
  tool_use_id: z.string(),
1220
1240
  is_error: z.boolean(),
1221
- content: anthropicMcpToolResultContentSchema,
1241
+ content: anthropicMcpToolResultContentSchema(),
1222
1242
  }),
1223
1243
  z.object({
1224
1244
  type: z.literal('web_fetch_tool_result'),
1225
1245
  tool_use_id: z.string(),
1226
- caller: anthropicToolCallCallerSchema.optional(),
1246
+ caller: anthropicToolCallCallerSchema().optional(),
1227
1247
  content: z.union([
1228
1248
  z.object({
1229
1249
  type: z.literal('web_fetch_result'),
@@ -1256,7 +1276,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1256
1276
  z.object({
1257
1277
  type: z.literal('web_search_tool_result'),
1258
1278
  tool_use_id: z.string(),
1259
- caller: anthropicToolCallCallerSchema.optional(),
1279
+ caller: anthropicToolCallCallerSchema().optional(),
1260
1280
  content: z.union([
1261
1281
  z.array(
1262
1282
  z.object({
@@ -1411,7 +1431,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1411
1431
  }),
1412
1432
  ]),
1413
1433
  }),
1414
- anthropicFallbackContentSchema,
1434
+ anthropicFallbackContentSchema(),
1415
1435
  ]),
1416
1436
  }),
1417
1437
  z.object({
@@ -1440,7 +1460,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1440
1460
  }),
1441
1461
  z.object({
1442
1462
  type: z.literal('citations_delta'),
1443
- citation: anthropicCitationSchema,
1463
+ citation: anthropicCitationSchema(),
1444
1464
  }),
1445
1465
  ]),
1446
1466
  }),
@@ -1464,8 +1484,10 @@ export const anthropicChunkSchema = lazySchema(() =>
1464
1484
  delta: z.object({
1465
1485
  stop_reason: z.string().nullish(),
1466
1486
  stop_sequence: z.string().nullish(),
1467
- stop_details: anthropicStopDetailsSchema.nullish(),
1468
- safeguard_results: z.array(anthropicSafeguardResultSchema).nullish(),
1487
+ stop_details: anthropicStopDetailsSchema().nullish(),
1488
+ safeguard_results: z
1489
+ .array(anthropicSafeguardResultSchema())
1490
+ .nullish(),
1469
1491
  container: z
1470
1492
  .object({
1471
1493
  expires_at: z.string(),
@@ -1514,7 +1536,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1514
1536
  .nullish(),
1515
1537
  }),
1516
1538
  input_transformations: z
1517
- .array(anthropicInputTransformationSchema)
1539
+ .array(anthropicInputTransformationSchema())
1518
1540
  .nullish(),
1519
1541
  context_management: z
1520
1542
  .object({
@@ -49,10 +49,7 @@ import {
49
49
  AnthropicLanguageModel,
50
50
  type AnthropicLanguageModelConfig,
51
51
  } from './anthropic-language-model';
52
- import {
53
- anthropicLanguageModelOptions,
54
- type AnthropicModelId,
55
- } from './anthropic-language-model-options';
52
+ import type { AnthropicModelId } from './anthropic-language-model-options';
56
53
  import type {
57
54
  AnthropicMessageMetadata,
58
55
  AnthropicUsageIteration,
@@ -62,9 +59,13 @@ import { mapAnthropicStopReason } from './map-anthropic-stop-reason';
62
59
 
63
60
  const anthropicBatchRequestIdPattern = /^[A-Za-z0-9_-]{1,64}$/;
64
61
 
65
- const anthropicBatchProviderOptionsSchema = anthropicLanguageModelOptions.pick({
66
- anthropicBeta: true,
67
- });
62
+ const anthropicBatchProviderOptionsSchema = lazySchema(() =>
63
+ zodSchema(
64
+ z.object({
65
+ anthropicBeta: z.array(z.string()).optional(),
66
+ }),
67
+ ),
68
+ );
68
69
 
69
70
  type AnthropicBatchRequest = TextBatchV4Request<AnthropicModelId>;
70
71