@ai-sdk/anthropic 4.0.84 → 4.0.86

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.84",
3
+ "version": "4.0.86",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -35,8 +35,8 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@ai-sdk/provider": "4.0.31",
39
- "@ai-sdk/provider-utils": "5.0.64"
38
+ "@ai-sdk/provider": "4.0.32",
39
+ "@ai-sdk/provider-utils": "5.0.66"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@ai-sdk/test-server": "2.0.4",
@@ -77,14 +77,18 @@ export interface AnthropicAssistantMessage {
77
77
  >;
78
78
  }
79
79
 
80
- export const anthropicFallbackContentSchema = z.object({
81
- type: z.literal('fallback'),
82
- from: z.object({ model: z.string() }),
83
- to: z.object({ model: z.string() }),
84
- });
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
+ );
85
89
 
86
90
  export type AnthropicFallbackContent = InferSchema<
87
- typeof anthropicFallbackContentSchema
91
+ ReturnType<typeof anthropicFallbackContentSchema>
88
92
  > & {
89
93
  cache_control?: never;
90
94
  };
@@ -689,117 +693,125 @@ export type AnthropicResponseContextManagement = {
689
693
  applied_edits: AnthropicResponseContextManagementEdit[];
690
694
  };
691
695
 
692
- const anthropicStopDetailsSchema = z.object({
693
- type: z.string(),
694
- category: z.string().nullish(),
695
- explanation: z.string().nullish(),
696
- recommended_model: z.string().nullish(),
697
- });
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
+ });
698
703
 
699
- export type AnthropicStopDetails = z.infer<typeof anthropicStopDetailsSchema>;
704
+ export type AnthropicStopDetails = z.infer<
705
+ ReturnType<typeof anthropicStopDetailsSchema>
706
+ >;
700
707
 
701
- const anthropicSafeguardResultSchema = z.object({
702
- type: z.string(),
703
- status: z.object({
708
+ const anthropicSafeguardResultSchema = () =>
709
+ z.object({
704
710
  type: z.string(),
705
- tool_uses: z
706
- .record(
707
- z.string(),
708
- z.object({
709
- type: z.string(),
710
- outcome: z.string().nullish(),
711
- explanation: z.string().nullish(),
712
- }),
713
- )
714
- .nullish(),
715
- }),
716
- });
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
+ });
717
725
 
718
726
  export type AnthropicSafeguardResult = z.infer<
719
- typeof anthropicSafeguardResultSchema
727
+ ReturnType<typeof anthropicSafeguardResultSchema>
720
728
  >;
721
729
 
722
- const anthropicToolCallCallerSchema = z.union([
723
- z.object({
724
- type: z.literal('code_execution_20250825'),
725
- tool_id: z.string(),
726
- }),
727
- z.object({
728
- type: z.literal('code_execution_20260120'),
729
- tool_id: z.string(),
730
- }),
731
- z.object({
732
- type: z.literal('direct'),
733
- }),
734
- ]);
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
+ ]);
735
744
 
736
- const anthropicCitationSchema = z.discriminatedUnion('type', [
737
- z.object({
738
- type: z.literal('web_search_result_location'),
739
- cited_text: z.string(),
740
- url: z.string(),
741
- title: z.string().nullable(),
742
- encrypted_index: z.string(),
743
- }),
744
- z.object({
745
- type: z.literal('page_location'),
746
- cited_text: z.string(),
747
- document_index: z.number(),
748
- document_title: z.string().nullable(),
749
- start_page_number: z.number(),
750
- end_page_number: z.number(),
751
- file_id: z.string().nullish(),
752
- }),
753
- z.object({
754
- type: z.literal('char_location'),
755
- cited_text: z.string(),
756
- document_index: z.number(),
757
- document_title: z.string().nullable(),
758
- start_char_index: z.number(),
759
- end_char_index: z.number(),
760
- file_id: z.string().nullish(),
761
- }),
762
- z.object({
763
- type: z.literal('content_block_location'),
764
- cited_text: z.string(),
765
- document_index: z.number(),
766
- document_title: z.string().nullable(),
767
- start_block_index: z.number(),
768
- end_block_index: z.number(),
769
- file_id: z.string().nullable(),
770
- }),
771
- z.object({
772
- type: z.literal('search_result_location'),
773
- cited_text: z.string(),
774
- search_result_index: z.number(),
775
- source: z.string(),
776
- title: z.string().nullable(),
777
- start_block_index: z.number(),
778
- end_block_index: z.number(),
779
- }),
780
- ]);
781
-
782
- const anthropicMcpToolResultContentSchema = z.union([
783
- z.string(),
784
- z.array(
785
- z.union([
786
- z.string(),
787
- z.object({
788
- type: z.literal('text'),
789
- text: z.string(),
790
- // MCP tool-result citations are opaque third-party pass-through data.
791
- // Keep them permissive so new shapes do not reject whole responses.
792
- citations: z.array(z.json()).nullable().optional(),
793
- }),
794
- ]),
795
- ),
796
- ]);
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
+ ]);
791
+
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
+ ]);
797
808
 
798
- const anthropicInputTransformationSchema = z.object({
799
- type: z.string(),
800
- path: z.string(),
801
- reason: z.string(),
802
- });
809
+ const anthropicInputTransformationSchema = () =>
810
+ z.object({
811
+ type: z.string(),
812
+ path: z.string(),
813
+ reason: z.string(),
814
+ });
803
815
 
804
816
  // limited version of the schema, focussed on what is needed for the implementation
805
817
  // this approach limits breakages when the API changes and increases efficiency
@@ -814,7 +826,7 @@ export const anthropicResponseSchema = lazySchema(() =>
814
826
  z.object({
815
827
  type: z.literal('text'),
816
828
  text: z.string(),
817
- citations: z.array(anthropicCitationSchema).nullable().optional(),
829
+ citations: z.array(anthropicCitationSchema()).nullable().optional(),
818
830
  }),
819
831
  z.object({
820
832
  type: z.literal('thinking'),
@@ -836,7 +848,7 @@ export const anthropicResponseSchema = lazySchema(() =>
836
848
  name: z.string(),
837
849
  input: z.unknown(),
838
850
  // Programmatic tool calling: caller info when triggered from code execution
839
- caller: anthropicToolCallCallerSchema.optional(),
851
+ caller: anthropicToolCallCallerSchema().optional(),
840
852
  // Toolsets (e.g. computer toolset): name of the toolset this member call belongs to
841
853
  toolset_name: z.string().nullish(),
842
854
  }),
@@ -845,7 +857,7 @@ export const anthropicResponseSchema = lazySchema(() =>
845
857
  id: z.string(),
846
858
  name: z.string(),
847
859
  input: z.record(z.string(), z.unknown()).nullish(),
848
- caller: anthropicToolCallCallerSchema.optional(),
860
+ caller: anthropicToolCallCallerSchema().optional(),
849
861
  }),
850
862
  z.object({
851
863
  type: z.literal('mcp_tool_use'),
@@ -858,12 +870,12 @@ export const anthropicResponseSchema = lazySchema(() =>
858
870
  type: z.literal('mcp_tool_result'),
859
871
  tool_use_id: z.string(),
860
872
  is_error: z.boolean(),
861
- content: anthropicMcpToolResultContentSchema,
873
+ content: anthropicMcpToolResultContentSchema(),
862
874
  }),
863
875
  z.object({
864
876
  type: z.literal('web_fetch_tool_result'),
865
877
  tool_use_id: z.string(),
866
- caller: anthropicToolCallCallerSchema.optional(),
878
+ caller: anthropicToolCallCallerSchema().optional(),
867
879
  content: z.union([
868
880
  z.object({
869
881
  type: z.literal('web_fetch_result'),
@@ -896,7 +908,7 @@ export const anthropicResponseSchema = lazySchema(() =>
896
908
  z.object({
897
909
  type: z.literal('web_search_tool_result'),
898
910
  tool_use_id: z.string(),
899
- caller: anthropicToolCallCallerSchema.optional(),
911
+ caller: anthropicToolCallCallerSchema().optional(),
900
912
  content: z.union([
901
913
  z.array(
902
914
  z.object({
@@ -1055,16 +1067,16 @@ export const anthropicResponseSchema = lazySchema(() =>
1055
1067
  }),
1056
1068
  ]),
1057
1069
  }),
1058
- anthropicFallbackContentSchema,
1070
+ anthropicFallbackContentSchema(),
1059
1071
  ]),
1060
1072
  ),
1061
1073
  stop_reason: z.string().nullish(),
1062
1074
  stop_sequence: z.string().nullish(),
1063
- stop_details: anthropicStopDetailsSchema.nullish(),
1075
+ stop_details: anthropicStopDetailsSchema().nullish(),
1064
1076
  input_transformations: z
1065
- .array(anthropicInputTransformationSchema)
1077
+ .array(anthropicInputTransformationSchema())
1066
1078
  .nullish(),
1067
- safeguard_results: z.array(anthropicSafeguardResultSchema).nullish(),
1079
+ safeguard_results: z.array(anthropicSafeguardResultSchema()).nullish(),
1068
1080
  usage: z.looseObject({
1069
1081
  input_tokens: z.number(),
1070
1082
  output_tokens: z.number(),
@@ -1158,7 +1170,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1158
1170
  id: z.string(),
1159
1171
  name: z.string(),
1160
1172
  input: z.unknown(),
1161
- caller: anthropicToolCallCallerSchema.optional(),
1173
+ caller: anthropicToolCallCallerSchema().optional(),
1162
1174
  toolset_name: z.string().nullish(),
1163
1175
  }),
1164
1176
  ]),
@@ -1166,7 +1178,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1166
1178
  .nullish(),
1167
1179
  stop_reason: z.string().nullish(),
1168
1180
  input_transformations: z
1169
- .array(anthropicInputTransformationSchema)
1181
+ .array(anthropicInputTransformationSchema())
1170
1182
  .nullish(),
1171
1183
  container: z
1172
1184
  .object({
@@ -1195,7 +1207,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1195
1207
  // Programmatic tool calling: input may be present directly for deferred tool calls
1196
1208
  input: z.record(z.string(), z.unknown()).optional(),
1197
1209
  // Programmatic tool calling: caller info when triggered from code execution
1198
- caller: anthropicToolCallCallerSchema.optional(),
1210
+ caller: anthropicToolCallCallerSchema().optional(),
1199
1211
  // Toolsets (e.g. computer toolset): name of the toolset this member call belongs to
1200
1212
  toolset_name: z.string().nullish(),
1201
1213
  }),
@@ -1213,7 +1225,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1213
1225
  id: z.string(),
1214
1226
  name: z.string(),
1215
1227
  input: z.record(z.string(), z.unknown()).nullish(),
1216
- caller: anthropicToolCallCallerSchema.optional(),
1228
+ caller: anthropicToolCallCallerSchema().optional(),
1217
1229
  }),
1218
1230
  z.object({
1219
1231
  type: z.literal('mcp_tool_use'),
@@ -1226,12 +1238,12 @@ export const anthropicChunkSchema = lazySchema(() =>
1226
1238
  type: z.literal('mcp_tool_result'),
1227
1239
  tool_use_id: z.string(),
1228
1240
  is_error: z.boolean(),
1229
- content: anthropicMcpToolResultContentSchema,
1241
+ content: anthropicMcpToolResultContentSchema(),
1230
1242
  }),
1231
1243
  z.object({
1232
1244
  type: z.literal('web_fetch_tool_result'),
1233
1245
  tool_use_id: z.string(),
1234
- caller: anthropicToolCallCallerSchema.optional(),
1246
+ caller: anthropicToolCallCallerSchema().optional(),
1235
1247
  content: z.union([
1236
1248
  z.object({
1237
1249
  type: z.literal('web_fetch_result'),
@@ -1264,7 +1276,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1264
1276
  z.object({
1265
1277
  type: z.literal('web_search_tool_result'),
1266
1278
  tool_use_id: z.string(),
1267
- caller: anthropicToolCallCallerSchema.optional(),
1279
+ caller: anthropicToolCallCallerSchema().optional(),
1268
1280
  content: z.union([
1269
1281
  z.array(
1270
1282
  z.object({
@@ -1419,7 +1431,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1419
1431
  }),
1420
1432
  ]),
1421
1433
  }),
1422
- anthropicFallbackContentSchema,
1434
+ anthropicFallbackContentSchema(),
1423
1435
  ]),
1424
1436
  }),
1425
1437
  z.object({
@@ -1448,7 +1460,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1448
1460
  }),
1449
1461
  z.object({
1450
1462
  type: z.literal('citations_delta'),
1451
- citation: anthropicCitationSchema,
1463
+ citation: anthropicCitationSchema(),
1452
1464
  }),
1453
1465
  ]),
1454
1466
  }),
@@ -1472,8 +1484,10 @@ export const anthropicChunkSchema = lazySchema(() =>
1472
1484
  delta: z.object({
1473
1485
  stop_reason: z.string().nullish(),
1474
1486
  stop_sequence: z.string().nullish(),
1475
- stop_details: anthropicStopDetailsSchema.nullish(),
1476
- safeguard_results: z.array(anthropicSafeguardResultSchema).nullish(),
1487
+ stop_details: anthropicStopDetailsSchema().nullish(),
1488
+ safeguard_results: z
1489
+ .array(anthropicSafeguardResultSchema())
1490
+ .nullish(),
1477
1491
  container: z
1478
1492
  .object({
1479
1493
  expires_at: z.string(),
@@ -1522,7 +1536,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1522
1536
  .nullish(),
1523
1537
  }),
1524
1538
  input_transformations: z
1525
- .array(anthropicInputTransformationSchema)
1539
+ .array(anthropicInputTransformationSchema())
1526
1540
  .nullish(),
1527
1541
  context_management: z
1528
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