@ai-sdk/anthropic 2.0.32 → 2.0.33

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,4 +1,4 @@
1
- import { LanguageModelV2, JSONSchema7, LanguageModelV2CallOptions, LanguageModelV2CallWarning } from '@ai-sdk/provider';
1
+ import { LanguageModelV2, JSONSchema7, SharedV2ProviderMetadata, LanguageModelV2CallWarning, LanguageModelV2CallOptions } from '@ai-sdk/provider';
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
 
@@ -463,6 +463,7 @@ declare const anthropicTools: {
463
463
 
464
464
  type AnthropicCacheControl = {
465
465
  type: 'ephemeral';
466
+ ttl?: '5m' | '1h';
466
467
  };
467
468
  type AnthropicTool = {
468
469
  name: string;
@@ -472,6 +473,7 @@ type AnthropicTool = {
472
473
  } | {
473
474
  type: 'code_execution_20250522';
474
475
  name: string;
476
+ cache_control: AnthropicCacheControl | undefined;
475
477
  } | {
476
478
  type: 'code_execution_20250825';
477
479
  name: string;
@@ -481,16 +483,20 @@ type AnthropicTool = {
481
483
  display_width_px: number;
482
484
  display_height_px: number;
483
485
  display_number: number;
486
+ cache_control: AnthropicCacheControl | undefined;
484
487
  } | {
485
488
  name: string;
486
489
  type: 'text_editor_20250124' | 'text_editor_20241022' | 'text_editor_20250429';
490
+ cache_control: AnthropicCacheControl | undefined;
487
491
  } | {
488
492
  name: string;
489
493
  type: 'text_editor_20250728';
490
494
  max_characters?: number;
495
+ cache_control: AnthropicCacheControl | undefined;
491
496
  } | {
492
497
  name: string;
493
498
  type: 'bash_20250124' | 'bash_20241022';
499
+ cache_control: AnthropicCacheControl | undefined;
494
500
  } | {
495
501
  name: string;
496
502
  type: 'memory_20250818';
@@ -504,6 +510,7 @@ type AnthropicTool = {
504
510
  enabled: boolean;
505
511
  };
506
512
  max_content_tokens?: number;
513
+ cache_control: AnthropicCacheControl | undefined;
507
514
  } | {
508
515
  type: 'web_search_20250305';
509
516
  name: string;
@@ -517,6 +524,7 @@ type AnthropicTool = {
517
524
  country?: string;
518
525
  timezone?: string;
519
526
  };
527
+ cache_control: AnthropicCacheControl | undefined;
520
528
  };
521
529
  type AnthropicToolChoice = {
522
530
  type: 'auto' | 'any';
@@ -527,10 +535,21 @@ type AnthropicToolChoice = {
527
535
  disable_parallel_tool_use?: boolean;
528
536
  };
529
537
 
530
- declare function prepareTools({ tools, toolChoice, disableParallelToolUse, }: {
538
+ declare class CacheControlValidator {
539
+ private breakpointCount;
540
+ private warnings;
541
+ getCacheControl(providerMetadata: SharedV2ProviderMetadata | undefined, context: {
542
+ type: string;
543
+ canCache: boolean;
544
+ }): AnthropicCacheControl | undefined;
545
+ getWarnings(): LanguageModelV2CallWarning[];
546
+ }
547
+
548
+ declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cacheControlValidator, }: {
531
549
  tools: LanguageModelV2CallOptions['tools'];
532
550
  toolChoice?: LanguageModelV2CallOptions['toolChoice'];
533
551
  disableParallelToolUse?: boolean;
552
+ cacheControlValidator?: CacheControlValidator;
534
553
  }): Promise<{
535
554
  tools: Array<AnthropicTool> | undefined;
536
555
  toolChoice: AnthropicToolChoice | undefined;
@@ -1,4 +1,4 @@
1
- import { LanguageModelV2, JSONSchema7, LanguageModelV2CallOptions, LanguageModelV2CallWarning } from '@ai-sdk/provider';
1
+ import { LanguageModelV2, JSONSchema7, SharedV2ProviderMetadata, LanguageModelV2CallWarning, LanguageModelV2CallOptions } from '@ai-sdk/provider';
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
 
@@ -463,6 +463,7 @@ declare const anthropicTools: {
463
463
 
464
464
  type AnthropicCacheControl = {
465
465
  type: 'ephemeral';
466
+ ttl?: '5m' | '1h';
466
467
  };
467
468
  type AnthropicTool = {
468
469
  name: string;
@@ -472,6 +473,7 @@ type AnthropicTool = {
472
473
  } | {
473
474
  type: 'code_execution_20250522';
474
475
  name: string;
476
+ cache_control: AnthropicCacheControl | undefined;
475
477
  } | {
476
478
  type: 'code_execution_20250825';
477
479
  name: string;
@@ -481,16 +483,20 @@ type AnthropicTool = {
481
483
  display_width_px: number;
482
484
  display_height_px: number;
483
485
  display_number: number;
486
+ cache_control: AnthropicCacheControl | undefined;
484
487
  } | {
485
488
  name: string;
486
489
  type: 'text_editor_20250124' | 'text_editor_20241022' | 'text_editor_20250429';
490
+ cache_control: AnthropicCacheControl | undefined;
487
491
  } | {
488
492
  name: string;
489
493
  type: 'text_editor_20250728';
490
494
  max_characters?: number;
495
+ cache_control: AnthropicCacheControl | undefined;
491
496
  } | {
492
497
  name: string;
493
498
  type: 'bash_20250124' | 'bash_20241022';
499
+ cache_control: AnthropicCacheControl | undefined;
494
500
  } | {
495
501
  name: string;
496
502
  type: 'memory_20250818';
@@ -504,6 +510,7 @@ type AnthropicTool = {
504
510
  enabled: boolean;
505
511
  };
506
512
  max_content_tokens?: number;
513
+ cache_control: AnthropicCacheControl | undefined;
507
514
  } | {
508
515
  type: 'web_search_20250305';
509
516
  name: string;
@@ -517,6 +524,7 @@ type AnthropicTool = {
517
524
  country?: string;
518
525
  timezone?: string;
519
526
  };
527
+ cache_control: AnthropicCacheControl | undefined;
520
528
  };
521
529
  type AnthropicToolChoice = {
522
530
  type: 'auto' | 'any';
@@ -527,10 +535,21 @@ type AnthropicToolChoice = {
527
535
  disable_parallel_tool_use?: boolean;
528
536
  };
529
537
 
530
- declare function prepareTools({ tools, toolChoice, disableParallelToolUse, }: {
538
+ declare class CacheControlValidator {
539
+ private breakpointCount;
540
+ private warnings;
541
+ getCacheControl(providerMetadata: SharedV2ProviderMetadata | undefined, context: {
542
+ type: string;
543
+ canCache: boolean;
544
+ }): AnthropicCacheControl | undefined;
545
+ getWarnings(): LanguageModelV2CallWarning[];
546
+ }
547
+
548
+ declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cacheControlValidator, }: {
531
549
  tools: LanguageModelV2CallOptions['tools'];
532
550
  toolChoice?: LanguageModelV2CallOptions['toolChoice'];
533
551
  disableParallelToolUse?: boolean;
552
+ cacheControlValidator?: CacheControlValidator;
534
553
  }): Promise<{
535
554
  tools: Array<AnthropicTool> | undefined;
536
555
  toolChoice: AnthropicToolChoice | undefined;
@@ -558,12 +558,46 @@ var anthropicProviderOptions = import_v43.z.object({
558
558
  var import_provider = require("@ai-sdk/provider");
559
559
 
560
560
  // src/get-cache-control.ts
561
+ var MAX_CACHE_BREAKPOINTS = 4;
561
562
  function getCacheControl(providerMetadata) {
562
563
  var _a;
563
564
  const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
564
565
  const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.cache_control;
565
566
  return cacheControlValue;
566
567
  }
568
+ var CacheControlValidator = class {
569
+ constructor() {
570
+ this.breakpointCount = 0;
571
+ this.warnings = [];
572
+ }
573
+ getCacheControl(providerMetadata, context) {
574
+ const cacheControlValue = getCacheControl(providerMetadata);
575
+ if (!cacheControlValue) {
576
+ return void 0;
577
+ }
578
+ if (!context.canCache) {
579
+ this.warnings.push({
580
+ type: "unsupported-setting",
581
+ setting: "cacheControl",
582
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
583
+ });
584
+ return void 0;
585
+ }
586
+ this.breakpointCount++;
587
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
588
+ this.warnings.push({
589
+ type: "unsupported-setting",
590
+ setting: "cacheControl",
591
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
592
+ });
593
+ return void 0;
594
+ }
595
+ return cacheControlValue;
596
+ }
597
+ getWarnings() {
598
+ return this.warnings;
599
+ }
600
+ };
567
601
 
568
602
  // src/tool/text-editor_20250728.ts
569
603
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
@@ -709,11 +743,13 @@ var import_provider_utils7 = require("@ai-sdk/provider-utils");
709
743
  async function prepareTools({
710
744
  tools,
711
745
  toolChoice,
712
- disableParallelToolUse
746
+ disableParallelToolUse,
747
+ cacheControlValidator
713
748
  }) {
714
749
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
715
750
  const toolWarnings = [];
716
751
  const betas = /* @__PURE__ */ new Set();
752
+ const validator = cacheControlValidator || new CacheControlValidator();
717
753
  if (tools == null) {
718
754
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
719
755
  }
@@ -721,7 +757,10 @@ async function prepareTools({
721
757
  for (const tool of tools) {
722
758
  switch (tool.type) {
723
759
  case "function": {
724
- const cacheControl = getCacheControl(tool.providerOptions);
760
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
761
+ type: "tool definition",
762
+ canCache: true
763
+ });
725
764
  anthropicTools2.push({
726
765
  name: tool.name,
727
766
  description: tool.description,
@@ -736,7 +775,8 @@ async function prepareTools({
736
775
  betas.add("code-execution-2025-05-22");
737
776
  anthropicTools2.push({
738
777
  type: "code_execution_20250522",
739
- name: "code_execution"
778
+ name: "code_execution",
779
+ cache_control: void 0
740
780
  });
741
781
  break;
742
782
  }
@@ -755,7 +795,8 @@ async function prepareTools({
755
795
  type: "computer_20250124",
756
796
  display_width_px: tool.args.displayWidthPx,
757
797
  display_height_px: tool.args.displayHeightPx,
758
- display_number: tool.args.displayNumber
798
+ display_number: tool.args.displayNumber,
799
+ cache_control: void 0
759
800
  });
760
801
  break;
761
802
  }
@@ -766,7 +807,8 @@ async function prepareTools({
766
807
  type: "computer_20241022",
767
808
  display_width_px: tool.args.displayWidthPx,
768
809
  display_height_px: tool.args.displayHeightPx,
769
- display_number: tool.args.displayNumber
810
+ display_number: tool.args.displayNumber,
811
+ cache_control: void 0
770
812
  });
771
813
  break;
772
814
  }
@@ -774,7 +816,8 @@ async function prepareTools({
774
816
  betas.add("computer-use-2025-01-24");
775
817
  anthropicTools2.push({
776
818
  name: "str_replace_editor",
777
- type: "text_editor_20250124"
819
+ type: "text_editor_20250124",
820
+ cache_control: void 0
778
821
  });
779
822
  break;
780
823
  }
@@ -782,7 +825,8 @@ async function prepareTools({
782
825
  betas.add("computer-use-2024-10-22");
783
826
  anthropicTools2.push({
784
827
  name: "str_replace_editor",
785
- type: "text_editor_20241022"
828
+ type: "text_editor_20241022",
829
+ cache_control: void 0
786
830
  });
787
831
  break;
788
832
  }
@@ -790,7 +834,8 @@ async function prepareTools({
790
834
  betas.add("computer-use-2025-01-24");
791
835
  anthropicTools2.push({
792
836
  name: "str_replace_based_edit_tool",
793
- type: "text_editor_20250429"
837
+ type: "text_editor_20250429",
838
+ cache_control: void 0
794
839
  });
795
840
  break;
796
841
  }
@@ -802,7 +847,8 @@ async function prepareTools({
802
847
  anthropicTools2.push({
803
848
  name: "str_replace_based_edit_tool",
804
849
  type: "text_editor_20250728",
805
- max_characters: args.maxCharacters
850
+ max_characters: args.maxCharacters,
851
+ cache_control: void 0
806
852
  });
807
853
  break;
808
854
  }
@@ -810,7 +856,8 @@ async function prepareTools({
810
856
  betas.add("computer-use-2025-01-24");
811
857
  anthropicTools2.push({
812
858
  name: "bash",
813
- type: "bash_20250124"
859
+ type: "bash_20250124",
860
+ cache_control: void 0
814
861
  });
815
862
  break;
816
863
  }
@@ -818,7 +865,8 @@ async function prepareTools({
818
865
  betas.add("computer-use-2024-10-22");
819
866
  anthropicTools2.push({
820
867
  name: "bash",
821
- type: "bash_20241022"
868
+ type: "bash_20241022",
869
+ cache_control: void 0
822
870
  });
823
871
  break;
824
872
  }
@@ -843,7 +891,8 @@ async function prepareTools({
843
891
  allowed_domains: args.allowedDomains,
844
892
  blocked_domains: args.blockedDomains,
845
893
  citations: args.citations,
846
- max_content_tokens: args.maxContentTokens
894
+ max_content_tokens: args.maxContentTokens,
895
+ cache_control: void 0
847
896
  });
848
897
  break;
849
898
  }
@@ -858,7 +907,8 @@ async function prepareTools({
858
907
  max_uses: args.maxUses,
859
908
  allowed_domains: args.allowedDomains,
860
909
  blocked_domains: args.blockedDomains,
861
- user_location: args.userLocation
910
+ user_location: args.userLocation,
911
+ cache_control: void 0
862
912
  });
863
913
  break;
864
914
  }
@@ -1070,11 +1120,13 @@ function convertToString(data) {
1070
1120
  async function convertToAnthropicMessagesPrompt({
1071
1121
  prompt,
1072
1122
  sendReasoning,
1073
- warnings
1123
+ warnings,
1124
+ cacheControlValidator
1074
1125
  }) {
1075
1126
  var _a, _b, _c, _d, _e;
1076
1127
  const betas = /* @__PURE__ */ new Set();
1077
1128
  const blocks = groupIntoBlocks(prompt);
1129
+ const validator = cacheControlValidator || new CacheControlValidator();
1078
1130
  let system = void 0;
1079
1131
  const messages = [];
1080
1132
  async function shouldEnableCitations(providerMetadata) {
@@ -1111,7 +1163,10 @@ async function convertToAnthropicMessagesPrompt({
1111
1163
  system = block.messages.map(({ content, providerOptions }) => ({
1112
1164
  type: "text",
1113
1165
  text: content,
1114
- cache_control: getCacheControl(providerOptions)
1166
+ cache_control: validator.getCacheControl(providerOptions, {
1167
+ type: "system message",
1168
+ canCache: true
1169
+ })
1115
1170
  }));
1116
1171
  break;
1117
1172
  }
@@ -1124,7 +1179,13 @@ async function convertToAnthropicMessagesPrompt({
1124
1179
  for (let j = 0; j < content.length; j++) {
1125
1180
  const part = content[j];
1126
1181
  const isLastPart = j === content.length - 1;
1127
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1182
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1183
+ type: "user message part",
1184
+ canCache: true
1185
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1186
+ type: "user message",
1187
+ canCache: true
1188
+ }) : void 0;
1128
1189
  switch (part.type) {
1129
1190
  case "text": {
1130
1191
  anthropicContent.push({
@@ -1212,7 +1273,13 @@ async function convertToAnthropicMessagesPrompt({
1212
1273
  for (let i2 = 0; i2 < content.length; i2++) {
1213
1274
  const part = content[i2];
1214
1275
  const isLastPart = i2 === content.length - 1;
1215
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1276
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1277
+ type: "tool result part",
1278
+ canCache: true
1279
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1280
+ type: "tool result message",
1281
+ canCache: true
1282
+ }) : void 0;
1216
1283
  const output = part.output;
1217
1284
  let contentValue;
1218
1285
  switch (output.type) {
@@ -1222,8 +1289,7 @@ async function convertToAnthropicMessagesPrompt({
1222
1289
  case "text":
1223
1290
  return {
1224
1291
  type: "text",
1225
- text: contentPart.text,
1226
- cache_control: void 0
1292
+ text: contentPart.text
1227
1293
  };
1228
1294
  case "media": {
1229
1295
  if (contentPart.mediaType.startsWith("image/")) {
@@ -1233,8 +1299,7 @@ async function convertToAnthropicMessagesPrompt({
1233
1299
  type: "base64",
1234
1300
  media_type: contentPart.mediaType,
1235
1301
  data: contentPart.data
1236
- },
1237
- cache_control: void 0
1302
+ }
1238
1303
  };
1239
1304
  }
1240
1305
  if (contentPart.mediaType === "application/pdf") {
@@ -1245,8 +1310,7 @@ async function convertToAnthropicMessagesPrompt({
1245
1310
  type: "base64",
1246
1311
  media_type: contentPart.mediaType,
1247
1312
  data: contentPart.data
1248
- },
1249
- cache_control: void 0
1313
+ }
1250
1314
  };
1251
1315
  }
1252
1316
  throw new import_provider2.UnsupportedFunctionalityError({
@@ -1294,7 +1358,13 @@ async function convertToAnthropicMessagesPrompt({
1294
1358
  for (let k = 0; k < content.length; k++) {
1295
1359
  const part = content[k];
1296
1360
  const isLastContentPart = k === content.length - 1;
1297
- const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1361
+ const cacheControl = (_e = validator.getCacheControl(part.providerOptions, {
1362
+ type: "assistant message part",
1363
+ canCache: true
1364
+ })) != null ? _e : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1365
+ type: "assistant message",
1366
+ canCache: true
1367
+ }) : void 0;
1298
1368
  switch (part.type) {
1299
1369
  case "text": {
1300
1370
  anthropicContent.push({
@@ -1318,17 +1388,23 @@ async function convertToAnthropicMessagesPrompt({
1318
1388
  });
1319
1389
  if (reasoningMetadata != null) {
1320
1390
  if (reasoningMetadata.signature != null) {
1391
+ validator.getCacheControl(part.providerOptions, {
1392
+ type: "thinking block",
1393
+ canCache: false
1394
+ });
1321
1395
  anthropicContent.push({
1322
1396
  type: "thinking",
1323
1397
  thinking: part.text,
1324
- signature: reasoningMetadata.signature,
1325
- cache_control: cacheControl
1398
+ signature: reasoningMetadata.signature
1326
1399
  });
1327
1400
  } else if (reasoningMetadata.redactedData != null) {
1401
+ validator.getCacheControl(part.providerOptions, {
1402
+ type: "redacted thinking block",
1403
+ canCache: false
1404
+ });
1328
1405
  anthropicContent.push({
1329
1406
  type: "redacted_thinking",
1330
- data: reasoningMetadata.redactedData,
1331
- cache_control: cacheControl
1407
+ data: reasoningMetadata.redactedData
1332
1408
  });
1333
1409
  } else {
1334
1410
  warnings.push({
@@ -1703,10 +1779,12 @@ var AnthropicMessagesLanguageModel = class {
1703
1779
  providerOptions,
1704
1780
  schema: anthropicProviderOptions
1705
1781
  });
1782
+ const cacheControlValidator = new CacheControlValidator();
1706
1783
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1707
1784
  prompt,
1708
1785
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1709
- warnings
1786
+ warnings,
1787
+ cacheControlValidator
1710
1788
  });
1711
1789
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1712
1790
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1804,20 +1882,23 @@ var AnthropicMessagesLanguageModel = class {
1804
1882
  jsonResponseTool != null ? {
1805
1883
  tools: [jsonResponseTool],
1806
1884
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1807
- disableParallelToolUse: true
1885
+ disableParallelToolUse: true,
1886
+ cacheControlValidator
1808
1887
  } : {
1809
1888
  tools: tools != null ? tools : [],
1810
1889
  toolChoice,
1811
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
1890
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
1891
+ cacheControlValidator
1812
1892
  }
1813
1893
  );
1894
+ const cacheWarnings = cacheControlValidator.getWarnings();
1814
1895
  return {
1815
1896
  args: {
1816
1897
  ...baseArgs,
1817
1898
  tools: anthropicTools2,
1818
1899
  tool_choice: anthropicToolChoice
1819
1900
  },
1820
- warnings: [...warnings, ...toolWarnings],
1901
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1821
1902
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1822
1903
  usesJsonResponseTool: jsonResponseTool != null
1823
1904
  };