@ai-sdk/anthropic 3.0.40 → 3.0.42

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.
@@ -100,6 +100,10 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
100
100
  type: import_v42.z.literal("redacted_thinking"),
101
101
  data: import_v42.z.string()
102
102
  }),
103
+ import_v42.z.object({
104
+ type: import_v42.z.literal("compaction"),
105
+ content: import_v42.z.string()
106
+ }),
103
107
  import_v42.z.object({
104
108
  type: import_v42.z.literal("tool_use"),
105
109
  id: import_v42.z.string(),
@@ -298,7 +302,14 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
298
302
  input_tokens: import_v42.z.number(),
299
303
  output_tokens: import_v42.z.number(),
300
304
  cache_creation_input_tokens: import_v42.z.number().nullish(),
301
- cache_read_input_tokens: import_v42.z.number().nullish()
305
+ cache_read_input_tokens: import_v42.z.number().nullish(),
306
+ iterations: import_v42.z.array(
307
+ import_v42.z.object({
308
+ type: import_v42.z.union([import_v42.z.literal("compaction"), import_v42.z.literal("message")]),
309
+ input_tokens: import_v42.z.number(),
310
+ output_tokens: import_v42.z.number()
311
+ })
312
+ ).nullish()
302
313
  }),
303
314
  container: import_v42.z.object({
304
315
  expires_at: import_v42.z.string(),
@@ -323,6 +334,9 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
323
334
  type: import_v42.z.literal("clear_thinking_20251015"),
324
335
  cleared_thinking_turns: import_v42.z.number(),
325
336
  cleared_input_tokens: import_v42.z.number()
337
+ }),
338
+ import_v42.z.object({
339
+ type: import_v42.z.literal("compact_20260112")
326
340
  })
327
341
  ])
328
342
  )
@@ -404,6 +418,10 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
404
418
  type: import_v42.z.literal("redacted_thinking"),
405
419
  data: import_v42.z.string()
406
420
  }),
421
+ import_v42.z.object({
422
+ type: import_v42.z.literal("compaction"),
423
+ content: import_v42.z.string().nullish()
424
+ }),
407
425
  import_v42.z.object({
408
426
  type: import_v42.z.literal("server_tool_use"),
409
427
  id: import_v42.z.string(),
@@ -600,6 +618,10 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
600
618
  type: import_v42.z.literal("signature_delta"),
601
619
  signature: import_v42.z.string()
602
620
  }),
621
+ import_v42.z.object({
622
+ type: import_v42.z.literal("compaction_delta"),
623
+ content: import_v42.z.string()
624
+ }),
603
625
  import_v42.z.object({
604
626
  type: import_v42.z.literal("citations_delta"),
605
627
  citation: import_v42.z.discriminatedUnion("type", [
@@ -665,7 +687,14 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
665
687
  input_tokens: import_v42.z.number().nullish(),
666
688
  output_tokens: import_v42.z.number(),
667
689
  cache_creation_input_tokens: import_v42.z.number().nullish(),
668
- cache_read_input_tokens: import_v42.z.number().nullish()
690
+ cache_read_input_tokens: import_v42.z.number().nullish(),
691
+ iterations: import_v42.z.array(
692
+ import_v42.z.object({
693
+ type: import_v42.z.union([import_v42.z.literal("compaction"), import_v42.z.literal("message")]),
694
+ input_tokens: import_v42.z.number(),
695
+ output_tokens: import_v42.z.number()
696
+ })
697
+ ).nullish()
669
698
  }),
670
699
  context_management: import_v42.z.object({
671
700
  applied_edits: import_v42.z.array(
@@ -679,6 +708,9 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
679
708
  type: import_v42.z.literal("clear_thinking_20251015"),
680
709
  cleared_thinking_turns: import_v42.z.number(),
681
710
  cleared_input_tokens: import_v42.z.number()
711
+ }),
712
+ import_v42.z.object({
713
+ type: import_v42.z.literal("compact_20260112")
682
714
  })
683
715
  ])
684
716
  )
@@ -727,7 +759,7 @@ var anthropicFilePartProviderOptions = import_v43.z.object({
727
759
  */
728
760
  context: import_v43.z.string().optional()
729
761
  });
730
- var anthropicProviderOptions = import_v43.z.object({
762
+ var anthropicLanguageModelOptions = import_v43.z.object({
731
763
  /**
732
764
  * Whether to send reasoning to the model.
733
765
  *
@@ -858,6 +890,15 @@ var anthropicProviderOptions = import_v43.z.object({
858
890
  value: import_v43.z.number()
859
891
  })
860
892
  ]).optional()
893
+ }),
894
+ import_v43.z.object({
895
+ type: import_v43.z.literal("compact_20260112"),
896
+ trigger: import_v43.z.object({
897
+ type: import_v43.z.literal("input_tokens"),
898
+ value: import_v43.z.number()
899
+ }).optional(),
900
+ pauseAfterCompaction: import_v43.z.boolean().optional(),
901
+ instructions: import_v43.z.string().optional()
861
902
  })
862
903
  ])
863
904
  )
@@ -1347,10 +1388,24 @@ function convertAnthropicMessagesUsage({
1347
1388
  rawUsage
1348
1389
  }) {
1349
1390
  var _a, _b;
1350
- const inputTokens = usage.input_tokens;
1351
- const outputTokens = usage.output_tokens;
1352
1391
  const cacheCreationTokens = (_a = usage.cache_creation_input_tokens) != null ? _a : 0;
1353
1392
  const cacheReadTokens = (_b = usage.cache_read_input_tokens) != null ? _b : 0;
1393
+ let inputTokens;
1394
+ let outputTokens;
1395
+ if (usage.iterations && usage.iterations.length > 0) {
1396
+ const totals = usage.iterations.reduce(
1397
+ (acc, iter) => ({
1398
+ input: acc.input + iter.input_tokens,
1399
+ output: acc.output + iter.output_tokens
1400
+ }),
1401
+ { input: 0, output: 0 }
1402
+ );
1403
+ inputTokens = totals.input;
1404
+ outputTokens = totals.output;
1405
+ } else {
1406
+ inputTokens = usage.input_tokens;
1407
+ outputTokens = usage.output_tokens;
1408
+ }
1354
1409
  return {
1355
1410
  inputTokens: {
1356
1411
  total: inputTokens + cacheCreationTokens + cacheReadTokens,
@@ -1592,7 +1647,7 @@ async function convertToAnthropicMessagesPrompt({
1592
1647
  cacheControlValidator,
1593
1648
  toolNameMapping
1594
1649
  }) {
1595
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1650
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
1596
1651
  const betas = /* @__PURE__ */ new Set();
1597
1652
  const blocks = groupIntoBlocks(prompt);
1598
1653
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -1885,16 +1940,25 @@ async function convertToAnthropicMessagesPrompt({
1885
1940
  }) : void 0;
1886
1941
  switch (part.type) {
1887
1942
  case "text": {
1888
- anthropicContent.push({
1889
- type: "text",
1890
- text: (
1891
- // trim the last text part if it's the last message in the block
1892
- // because Anthropic does not allow trailing whitespace
1893
- // in pre-filled assistant responses
1894
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1895
- ),
1896
- cache_control: cacheControl
1897
- });
1943
+ const textMetadata = (_g = part.providerOptions) == null ? void 0 : _g.anthropic;
1944
+ if ((textMetadata == null ? void 0 : textMetadata.type) === "compaction") {
1945
+ anthropicContent.push({
1946
+ type: "compaction",
1947
+ content: part.text,
1948
+ cache_control: cacheControl
1949
+ });
1950
+ } else {
1951
+ anthropicContent.push({
1952
+ type: "text",
1953
+ text: (
1954
+ // trim the last text part if it's the last message in the block
1955
+ // because Anthropic does not allow trailing whitespace
1956
+ // in pre-filled assistant responses
1957
+ isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1958
+ ),
1959
+ cache_control: cacheControl
1960
+ });
1961
+ }
1898
1962
  break;
1899
1963
  }
1900
1964
  case "reasoning": {
@@ -1949,10 +2013,10 @@ async function convertToAnthropicMessagesPrompt({
1949
2013
  const providerToolName = toolNameMapping.toProviderToolName(
1950
2014
  part.toolName
1951
2015
  );
1952
- const isMcpToolUse = ((_h = (_g = part.providerOptions) == null ? void 0 : _g.anthropic) == null ? void 0 : _h.type) === "mcp-tool-use";
2016
+ const isMcpToolUse = ((_i = (_h = part.providerOptions) == null ? void 0 : _h.anthropic) == null ? void 0 : _i.type) === "mcp-tool-use";
1953
2017
  if (isMcpToolUse) {
1954
2018
  mcpToolUseIds.add(part.toolCallId);
1955
- const serverName = (_j = (_i = part.providerOptions) == null ? void 0 : _i.anthropic) == null ? void 0 : _j.serverName;
2019
+ const serverName = (_k = (_j = part.providerOptions) == null ? void 0 : _j.anthropic) == null ? void 0 : _k.serverName;
1956
2020
  if (serverName == null || typeof serverName !== "string") {
1957
2021
  warnings.push({
1958
2022
  type: "other",
@@ -2020,7 +2084,7 @@ async function convertToAnthropicMessagesPrompt({
2020
2084
  }
2021
2085
  break;
2022
2086
  }
2023
- const callerOptions = (_k = part.providerOptions) == null ? void 0 : _k.anthropic;
2087
+ const callerOptions = (_l = part.providerOptions) == null ? void 0 : _l.anthropic;
2024
2088
  const caller = (callerOptions == null ? void 0 : callerOptions.caller) ? callerOptions.caller.type === "code_execution_20250825" && callerOptions.caller.toolId ? {
2025
2089
  type: "code_execution_20250825",
2026
2090
  tool_id: callerOptions.caller.toolId
@@ -2073,7 +2137,7 @@ async function convertToAnthropicMessagesPrompt({
2073
2137
  tool_use_id: part.toolCallId,
2074
2138
  content: {
2075
2139
  type: "code_execution_tool_result_error",
2076
- error_code: (_l = errorInfo.errorCode) != null ? _l : "unknown"
2140
+ error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2077
2141
  },
2078
2142
  cache_control: cacheControl
2079
2143
  });
@@ -2084,7 +2148,7 @@ async function convertToAnthropicMessagesPrompt({
2084
2148
  cache_control: cacheControl,
2085
2149
  content: {
2086
2150
  type: "bash_code_execution_tool_result_error",
2087
- error_code: (_m = errorInfo.errorCode) != null ? _m : "unknown"
2151
+ error_code: (_n = errorInfo.errorCode) != null ? _n : "unknown"
2088
2152
  }
2089
2153
  });
2090
2154
  }
@@ -2117,7 +2181,7 @@ async function convertToAnthropicMessagesPrompt({
2117
2181
  stdout: codeExecutionOutput.stdout,
2118
2182
  stderr: codeExecutionOutput.stderr,
2119
2183
  return_code: codeExecutionOutput.return_code,
2120
- content: (_n = codeExecutionOutput.content) != null ? _n : []
2184
+ content: (_o = codeExecutionOutput.content) != null ? _o : []
2121
2185
  },
2122
2186
  cache_control: cacheControl
2123
2187
  });
@@ -2135,7 +2199,7 @@ async function convertToAnthropicMessagesPrompt({
2135
2199
  stdout: codeExecutionOutput.stdout,
2136
2200
  stderr: codeExecutionOutput.stderr,
2137
2201
  return_code: codeExecutionOutput.return_code,
2138
- content: (_o = codeExecutionOutput.content) != null ? _o : []
2202
+ content: (_p = codeExecutionOutput.content) != null ? _p : []
2139
2203
  },
2140
2204
  cache_control: cacheControl
2141
2205
  });
@@ -2168,7 +2232,7 @@ async function convertToAnthropicMessagesPrompt({
2168
2232
  errorValue = output.value;
2169
2233
  }
2170
2234
  } catch (e) {
2171
- const extractedErrorCode = (_p = output.value) == null ? void 0 : _p.errorCode;
2235
+ const extractedErrorCode = (_q = output.value) == null ? void 0 : _q.errorCode;
2172
2236
  errorValue = {
2173
2237
  errorCode: typeof extractedErrorCode === "string" ? extractedErrorCode : "unknown"
2174
2238
  };
@@ -2178,7 +2242,7 @@ async function convertToAnthropicMessagesPrompt({
2178
2242
  tool_use_id: part.toolCallId,
2179
2243
  content: {
2180
2244
  type: "web_fetch_tool_result_error",
2181
- error_code: (_q = errorValue.errorCode) != null ? _q : "unknown"
2245
+ error_code: (_r = errorValue.errorCode) != null ? _r : "unknown"
2182
2246
  },
2183
2247
  cache_control: cacheControl
2184
2248
  });
@@ -2359,6 +2423,8 @@ function mapAnthropicStopReason({
2359
2423
  case "max_tokens":
2360
2424
  case "model_context_window_exceeded":
2361
2425
  return "length";
2426
+ case "compaction":
2427
+ return "other";
2362
2428
  default:
2363
2429
  return "other";
2364
2430
  }
@@ -2492,12 +2558,12 @@ var AnthropicMessagesLanguageModel = class {
2492
2558
  const canonicalOptions = await (0, import_provider_utils12.parseProviderOptions)({
2493
2559
  provider: "anthropic",
2494
2560
  providerOptions,
2495
- schema: anthropicProviderOptions
2561
+ schema: anthropicLanguageModelOptions
2496
2562
  });
2497
2563
  const customProviderOptions = providerOptionsName !== "anthropic" ? await (0, import_provider_utils12.parseProviderOptions)({
2498
2564
  provider: providerOptionsName,
2499
2565
  providerOptions,
2500
- schema: anthropicProviderOptions
2566
+ schema: anthropicLanguageModelOptions
2501
2567
  }) : null;
2502
2568
  const usedCustomProviderKey = customProviderOptions != null;
2503
2569
  const anthropicOptions = Object.assign(
@@ -2641,6 +2707,19 @@ var AnthropicMessagesLanguageModel = class {
2641
2707
  type: edit.type,
2642
2708
  ...edit.keep !== void 0 && { keep: edit.keep }
2643
2709
  };
2710
+ case "compact_20260112":
2711
+ return {
2712
+ type: edit.type,
2713
+ ...edit.trigger !== void 0 && {
2714
+ trigger: edit.trigger
2715
+ },
2716
+ ...edit.pauseAfterCompaction !== void 0 && {
2717
+ pause_after_compaction: edit.pauseAfterCompaction
2718
+ },
2719
+ ...edit.instructions !== void 0 && {
2720
+ instructions: edit.instructions
2721
+ }
2722
+ };
2644
2723
  default:
2645
2724
  warnings.push({
2646
2725
  type: "other",
@@ -2715,6 +2794,9 @@ var AnthropicMessagesLanguageModel = class {
2715
2794
  }
2716
2795
  if (contextManagement) {
2717
2796
  betas.add("context-management-2025-06-27");
2797
+ if (contextManagement.edits.some((e) => e.type === "compact_20260112")) {
2798
+ betas.add("compact-2026-01-12");
2799
+ }
2718
2800
  }
2719
2801
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2720
2802
  betas.add("code-execution-2025-08-25");
@@ -2913,6 +2995,18 @@ var AnthropicMessagesLanguageModel = class {
2913
2995
  });
2914
2996
  break;
2915
2997
  }
2998
+ case "compaction": {
2999
+ content.push({
3000
+ type: "text",
3001
+ text: part.content,
3002
+ providerMetadata: {
3003
+ anthropic: {
3004
+ type: "compaction"
3005
+ }
3006
+ }
3007
+ });
3008
+ break;
3009
+ }
2916
3010
  case "tool_use": {
2917
3011
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
2918
3012
  if (isJsonResponseTool) {
@@ -3196,6 +3290,11 @@ var AnthropicMessagesLanguageModel = class {
3196
3290
  usage: response.usage,
3197
3291
  cacheCreationInputTokens: (_a2 = response.usage.cache_creation_input_tokens) != null ? _a2 : null,
3198
3292
  stopSequence: (_b2 = response.stop_sequence) != null ? _b2 : null,
3293
+ iterations: response.usage.iterations ? response.usage.iterations.map((iter) => ({
3294
+ type: iter.type,
3295
+ inputTokens: iter.input_tokens,
3296
+ outputTokens: iter.output_tokens
3297
+ })) : null,
3199
3298
  container: response.container ? {
3200
3299
  expiresAt: response.container.expires_at,
3201
3300
  id: response.container.id,
@@ -3257,7 +3356,8 @@ var AnthropicMessagesLanguageModel = class {
3257
3356
  input_tokens: 0,
3258
3357
  output_tokens: 0,
3259
3358
  cache_creation_input_tokens: 0,
3260
- cache_read_input_tokens: 0
3359
+ cache_read_input_tokens: 0,
3360
+ iterations: null
3261
3361
  };
3262
3362
  const contentBlocks = {};
3263
3363
  const mcpToolCalls = {};
@@ -3326,6 +3426,19 @@ var AnthropicMessagesLanguageModel = class {
3326
3426
  });
3327
3427
  return;
3328
3428
  }
3429
+ case "compaction": {
3430
+ contentBlocks[value.index] = { type: "text" };
3431
+ controller.enqueue({
3432
+ type: "text-start",
3433
+ id: String(value.index),
3434
+ providerMetadata: {
3435
+ anthropic: {
3436
+ type: "compaction"
3437
+ }
3438
+ }
3439
+ });
3440
+ return;
3441
+ }
3329
3442
  case "tool_use": {
3330
3443
  const isJsonResponseTool = usesJsonResponseTool && part.name === "json";
3331
3444
  if (isJsonResponseTool) {
@@ -3712,6 +3825,14 @@ var AnthropicMessagesLanguageModel = class {
3712
3825
  }
3713
3826
  return;
3714
3827
  }
3828
+ case "compaction_delta": {
3829
+ controller.enqueue({
3830
+ type: "text-delta",
3831
+ id: String(value.index),
3832
+ delta: value.delta.content
3833
+ });
3834
+ return;
3835
+ }
3715
3836
  case "input_json_delta": {
3716
3837
  const contentBlock = contentBlocks[value.index];
3717
3838
  let delta = value.delta.partial_json;
@@ -3847,6 +3968,9 @@ var AnthropicMessagesLanguageModel = class {
3847
3968
  usage.cache_creation_input_tokens = value.usage.cache_creation_input_tokens;
3848
3969
  cacheCreationInputTokens = value.usage.cache_creation_input_tokens;
3849
3970
  }
3971
+ if (value.usage.iterations != null) {
3972
+ usage.iterations = value.usage.iterations;
3973
+ }
3850
3974
  finishReason = {
3851
3975
  unified: mapAnthropicStopReason({
3852
3976
  finishReason: value.delta.stop_reason,
@@ -3880,6 +4004,11 @@ var AnthropicMessagesLanguageModel = class {
3880
4004
  usage: rawUsage != null ? rawUsage : null,
3881
4005
  cacheCreationInputTokens,
3882
4006
  stopSequence,
4007
+ iterations: usage.iterations ? usage.iterations.map((iter) => ({
4008
+ type: iter.type,
4009
+ inputTokens: iter.input_tokens,
4010
+ outputTokens: iter.output_tokens
4011
+ })) : null,
3883
4012
  container,
3884
4013
  contextManagement
3885
4014
  };
@@ -4009,6 +4138,10 @@ function mapAnthropicResponseContextManagement(contextManagement) {
4009
4138
  clearedThinkingTurns: edit.cleared_thinking_turns,
4010
4139
  clearedInputTokens: edit.cleared_input_tokens
4011
4140
  };
4141
+ case "compact_20260112":
4142
+ return {
4143
+ type: edit.type
4144
+ };
4012
4145
  }
4013
4146
  }).filter((edit) => edit !== void 0)
4014
4147
  } : null;