@ai-sdk/anthropic 3.0.81 → 3.0.83

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.
@@ -2,7 +2,7 @@ import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateRes
2
2
  import * as _ai_sdk_provider_utils from '@ai-sdk/provider-utils';
3
3
  import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
4
4
 
5
- type AnthropicMessagesModelId = 'claude-3-haiku-20240307' | 'claude-haiku-4-5-20251001' | 'claude-haiku-4-5' | 'claude-opus-4-0' | 'claude-opus-4-20250514' | 'claude-opus-4-1-20250805' | 'claude-opus-4-1' | 'claude-opus-4-5' | 'claude-opus-4-5-20251101' | 'claude-sonnet-4-0' | 'claude-sonnet-4-20250514' | 'claude-sonnet-4-5-20250929' | 'claude-sonnet-4-5' | 'claude-sonnet-4-6' | 'claude-opus-4-6' | 'claude-opus-4-7' | 'claude-opus-4-8' | (string & {});
5
+ type AnthropicMessagesModelId = 'claude-3-haiku-20240307' | 'claude-haiku-4-5-20251001' | 'claude-haiku-4-5' | 'claude-opus-4-0' | 'claude-opus-4-20250514' | 'claude-opus-4-1-20250805' | 'claude-opus-4-1' | 'claude-opus-4-5' | 'claude-opus-4-5-20251101' | 'claude-sonnet-4-0' | 'claude-sonnet-4-20250514' | 'claude-sonnet-4-5-20250929' | 'claude-sonnet-4-5' | 'claude-sonnet-4-6' | 'claude-opus-4-6' | 'claude-opus-4-7' | 'claude-opus-4-8' | 'claude-fable-5' | (string & {});
6
6
 
7
7
  type AnthropicMessagesConfig = {
8
8
  provider: string;
@@ -2,7 +2,7 @@ import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateRes
2
2
  import * as _ai_sdk_provider_utils from '@ai-sdk/provider-utils';
3
3
  import { Resolvable, FetchFunction } from '@ai-sdk/provider-utils';
4
4
 
5
- type AnthropicMessagesModelId = 'claude-3-haiku-20240307' | 'claude-haiku-4-5-20251001' | 'claude-haiku-4-5' | 'claude-opus-4-0' | 'claude-opus-4-20250514' | 'claude-opus-4-1-20250805' | 'claude-opus-4-1' | 'claude-opus-4-5' | 'claude-opus-4-5-20251101' | 'claude-sonnet-4-0' | 'claude-sonnet-4-20250514' | 'claude-sonnet-4-5-20250929' | 'claude-sonnet-4-5' | 'claude-sonnet-4-6' | 'claude-opus-4-6' | 'claude-opus-4-7' | 'claude-opus-4-8' | (string & {});
5
+ type AnthropicMessagesModelId = 'claude-3-haiku-20240307' | 'claude-haiku-4-5-20251001' | 'claude-haiku-4-5' | 'claude-opus-4-0' | 'claude-opus-4-20250514' | 'claude-opus-4-1-20250805' | 'claude-opus-4-1' | 'claude-opus-4-5' | 'claude-opus-4-5-20251101' | 'claude-sonnet-4-0' | 'claude-sonnet-4-20250514' | 'claude-sonnet-4-5-20250929' | 'claude-sonnet-4-5' | 'claude-sonnet-4-6' | 'claude-opus-4-6' | 'claude-opus-4-7' | 'claude-opus-4-8' | 'claude-fable-5' | (string & {});
6
6
 
7
7
  type AnthropicMessagesConfig = {
8
8
  provider: string;
@@ -52,6 +52,12 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
52
52
  // src/anthropic-messages-api.ts
53
53
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
54
54
  var import_v42 = require("zod/v4");
55
+ var anthropicStopDetailsSchema = import_v42.z.object({
56
+ type: import_v42.z.string(),
57
+ category: import_v42.z.string().nullish(),
58
+ explanation: import_v42.z.string().nullish(),
59
+ recommended_model: import_v42.z.string().nullish()
60
+ });
55
61
  var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
56
62
  () => (0, import_provider_utils2.zodSchema)(
57
63
  import_v42.z.object({
@@ -337,34 +343,37 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
337
343
  error_code: import_v42.z.string()
338
344
  })
339
345
  ])
346
+ }),
347
+ // Server-side fallback marker. Parsed so the response validates, but
348
+ // dropped from the content output (the AI SDK has no model-hop
349
+ // primitive). The hop remains observable via usage.iterations.
350
+ import_v42.z.object({
351
+ type: import_v42.z.literal("fallback")
340
352
  })
341
353
  ])
342
354
  ),
343
355
  stop_reason: import_v42.z.string().nullish(),
344
356
  stop_sequence: import_v42.z.string().nullish(),
357
+ stop_details: anthropicStopDetailsSchema.nullish(),
345
358
  usage: import_v42.z.looseObject({
346
359
  input_tokens: import_v42.z.number(),
347
360
  output_tokens: import_v42.z.number(),
348
361
  cache_creation_input_tokens: import_v42.z.number().nullish(),
349
362
  cache_read_input_tokens: import_v42.z.number().nullish(),
350
363
  iterations: import_v42.z.array(
351
- import_v42.z.union([
352
- import_v42.z.object({
353
- type: import_v42.z.union([import_v42.z.literal("compaction"), import_v42.z.literal("message")]),
354
- input_tokens: import_v42.z.number(),
355
- output_tokens: import_v42.z.number(),
356
- cache_creation_input_tokens: import_v42.z.number().nullish(),
357
- cache_read_input_tokens: import_v42.z.number().nullish()
358
- }),
359
- import_v42.z.object({
360
- type: import_v42.z.literal("advisor_message"),
361
- model: import_v42.z.string(),
362
- input_tokens: import_v42.z.number(),
363
- output_tokens: import_v42.z.number(),
364
- cache_creation_input_tokens: import_v42.z.number().nullish(),
365
- cache_read_input_tokens: import_v42.z.number().nullish()
366
- })
367
- ])
364
+ import_v42.z.object({
365
+ type: import_v42.z.union([
366
+ import_v42.z.literal("compaction"),
367
+ import_v42.z.literal("message"),
368
+ import_v42.z.literal("advisor_message"),
369
+ import_v42.z.literal("fallback_message")
370
+ ]),
371
+ model: import_v42.z.string().nullish(),
372
+ input_tokens: import_v42.z.number(),
373
+ output_tokens: import_v42.z.number(),
374
+ cache_creation_input_tokens: import_v42.z.number().nullish(),
375
+ cache_read_input_tokens: import_v42.z.number().nullish()
376
+ })
368
377
  ).nullish()
369
378
  }),
370
379
  container: import_v42.z.object({
@@ -699,6 +708,11 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
699
708
  error_code: import_v42.z.string()
700
709
  })
701
710
  ])
711
+ }),
712
+ // Server-side fallback marker; dropped from content output (see the
713
+ // response schema). The hop remains observable via usage.iterations.
714
+ import_v42.z.object({
715
+ type: import_v42.z.literal("fallback")
702
716
  })
703
717
  ])
704
718
  }),
@@ -772,6 +786,7 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
772
786
  delta: import_v42.z.object({
773
787
  stop_reason: import_v42.z.string().nullish(),
774
788
  stop_sequence: import_v42.z.string().nullish(),
789
+ stop_details: anthropicStopDetailsSchema.nullish(),
775
790
  container: import_v42.z.object({
776
791
  expires_at: import_v42.z.string(),
777
792
  id: import_v42.z.string(),
@@ -793,26 +808,19 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
793
808
  cache_creation_input_tokens: import_v42.z.number().nullish(),
794
809
  cache_read_input_tokens: import_v42.z.number().nullish(),
795
810
  iterations: import_v42.z.array(
796
- import_v42.z.union([
797
- import_v42.z.object({
798
- type: import_v42.z.union([
799
- import_v42.z.literal("compaction"),
800
- import_v42.z.literal("message")
801
- ]),
802
- input_tokens: import_v42.z.number(),
803
- output_tokens: import_v42.z.number(),
804
- cache_creation_input_tokens: import_v42.z.number().nullish(),
805
- cache_read_input_tokens: import_v42.z.number().nullish()
806
- }),
807
- import_v42.z.object({
808
- type: import_v42.z.literal("advisor_message"),
809
- model: import_v42.z.string(),
810
- input_tokens: import_v42.z.number(),
811
- output_tokens: import_v42.z.number(),
812
- cache_creation_input_tokens: import_v42.z.number().nullish(),
813
- cache_read_input_tokens: import_v42.z.number().nullish()
814
- })
815
- ])
811
+ import_v42.z.object({
812
+ type: import_v42.z.union([
813
+ import_v42.z.literal("compaction"),
814
+ import_v42.z.literal("message"),
815
+ import_v42.z.literal("advisor_message"),
816
+ import_v42.z.literal("fallback_message")
817
+ ]),
818
+ model: import_v42.z.string().nullish(),
819
+ input_tokens: import_v42.z.number(),
820
+ output_tokens: import_v42.z.number(),
821
+ cache_creation_input_tokens: import_v42.z.number().nullish(),
822
+ cache_read_input_tokens: import_v42.z.number().nullish()
823
+ })
816
824
  ).nullish()
817
825
  }),
818
826
  context_management: import_v42.z.object({
@@ -1016,6 +1024,31 @@ var anthropicLanguageModelOptions = import_v43.z.object({
1016
1024
  * See https://platform.claude.com/docs/en/build-with-claude/data-residency
1017
1025
  */
1018
1026
  inferenceGeo: import_v43.z.enum(["us", "global"]).optional(),
1027
+ /**
1028
+ * Server-side fallback chain.
1029
+ *
1030
+ * When the primary model's safety classifiers block a turn, the API
1031
+ * automatically retries it on the next model in the chain, server-side. A
1032
+ * `content-filter` finish reason means the entire chain refused.
1033
+ *
1034
+ * Each entry is merged into the request as a direct request to that entry's
1035
+ * model, so it must be formatted accordingly: `model` is required, and an
1036
+ * entry may additionally override `max_tokens`, `thinking`, `output_config`,
1037
+ * and `speed` for that attempt only (`speed` additionally requires the speed
1038
+ * beta). The value is passed through to the API as-is.
1039
+ *
1040
+ * The required `server-side-fallback-2026-06-01` beta is added automatically
1041
+ * when this option is set.
1042
+ */
1043
+ fallbacks: import_v43.z.array(
1044
+ import_v43.z.object({
1045
+ model: import_v43.z.string(),
1046
+ max_tokens: import_v43.z.number().int().optional(),
1047
+ thinking: import_v43.z.record(import_v43.z.string(), import_v43.z.unknown()).optional(),
1048
+ output_config: import_v43.z.record(import_v43.z.string(), import_v43.z.unknown()).optional(),
1049
+ speed: import_v43.z.enum(["fast", "standard"]).optional()
1050
+ })
1051
+ ).optional(),
1019
1052
  /**
1020
1053
  * A set of beta features to enable.
1021
1054
  * Allow a provider to receive the full `betas` set if it needs it.
@@ -1770,12 +1803,15 @@ function convertAnthropicMessagesUsage({
1770
1803
  usage,
1771
1804
  rawUsage
1772
1805
  }) {
1773
- var _a, _b;
1806
+ var _a, _b, _c;
1774
1807
  const cacheCreationTokens = (_a = usage.cache_creation_input_tokens) != null ? _a : 0;
1775
1808
  const cacheReadTokens = (_b = usage.cache_read_input_tokens) != null ? _b : 0;
1776
1809
  let inputTokens;
1777
1810
  let outputTokens;
1778
- if (usage.iterations && usage.iterations.length > 0) {
1811
+ const servedByFallback = (_c = usage.iterations) == null ? void 0 : _c.some(
1812
+ (iter) => iter.type === "fallback_message"
1813
+ );
1814
+ if (usage.iterations && usage.iterations.length > 0 && !servedByFallback) {
1779
1815
  const executorIterations = usage.iterations.filter(
1780
1816
  (iter) => iter.type === "compaction" || iter.type === "message"
1781
1817
  );
@@ -3445,6 +3481,9 @@ var AnthropicMessagesLanguageModel = class {
3445
3481
  ...(anthropicOptions == null ? void 0 : anthropicOptions.inferenceGeo) && {
3446
3482
  inference_geo: anthropicOptions.inferenceGeo
3447
3483
  },
3484
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0 && {
3485
+ fallbacks: anthropicOptions.fallbacks
3486
+ },
3448
3487
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
3449
3488
  cache_control: anthropicOptions.cacheControl
3450
3489
  },
@@ -3621,6 +3660,9 @@ var AnthropicMessagesLanguageModel = class {
3621
3660
  if ((anthropicOptions == null ? void 0 : anthropicOptions.speed) === "fast") {
3622
3661
  betas.add("fast-mode-2026-02-01");
3623
3662
  }
3663
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0) {
3664
+ betas.add("server-side-fallback-2026-06-01");
3665
+ }
3624
3666
  const defaultEagerInputStreaming = stream && ((_i = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _i : true);
3625
3667
  const {
3626
3668
  tools: anthropicTools2,
@@ -4143,6 +4185,12 @@ var AnthropicMessagesLanguageModel = class {
4143
4185
  }
4144
4186
  break;
4145
4187
  }
4188
+ // Server-side fallback marker: the AI SDK has no content primitive for
4189
+ // a model hop, so drop it. The hop is still observable via
4190
+ // usage.iterations.
4191
+ case "fallback": {
4192
+ break;
4193
+ }
4146
4194
  }
4147
4195
  }
4148
4196
  return {
@@ -4165,14 +4213,16 @@ var AnthropicMessagesLanguageModel = class {
4165
4213
  warnings,
4166
4214
  providerMetadata: (() => {
4167
4215
  var _a2, _b2, _c2, _d2, _e2;
4216
+ const stopDetails = mapAnthropicStopDetails(response.stop_details);
4168
4217
  const anthropicMetadata = {
4169
4218
  usage: response.usage,
4170
4219
  cacheCreationInputTokens: (_a2 = response.usage.cache_creation_input_tokens) != null ? _a2 : null,
4171
4220
  stopSequence: (_b2 = response.stop_sequence) != null ? _b2 : null,
4221
+ ...stopDetails != null ? { stopDetails } : {},
4172
4222
  iterations: response.usage.iterations ? response.usage.iterations.map(
4173
- (iter) => iter.type === "advisor_message" ? {
4223
+ (iter) => ({
4174
4224
  type: iter.type,
4175
- model: iter.model,
4225
+ ...iter.model != null ? { model: iter.model } : {},
4176
4226
  inputTokens: iter.input_tokens,
4177
4227
  outputTokens: iter.output_tokens,
4178
4228
  ...iter.cache_creation_input_tokens ? {
@@ -4181,17 +4231,7 @@ var AnthropicMessagesLanguageModel = class {
4181
4231
  ...iter.cache_read_input_tokens ? {
4182
4232
  cacheReadInputTokens: iter.cache_read_input_tokens
4183
4233
  } : {}
4184
- } : {
4185
- type: iter.type,
4186
- inputTokens: iter.input_tokens,
4187
- outputTokens: iter.output_tokens,
4188
- ...iter.cache_creation_input_tokens ? {
4189
- cacheCreationInputTokens: iter.cache_creation_input_tokens
4190
- } : {},
4191
- ...iter.cache_read_input_tokens ? {
4192
- cacheReadInputTokens: iter.cache_read_input_tokens
4193
- } : {}
4194
- }
4234
+ })
4195
4235
  ) : null,
4196
4236
  container: response.container ? {
4197
4237
  expiresAt: response.container.expires_at,
@@ -4267,6 +4307,7 @@ var AnthropicMessagesLanguageModel = class {
4267
4307
  let rawUsage = void 0;
4268
4308
  let cacheCreationInputTokens = null;
4269
4309
  let stopSequence = null;
4310
+ let stopDetails = void 0;
4270
4311
  let container = null;
4271
4312
  let isJsonResponseFromTool = false;
4272
4313
  let blockType = void 0;
@@ -4293,6 +4334,9 @@ var AnthropicMessagesLanguageModel = class {
4293
4334
  case "content_block_start": {
4294
4335
  const part = value.content_block;
4295
4336
  const contentBlockType = part.type;
4337
+ if (contentBlockType === "fallback") {
4338
+ return;
4339
+ }
4296
4340
  blockType = contentBlockType;
4297
4341
  switch (contentBlockType) {
4298
4342
  case "text": {
@@ -4966,6 +5010,7 @@ var AnthropicMessagesLanguageModel = class {
4966
5010
  raw: (_k = value.delta.stop_reason) != null ? _k : void 0
4967
5011
  };
4968
5012
  stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
5013
+ stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
4969
5014
  container = value.delta.container != null ? {
4970
5015
  expiresAt: value.delta.container.expires_at,
4971
5016
  id: value.delta.container.id,
@@ -4991,10 +5036,11 @@ var AnthropicMessagesLanguageModel = class {
4991
5036
  usage: rawUsage != null ? rawUsage : null,
4992
5037
  cacheCreationInputTokens,
4993
5038
  stopSequence,
5039
+ ...stopDetails != null ? { stopDetails } : {},
4994
5040
  iterations: usage.iterations ? usage.iterations.map(
4995
- (iter) => iter.type === "advisor_message" ? {
5041
+ (iter) => ({
4996
5042
  type: iter.type,
4997
- model: iter.model,
5043
+ ...iter.model != null ? { model: iter.model } : {},
4998
5044
  inputTokens: iter.input_tokens,
4999
5045
  outputTokens: iter.output_tokens,
5000
5046
  ...iter.cache_creation_input_tokens ? {
@@ -5003,17 +5049,7 @@ var AnthropicMessagesLanguageModel = class {
5003
5049
  ...iter.cache_read_input_tokens ? {
5004
5050
  cacheReadInputTokens: iter.cache_read_input_tokens
5005
5051
  } : {}
5006
- } : {
5007
- type: iter.type,
5008
- inputTokens: iter.input_tokens,
5009
- outputTokens: iter.output_tokens,
5010
- ...iter.cache_creation_input_tokens ? {
5011
- cacheCreationInputTokens: iter.cache_creation_input_tokens
5012
- } : {},
5013
- ...iter.cache_read_input_tokens ? {
5014
- cacheReadInputTokens: iter.cache_read_input_tokens
5015
- } : {}
5016
- }
5052
+ })
5017
5053
  ) : null,
5018
5054
  container,
5019
5055
  contextManagement
@@ -5077,7 +5113,7 @@ var AnthropicMessagesLanguageModel = class {
5077
5113
  }
5078
5114
  };
5079
5115
  function getModelCapabilities(modelId) {
5080
- if (modelId.includes("claude-opus-4-8") || modelId.includes("claude-opus-4-7")) {
5116
+ if (modelId.includes("claude-opus-4-8") || modelId.includes("claude-opus-4-7") || modelId.includes("claude-fable-5")) {
5081
5117
  return {
5082
5118
  maxOutputTokens: 128e3,
5083
5119
  supportsStructuredOutput: true,
@@ -5178,6 +5214,17 @@ function mapAnthropicResponseContextManagement(contextManagement) {
5178
5214
  }).filter((edit) => edit !== void 0)
5179
5215
  } : null;
5180
5216
  }
5217
+ function mapAnthropicStopDetails(stopDetails) {
5218
+ if (stopDetails == null) {
5219
+ return void 0;
5220
+ }
5221
+ return {
5222
+ type: stopDetails.type,
5223
+ ...stopDetails.category != null ? { category: stopDetails.category } : {},
5224
+ ...stopDetails.explanation != null ? { explanation: stopDetails.explanation } : {},
5225
+ ...stopDetails.recommended_model != null ? { recommendedModel: stopDetails.recommended_model } : {}
5226
+ };
5227
+ }
5181
5228
 
5182
5229
  // src/tool/bash_20241022.ts
5183
5230
  var import_provider_utils16 = require("@ai-sdk/provider-utils");