@ai-sdk/anthropic 2.0.59 → 2.0.61

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.
@@ -87,6 +87,10 @@ var anthropicMessagesResponseSchema = lazySchema2(
87
87
  type: z2.literal("redacted_thinking"),
88
88
  data: z2.string()
89
89
  }),
90
+ z2.object({
91
+ type: z2.literal("compaction"),
92
+ content: z2.string()
93
+ }),
90
94
  z2.object({
91
95
  type: z2.literal("tool_use"),
92
96
  id: z2.string(),
@@ -231,8 +235,28 @@ var anthropicMessagesResponseSchema = lazySchema2(
231
235
  input_tokens: z2.number(),
232
236
  output_tokens: z2.number(),
233
237
  cache_creation_input_tokens: z2.number().nullish(),
234
- cache_read_input_tokens: z2.number().nullish()
238
+ cache_read_input_tokens: z2.number().nullish(),
239
+ iterations: z2.array(
240
+ z2.object({
241
+ type: z2.union([z2.literal("compaction"), z2.literal("message")]),
242
+ input_tokens: z2.number(),
243
+ output_tokens: z2.number()
244
+ })
245
+ ).nullish()
235
246
  }),
247
+ context_management: z2.object({
248
+ applied_edits: z2.array(
249
+ z2.union([
250
+ z2.object({
251
+ type: z2.literal("clear_01"),
252
+ cleared_input_tokens: z2.number()
253
+ }),
254
+ z2.object({
255
+ type: z2.literal("compact_20260112")
256
+ })
257
+ ])
258
+ )
259
+ }).nullish(),
236
260
  container: z2.object({
237
261
  expires_at: z2.string(),
238
262
  id: z2.string(),
@@ -283,6 +307,10 @@ var anthropicMessagesChunkSchema = lazySchema2(
283
307
  type: z2.literal("redacted_thinking"),
284
308
  data: z2.string()
285
309
  }),
310
+ z2.object({
311
+ type: z2.literal("compaction"),
312
+ content: z2.string().nullish()
313
+ }),
286
314
  z2.object({
287
315
  type: z2.literal("server_tool_use"),
288
316
  id: z2.string(),
@@ -435,6 +463,10 @@ var anthropicMessagesChunkSchema = lazySchema2(
435
463
  type: z2.literal("signature_delta"),
436
464
  signature: z2.string()
437
465
  }),
466
+ z2.object({
467
+ type: z2.literal("compaction_delta"),
468
+ content: z2.string()
469
+ }),
438
470
  z2.object({
439
471
  type: z2.literal("citations_delta"),
440
472
  citation: z2.discriminatedUnion("type", [
@@ -500,8 +532,28 @@ var anthropicMessagesChunkSchema = lazySchema2(
500
532
  input_tokens: z2.number().nullish(),
501
533
  output_tokens: z2.number(),
502
534
  cache_creation_input_tokens: z2.number().nullish(),
503
- cache_read_input_tokens: z2.number().nullish()
504
- })
535
+ cache_read_input_tokens: z2.number().nullish(),
536
+ iterations: z2.array(
537
+ z2.object({
538
+ type: z2.union([z2.literal("compaction"), z2.literal("message")]),
539
+ input_tokens: z2.number(),
540
+ output_tokens: z2.number()
541
+ })
542
+ ).nullish()
543
+ }),
544
+ context_management: z2.object({
545
+ applied_edits: z2.array(
546
+ z2.union([
547
+ z2.object({
548
+ type: z2.literal("clear_01"),
549
+ cleared_input_tokens: z2.number()
550
+ }),
551
+ z2.object({
552
+ type: z2.literal("compact_20260112")
553
+ })
554
+ ])
555
+ )
556
+ }).nullish()
505
557
  }),
506
558
  z2.object({
507
559
  type: z2.literal("message_stop")
@@ -607,7 +659,45 @@ var anthropicProviderOptions = z3.object({
607
659
  /**
608
660
  * @default 'high'
609
661
  */
610
- effort: z3.enum(["low", "medium", "high", "max"]).optional()
662
+ effort: z3.enum(["low", "medium", "high", "max"]).optional(),
663
+ /**
664
+ * Enable fast mode for faster inference (2.5x faster output token speeds).
665
+ * Only supported with claude-opus-4-6.
666
+ */
667
+ speed: z3.literal("fast").optional(),
668
+ /**
669
+ * Context management configuration for automatic context window management.
670
+ * Enables features like automatic compaction and clearing of tool uses/thinking blocks.
671
+ */
672
+ contextManagement: z3.object({
673
+ edits: z3.array(
674
+ z3.discriminatedUnion("type", [
675
+ z3.object({
676
+ type: z3.literal("clear_01"),
677
+ trigger: z3.object({
678
+ type: z3.literal("input_tokens"),
679
+ value: z3.number()
680
+ }).optional(),
681
+ keep: z3.union([
682
+ z3.literal("all"),
683
+ z3.object({
684
+ type: z3.literal("thinking_turns"),
685
+ value: z3.number()
686
+ })
687
+ ]).optional()
688
+ }),
689
+ z3.object({
690
+ type: z3.literal("compact_20260112"),
691
+ trigger: z3.object({
692
+ type: z3.literal("input_tokens"),
693
+ value: z3.number()
694
+ }).optional(),
695
+ pauseAfterCompaction: z3.boolean().optional(),
696
+ instructions: z3.string().optional()
697
+ })
698
+ ])
699
+ )
700
+ }).optional()
611
701
  });
612
702
 
613
703
  // src/anthropic-prepare-tools.ts
@@ -1203,7 +1293,7 @@ async function convertToAnthropicMessagesPrompt({
1203
1293
  warnings,
1204
1294
  cacheControlValidator
1205
1295
  }) {
1206
- var _a, _b, _c, _d, _e;
1296
+ var _a, _b, _c, _d, _e, _f;
1207
1297
  const betas = /* @__PURE__ */ new Set();
1208
1298
  const blocks = groupIntoBlocks(prompt);
1209
1299
  const validator = cacheControlValidator || new CacheControlValidator();
@@ -1447,16 +1537,25 @@ async function convertToAnthropicMessagesPrompt({
1447
1537
  }) : void 0;
1448
1538
  switch (part.type) {
1449
1539
  case "text": {
1450
- anthropicContent.push({
1451
- type: "text",
1452
- text: (
1453
- // trim the last text part if it's the last message in the block
1454
- // because Anthropic does not allow trailing whitespace
1455
- // in pre-filled assistant responses
1456
- isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1457
- ),
1458
- cache_control: cacheControl
1459
- });
1540
+ const textMetadata = (_f = part.providerOptions) == null ? void 0 : _f.anthropic;
1541
+ if ((textMetadata == null ? void 0 : textMetadata.type) === "compaction") {
1542
+ anthropicContent.push({
1543
+ type: "compaction",
1544
+ content: part.text,
1545
+ cache_control: cacheControl
1546
+ });
1547
+ } else {
1548
+ anthropicContent.push({
1549
+ type: "text",
1550
+ text: (
1551
+ // trim the last text part if it's the last message in the block
1552
+ // because Anthropic does not allow trailing whitespace
1553
+ // in pre-filled assistant responses
1554
+ isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
1555
+ ),
1556
+ cache_control: cacheControl
1557
+ });
1558
+ }
1460
1559
  break;
1461
1560
  }
1462
1561
  case "reasoning": {
@@ -1745,6 +1844,8 @@ function mapAnthropicStopReason({
1745
1844
  return isJsonResponseFromTool ? "stop" : "tool-calls";
1746
1845
  case "max_tokens":
1747
1846
  return "length";
1847
+ case "compaction":
1848
+ return "other";
1748
1849
  default:
1749
1850
  return "unknown";
1750
1851
  }
@@ -1912,6 +2013,9 @@ var AnthropicMessagesLanguageModel = class {
1912
2013
  ...(anthropicOptions == null ? void 0 : anthropicOptions.effort) && {
1913
2014
  output_config: { effort: anthropicOptions.effort }
1914
2015
  },
2016
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.speed) && {
2017
+ speed: anthropicOptions.speed
2018
+ },
1915
2019
  // structured output:
1916
2020
  ...useStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && {
1917
2021
  output_format: {
@@ -1930,6 +2034,48 @@ var AnthropicMessagesLanguageModel = class {
1930
2034
  }))
1931
2035
  }
1932
2036
  },
2037
+ // context management:
2038
+ ...(anthropicOptions == null ? void 0 : anthropicOptions.contextManagement) && {
2039
+ context_management: {
2040
+ edits: anthropicOptions.contextManagement.edits.map((edit) => {
2041
+ const convertTrigger = (trigger) => trigger ? {
2042
+ type: trigger.type,
2043
+ value: trigger.value
2044
+ } : void 0;
2045
+ switch (edit.type) {
2046
+ case "clear_01":
2047
+ return {
2048
+ type: edit.type,
2049
+ ...edit.trigger !== void 0 && {
2050
+ trigger: convertTrigger(edit.trigger)
2051
+ },
2052
+ ...edit.keep !== void 0 && {
2053
+ keep: edit.keep
2054
+ }
2055
+ };
2056
+ case "compact_20260112":
2057
+ return {
2058
+ type: edit.type,
2059
+ ...edit.trigger !== void 0 && {
2060
+ trigger: convertTrigger(edit.trigger)
2061
+ },
2062
+ ...edit.pauseAfterCompaction !== void 0 && {
2063
+ pause_after_compaction: edit.pauseAfterCompaction
2064
+ },
2065
+ ...edit.instructions !== void 0 && {
2066
+ instructions: edit.instructions
2067
+ }
2068
+ };
2069
+ default:
2070
+ warnings.push({
2071
+ type: "other",
2072
+ message: `Unknown context management edit type: ${edit.type}`
2073
+ });
2074
+ return edit;
2075
+ }
2076
+ })
2077
+ }
2078
+ },
1933
2079
  // prompt:
1934
2080
  system: messagesPrompt.system,
1935
2081
  messages: messagesPrompt.messages
@@ -1992,9 +2138,19 @@ var AnthropicMessagesLanguageModel = class {
1992
2138
  if (anthropicOptions == null ? void 0 : anthropicOptions.effort) {
1993
2139
  betas.add("effort-2025-11-24");
1994
2140
  }
2141
+ if (anthropicOptions == null ? void 0 : anthropicOptions.speed) {
2142
+ betas.add("fast-mode-2026-02-01");
2143
+ }
1995
2144
  if (useStructuredOutput) {
1996
2145
  betas.add("structured-outputs-2025-11-13");
1997
2146
  }
2147
+ const contextManagement = anthropicOptions == null ? void 0 : anthropicOptions.contextManagement;
2148
+ if (contextManagement) {
2149
+ betas.add("context-management-2025-06-27");
2150
+ if (contextManagement.edits.some((e) => e.type === "compact_20260112")) {
2151
+ betas.add("compact-2026-01-12");
2152
+ }
2153
+ }
1998
2154
  const {
1999
2155
  tools: anthropicTools2,
2000
2156
  toolChoice: anthropicToolChoice,
@@ -2145,6 +2301,18 @@ var AnthropicMessagesLanguageModel = class {
2145
2301
  });
2146
2302
  break;
2147
2303
  }
2304
+ case "compaction": {
2305
+ content.push({
2306
+ type: "text",
2307
+ text: part.content,
2308
+ providerMetadata: {
2309
+ anthropic: {
2310
+ type: "compaction"
2311
+ }
2312
+ }
2313
+ });
2314
+ break;
2315
+ }
2148
2316
  case "tool_use": {
2149
2317
  content.push(
2150
2318
  // when a json response tool is used, the tool call becomes the text:
@@ -2309,6 +2477,22 @@ var AnthropicMessagesLanguageModel = class {
2309
2477
  }
2310
2478
  }
2311
2479
  }
2480
+ let inputTokens;
2481
+ let outputTokens;
2482
+ if (response.usage.iterations && response.usage.iterations.length > 0) {
2483
+ const totals = response.usage.iterations.reduce(
2484
+ (acc, iter) => ({
2485
+ input: acc.input + iter.input_tokens,
2486
+ output: acc.output + iter.output_tokens
2487
+ }),
2488
+ { input: 0, output: 0 }
2489
+ );
2490
+ inputTokens = totals.input;
2491
+ outputTokens = totals.output;
2492
+ } else {
2493
+ inputTokens = response.usage.input_tokens;
2494
+ outputTokens = response.usage.output_tokens;
2495
+ }
2312
2496
  return {
2313
2497
  content,
2314
2498
  finishReason: mapAnthropicStopReason({
@@ -2316,9 +2500,9 @@ var AnthropicMessagesLanguageModel = class {
2316
2500
  isJsonResponseFromTool: usesJsonResponseTool
2317
2501
  }),
2318
2502
  usage: {
2319
- inputTokens: response.usage.input_tokens,
2320
- outputTokens: response.usage.output_tokens,
2321
- totalTokens: response.usage.input_tokens + response.usage.output_tokens,
2503
+ inputTokens,
2504
+ outputTokens,
2505
+ totalTokens: inputTokens + outputTokens,
2322
2506
  cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
2323
2507
  },
2324
2508
  request: { body: args },
@@ -2334,6 +2518,11 @@ var AnthropicMessagesLanguageModel = class {
2334
2518
  usage: response.usage,
2335
2519
  cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null,
2336
2520
  stopSequence: (_f = response.stop_sequence) != null ? _f : null,
2521
+ iterations: response.usage.iterations ? response.usage.iterations.map((iter) => ({
2522
+ type: iter.type,
2523
+ inputTokens: iter.input_tokens,
2524
+ outputTokens: iter.output_tokens
2525
+ })) : null,
2337
2526
  container: response.container ? {
2338
2527
  expiresAt: response.container.expires_at,
2339
2528
  id: response.container.id,
@@ -2342,6 +2531,23 @@ var AnthropicMessagesLanguageModel = class {
2342
2531
  skillId: skill.skill_id,
2343
2532
  version: skill.version
2344
2533
  }))) != null ? _h : null
2534
+ } : null,
2535
+ contextManagement: response.context_management ? {
2536
+ appliedEdits: response.context_management.applied_edits.map(
2537
+ (edit) => {
2538
+ switch (edit.type) {
2539
+ case "clear_01":
2540
+ return {
2541
+ type: edit.type,
2542
+ clearedInputTokens: edit.cleared_input_tokens
2543
+ };
2544
+ case "compact_20260112":
2545
+ return {
2546
+ type: edit.type
2547
+ };
2548
+ }
2549
+ }
2550
+ )
2345
2551
  } : null
2346
2552
  }
2347
2553
  }
@@ -2378,6 +2584,8 @@ var AnthropicMessagesLanguageModel = class {
2378
2584
  let cacheCreationInputTokens = null;
2379
2585
  let stopSequence = null;
2380
2586
  let container = null;
2587
+ let iterations = null;
2588
+ let contextManagement = null;
2381
2589
  let blockType = void 0;
2382
2590
  const generateId2 = this.generateId;
2383
2591
  const transformedStream = response.pipeThrough(
@@ -2432,6 +2640,19 @@ var AnthropicMessagesLanguageModel = class {
2432
2640
  });
2433
2641
  return;
2434
2642
  }
2643
+ case "compaction": {
2644
+ contentBlocks[value.index] = { type: "text" };
2645
+ controller.enqueue({
2646
+ type: "text-start",
2647
+ id: String(value.index),
2648
+ providerMetadata: {
2649
+ anthropic: {
2650
+ type: "compaction"
2651
+ }
2652
+ }
2653
+ });
2654
+ return;
2655
+ }
2435
2656
  case "tool_use": {
2436
2657
  contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
2437
2658
  type: "tool-call",
@@ -2694,6 +2915,14 @@ var AnthropicMessagesLanguageModel = class {
2694
2915
  }
2695
2916
  return;
2696
2917
  }
2918
+ case "compaction_delta": {
2919
+ controller.enqueue({
2920
+ type: "text-delta",
2921
+ id: String(value.index),
2922
+ delta: value.delta.content
2923
+ });
2924
+ return;
2925
+ }
2697
2926
  case "input_json_delta": {
2698
2927
  const contentBlock = contentBlocks[value.index];
2699
2928
  let delta = value.delta.partial_json;
@@ -2761,16 +2990,31 @@ var AnthropicMessagesLanguageModel = class {
2761
2990
  return;
2762
2991
  }
2763
2992
  case "message_delta": {
2764
- if (value.usage.input_tokens != null && usage.inputTokens !== value.usage.input_tokens) {
2765
- usage.inputTokens = value.usage.input_tokens;
2766
- }
2767
- usage.outputTokens = value.usage.output_tokens;
2768
2993
  if (value.usage.cache_read_input_tokens != null) {
2769
2994
  usage.cachedInputTokens = value.usage.cache_read_input_tokens;
2770
2995
  }
2771
2996
  if (value.usage.cache_creation_input_tokens != null) {
2772
2997
  cacheCreationInputTokens = value.usage.cache_creation_input_tokens;
2773
2998
  }
2999
+ if (value.usage.iterations != null) {
3000
+ iterations = value.usage.iterations;
3001
+ }
3002
+ if (value.usage.iterations && value.usage.iterations.length > 0) {
3003
+ const totals = value.usage.iterations.reduce(
3004
+ (acc, iter) => ({
3005
+ input: acc.input + iter.input_tokens,
3006
+ output: acc.output + iter.output_tokens
3007
+ }),
3008
+ { input: 0, output: 0 }
3009
+ );
3010
+ usage.inputTokens = totals.input;
3011
+ usage.outputTokens = totals.output;
3012
+ } else {
3013
+ if (value.usage.input_tokens != null && usage.inputTokens !== value.usage.input_tokens) {
3014
+ usage.inputTokens = value.usage.input_tokens;
3015
+ }
3016
+ usage.outputTokens = value.usage.output_tokens;
3017
+ }
2774
3018
  usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = usage.outputTokens) != null ? _g : 0);
2775
3019
  finishReason = mapAnthropicStopReason({
2776
3020
  finishReason: value.delta.stop_reason,
@@ -2786,6 +3030,25 @@ var AnthropicMessagesLanguageModel = class {
2786
3030
  version: skill.version
2787
3031
  }))) != null ? _j : null
2788
3032
  } : null;
3033
+ if (value.context_management != null) {
3034
+ contextManagement = {
3035
+ appliedEdits: value.context_management.applied_edits.map(
3036
+ (edit) => {
3037
+ switch (edit.type) {
3038
+ case "clear_01":
3039
+ return {
3040
+ type: edit.type,
3041
+ clearedInputTokens: edit.cleared_input_tokens
3042
+ };
3043
+ case "compact_20260112":
3044
+ return {
3045
+ type: edit.type
3046
+ };
3047
+ }
3048
+ }
3049
+ )
3050
+ };
3051
+ }
2789
3052
  rawUsage = {
2790
3053
  ...rawUsage,
2791
3054
  ...value.usage
@@ -2802,7 +3065,13 @@ var AnthropicMessagesLanguageModel = class {
2802
3065
  usage: rawUsage != null ? rawUsage : null,
2803
3066
  cacheCreationInputTokens,
2804
3067
  stopSequence,
2805
- container
3068
+ iterations: iterations ? iterations.map((iter) => ({
3069
+ type: iter.type,
3070
+ inputTokens: iter.input_tokens,
3071
+ outputTokens: iter.output_tokens
3072
+ })) : null,
3073
+ container,
3074
+ contextManagement
2806
3075
  }
2807
3076
  }
2808
3077
  });