@ai-sdk/anthropic 3.0.80 → 3.0.82

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' | (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;
@@ -70,7 +70,7 @@ declare const anthropicTools: {
70
70
  * Supported executor models: Claude Haiku 4.5, Sonnet 4.6, Opus 4.6,
71
71
  * Opus 4.7. The advisor must be at least as capable as the executor.
72
72
  *
73
- * @param model - The advisor model ID (required), e.g. `"claude-opus-4-7"`.
73
+ * @param model - The advisor model ID (required), e.g. `"claude-opus-4-8"`.
74
74
  * @param maxUses - Maximum advisor calls per request (per-request cap).
75
75
  * @param caching - Enables prompt caching for the advisor's transcript
76
76
  * across calls within a conversation. Worthwhile from ~3 advisor calls
@@ -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' | (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;
@@ -70,7 +70,7 @@ declare const anthropicTools: {
70
70
  * Supported executor models: Claude Haiku 4.5, Sonnet 4.6, Opus 4.6,
71
71
  * Opus 4.7. The advisor must be at least as capable as the executor.
72
72
  *
73
- * @param model - The advisor model ID (required), e.g. `"claude-opus-4-7"`.
73
+ * @param model - The advisor model ID (required), e.g. `"claude-opus-4-8"`.
74
74
  * @param maxUses - Maximum advisor calls per request (per-request cap).
75
75
  * @param caching - Enables prompt caching for the advisor's transcript
76
76
  * across calls within a conversation. Worthwhile from ~3 advisor calls
@@ -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
  );
@@ -2202,19 +2238,20 @@ async function convertToAnthropicMessagesPrompt({
2202
2238
  const type = block.type;
2203
2239
  switch (type) {
2204
2240
  case "system": {
2205
- if (system != null) {
2206
- throw new import_provider2.UnsupportedFunctionalityError({
2207
- functionality: "Multiple system messages that are separated by user/assistant messages"
2208
- });
2209
- }
2210
- system = block.messages.map(({ content, providerOptions }) => ({
2241
+ const content = block.messages.map(({ content: content2, providerOptions }) => ({
2211
2242
  type: "text",
2212
- text: content,
2243
+ text: content2,
2213
2244
  cache_control: validator.getCacheControl(providerOptions, {
2214
2245
  type: "system message",
2215
2246
  canCache: true
2216
2247
  })
2217
2248
  }));
2249
+ if (system == null) {
2250
+ system = content;
2251
+ } else {
2252
+ messages.push({ role: "system", content });
2253
+ betas.add("mid-conversation-system-2026-04-07");
2254
+ }
2218
2255
  break;
2219
2256
  }
2220
2257
  case "user": {
@@ -3444,6 +3481,9 @@ var AnthropicMessagesLanguageModel = class {
3444
3481
  ...(anthropicOptions == null ? void 0 : anthropicOptions.inferenceGeo) && {
3445
3482
  inference_geo: anthropicOptions.inferenceGeo
3446
3483
  },
3484
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0 && {
3485
+ fallbacks: anthropicOptions.fallbacks
3486
+ },
3447
3487
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
3448
3488
  cache_control: anthropicOptions.cacheControl
3449
3489
  },
@@ -3620,6 +3660,9 @@ var AnthropicMessagesLanguageModel = class {
3620
3660
  if ((anthropicOptions == null ? void 0 : anthropicOptions.speed) === "fast") {
3621
3661
  betas.add("fast-mode-2026-02-01");
3622
3662
  }
3663
+ if ((anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0) {
3664
+ betas.add("server-side-fallback-2026-06-01");
3665
+ }
3623
3666
  const defaultEagerInputStreaming = stream && ((_i = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _i : true);
3624
3667
  const {
3625
3668
  tools: anthropicTools2,
@@ -4142,6 +4185,12 @@ var AnthropicMessagesLanguageModel = class {
4142
4185
  }
4143
4186
  break;
4144
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
+ }
4145
4194
  }
4146
4195
  }
4147
4196
  return {
@@ -4164,24 +4213,16 @@ var AnthropicMessagesLanguageModel = class {
4164
4213
  warnings,
4165
4214
  providerMetadata: (() => {
4166
4215
  var _a2, _b2, _c2, _d2, _e2;
4216
+ const stopDetails = mapAnthropicStopDetails(response.stop_details);
4167
4217
  const anthropicMetadata = {
4168
4218
  usage: response.usage,
4169
4219
  cacheCreationInputTokens: (_a2 = response.usage.cache_creation_input_tokens) != null ? _a2 : null,
4170
4220
  stopSequence: (_b2 = response.stop_sequence) != null ? _b2 : null,
4221
+ ...stopDetails != null ? { stopDetails } : {},
4171
4222
  iterations: response.usage.iterations ? response.usage.iterations.map(
4172
- (iter) => iter.type === "advisor_message" ? {
4173
- type: iter.type,
4174
- model: iter.model,
4175
- inputTokens: iter.input_tokens,
4176
- outputTokens: iter.output_tokens,
4177
- ...iter.cache_creation_input_tokens ? {
4178
- cacheCreationInputTokens: iter.cache_creation_input_tokens
4179
- } : {},
4180
- ...iter.cache_read_input_tokens ? {
4181
- cacheReadInputTokens: iter.cache_read_input_tokens
4182
- } : {}
4183
- } : {
4223
+ (iter) => ({
4184
4224
  type: iter.type,
4225
+ ...iter.model != null ? { model: iter.model } : {},
4185
4226
  inputTokens: iter.input_tokens,
4186
4227
  outputTokens: iter.output_tokens,
4187
4228
  ...iter.cache_creation_input_tokens ? {
@@ -4190,7 +4231,7 @@ var AnthropicMessagesLanguageModel = class {
4190
4231
  ...iter.cache_read_input_tokens ? {
4191
4232
  cacheReadInputTokens: iter.cache_read_input_tokens
4192
4233
  } : {}
4193
- }
4234
+ })
4194
4235
  ) : null,
4195
4236
  container: response.container ? {
4196
4237
  expiresAt: response.container.expires_at,
@@ -4266,6 +4307,7 @@ var AnthropicMessagesLanguageModel = class {
4266
4307
  let rawUsage = void 0;
4267
4308
  let cacheCreationInputTokens = null;
4268
4309
  let stopSequence = null;
4310
+ let stopDetails = void 0;
4269
4311
  let container = null;
4270
4312
  let isJsonResponseFromTool = false;
4271
4313
  let blockType = void 0;
@@ -4292,6 +4334,9 @@ var AnthropicMessagesLanguageModel = class {
4292
4334
  case "content_block_start": {
4293
4335
  const part = value.content_block;
4294
4336
  const contentBlockType = part.type;
4337
+ if (contentBlockType === "fallback") {
4338
+ return;
4339
+ }
4295
4340
  blockType = contentBlockType;
4296
4341
  switch (contentBlockType) {
4297
4342
  case "text": {
@@ -4965,6 +5010,7 @@ var AnthropicMessagesLanguageModel = class {
4965
5010
  raw: (_k = value.delta.stop_reason) != null ? _k : void 0
4966
5011
  };
4967
5012
  stopSequence = (_l = value.delta.stop_sequence) != null ? _l : null;
5013
+ stopDetails = mapAnthropicStopDetails(value.delta.stop_details);
4968
5014
  container = value.delta.container != null ? {
4969
5015
  expiresAt: value.delta.container.expires_at,
4970
5016
  id: value.delta.container.id,
@@ -4990,20 +5036,11 @@ var AnthropicMessagesLanguageModel = class {
4990
5036
  usage: rawUsage != null ? rawUsage : null,
4991
5037
  cacheCreationInputTokens,
4992
5038
  stopSequence,
5039
+ ...stopDetails != null ? { stopDetails } : {},
4993
5040
  iterations: usage.iterations ? usage.iterations.map(
4994
- (iter) => iter.type === "advisor_message" ? {
4995
- type: iter.type,
4996
- model: iter.model,
4997
- inputTokens: iter.input_tokens,
4998
- outputTokens: iter.output_tokens,
4999
- ...iter.cache_creation_input_tokens ? {
5000
- cacheCreationInputTokens: iter.cache_creation_input_tokens
5001
- } : {},
5002
- ...iter.cache_read_input_tokens ? {
5003
- cacheReadInputTokens: iter.cache_read_input_tokens
5004
- } : {}
5005
- } : {
5041
+ (iter) => ({
5006
5042
  type: iter.type,
5043
+ ...iter.model != null ? { model: iter.model } : {},
5007
5044
  inputTokens: iter.input_tokens,
5008
5045
  outputTokens: iter.output_tokens,
5009
5046
  ...iter.cache_creation_input_tokens ? {
@@ -5012,7 +5049,7 @@ var AnthropicMessagesLanguageModel = class {
5012
5049
  ...iter.cache_read_input_tokens ? {
5013
5050
  cacheReadInputTokens: iter.cache_read_input_tokens
5014
5051
  } : {}
5015
- }
5052
+ })
5016
5053
  ) : null,
5017
5054
  container,
5018
5055
  contextManagement
@@ -5076,7 +5113,7 @@ var AnthropicMessagesLanguageModel = class {
5076
5113
  }
5077
5114
  };
5078
5115
  function getModelCapabilities(modelId) {
5079
- if (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")) {
5080
5117
  return {
5081
5118
  maxOutputTokens: 128e3,
5082
5119
  supportsStructuredOutput: true,
@@ -5177,6 +5214,17 @@ function mapAnthropicResponseContextManagement(contextManagement) {
5177
5214
  }).filter((edit) => edit !== void 0)
5178
5215
  } : null;
5179
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
+ }
5180
5228
 
5181
5229
  // src/tool/bash_20241022.ts
5182
5230
  var import_provider_utils16 = require("@ai-sdk/provider-utils");
@@ -5494,7 +5542,7 @@ var anthropicTools = {
5494
5542
  * Supported executor models: Claude Haiku 4.5, Sonnet 4.6, Opus 4.6,
5495
5543
  * Opus 4.7. The advisor must be at least as capable as the executor.
5496
5544
  *
5497
- * @param model - The advisor model ID (required), e.g. `"claude-opus-4-7"`.
5545
+ * @param model - The advisor model ID (required), e.g. `"claude-opus-4-8"`.
5498
5546
  * @param maxUses - Maximum advisor calls per request (per-request cap).
5499
5547
  * @param caching - Enables prompt caching for the advisor's transcript
5500
5548
  * across calls within a conversation. Worthwhile from ~3 advisor calls