@ai-sdk/anthropic 3.0.0-beta.30 → 3.0.0-beta.31

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 { LanguageModelV3, JSONSchema7, LanguageModelV3CallOptions, LanguageModelV3CallWarning } from '@ai-sdk/provider';
1
+ import { LanguageModelV3, JSONSchema7, SharedV3ProviderMetadata, LanguageModelV3CallWarning, LanguageModelV3CallOptions } 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: SharedV3ProviderMetadata | undefined, context: {
542
+ type: string;
543
+ canCache: boolean;
544
+ }): AnthropicCacheControl | undefined;
545
+ getWarnings(): LanguageModelV3CallWarning[];
546
+ }
547
+
548
+ declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cacheControlValidator, }: {
531
549
  tools: LanguageModelV3CallOptions['tools'];
532
550
  toolChoice?: LanguageModelV3CallOptions['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 { LanguageModelV3, JSONSchema7, LanguageModelV3CallOptions, LanguageModelV3CallWarning } from '@ai-sdk/provider';
1
+ import { LanguageModelV3, JSONSchema7, SharedV3ProviderMetadata, LanguageModelV3CallWarning, LanguageModelV3CallOptions } 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: SharedV3ProviderMetadata | undefined, context: {
542
+ type: string;
543
+ canCache: boolean;
544
+ }): AnthropicCacheControl | undefined;
545
+ getWarnings(): LanguageModelV3CallWarning[];
546
+ }
547
+
548
+ declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cacheControlValidator, }: {
531
549
  tools: LanguageModelV3CallOptions['tools'];
532
550
  toolChoice?: LanguageModelV3CallOptions['toolChoice'];
533
551
  disableParallelToolUse?: boolean;
552
+ cacheControlValidator?: CacheControlValidator;
534
553
  }): Promise<{
535
554
  tools: Array<AnthropicTool> | undefined;
536
555
  toolChoice: AnthropicToolChoice | undefined;
@@ -606,12 +606,46 @@ var anthropicProviderOptions = import_v43.z.object({
606
606
  var import_provider = require("@ai-sdk/provider");
607
607
 
608
608
  // src/get-cache-control.ts
609
+ var MAX_CACHE_BREAKPOINTS = 4;
609
610
  function getCacheControl(providerMetadata) {
610
611
  var _a;
611
612
  const anthropic = providerMetadata == null ? void 0 : providerMetadata.anthropic;
612
613
  const cacheControlValue = (_a = anthropic == null ? void 0 : anthropic.cacheControl) != null ? _a : anthropic == null ? void 0 : anthropic.cache_control;
613
614
  return cacheControlValue;
614
615
  }
616
+ var CacheControlValidator = class {
617
+ constructor() {
618
+ this.breakpointCount = 0;
619
+ this.warnings = [];
620
+ }
621
+ getCacheControl(providerMetadata, context) {
622
+ const cacheControlValue = getCacheControl(providerMetadata);
623
+ if (!cacheControlValue) {
624
+ return void 0;
625
+ }
626
+ if (!context.canCache) {
627
+ this.warnings.push({
628
+ type: "unsupported-setting",
629
+ setting: "cacheControl",
630
+ details: `cache_control cannot be set on ${context.type}. It will be ignored.`
631
+ });
632
+ return void 0;
633
+ }
634
+ this.breakpointCount++;
635
+ if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
636
+ this.warnings.push({
637
+ type: "unsupported-setting",
638
+ setting: "cacheControl",
639
+ details: `Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
640
+ });
641
+ return void 0;
642
+ }
643
+ return cacheControlValue;
644
+ }
645
+ getWarnings() {
646
+ return this.warnings;
647
+ }
648
+ };
615
649
 
616
650
  // src/tool/text-editor_20250728.ts
617
651
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
@@ -757,11 +791,13 @@ var import_provider_utils7 = require("@ai-sdk/provider-utils");
757
791
  async function prepareTools({
758
792
  tools,
759
793
  toolChoice,
760
- disableParallelToolUse
794
+ disableParallelToolUse,
795
+ cacheControlValidator
761
796
  }) {
762
797
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
763
798
  const toolWarnings = [];
764
799
  const betas = /* @__PURE__ */ new Set();
800
+ const validator = cacheControlValidator || new CacheControlValidator();
765
801
  if (tools == null) {
766
802
  return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
767
803
  }
@@ -769,7 +805,10 @@ async function prepareTools({
769
805
  for (const tool of tools) {
770
806
  switch (tool.type) {
771
807
  case "function": {
772
- const cacheControl = getCacheControl(tool.providerOptions);
808
+ const cacheControl = validator.getCacheControl(tool.providerOptions, {
809
+ type: "tool definition",
810
+ canCache: true
811
+ });
773
812
  anthropicTools2.push({
774
813
  name: tool.name,
775
814
  description: tool.description,
@@ -784,7 +823,8 @@ async function prepareTools({
784
823
  betas.add("code-execution-2025-05-22");
785
824
  anthropicTools2.push({
786
825
  type: "code_execution_20250522",
787
- name: "code_execution"
826
+ name: "code_execution",
827
+ cache_control: void 0
788
828
  });
789
829
  break;
790
830
  }
@@ -803,7 +843,8 @@ async function prepareTools({
803
843
  type: "computer_20250124",
804
844
  display_width_px: tool.args.displayWidthPx,
805
845
  display_height_px: tool.args.displayHeightPx,
806
- display_number: tool.args.displayNumber
846
+ display_number: tool.args.displayNumber,
847
+ cache_control: void 0
807
848
  });
808
849
  break;
809
850
  }
@@ -814,7 +855,8 @@ async function prepareTools({
814
855
  type: "computer_20241022",
815
856
  display_width_px: tool.args.displayWidthPx,
816
857
  display_height_px: tool.args.displayHeightPx,
817
- display_number: tool.args.displayNumber
858
+ display_number: tool.args.displayNumber,
859
+ cache_control: void 0
818
860
  });
819
861
  break;
820
862
  }
@@ -822,7 +864,8 @@ async function prepareTools({
822
864
  betas.add("computer-use-2025-01-24");
823
865
  anthropicTools2.push({
824
866
  name: "str_replace_editor",
825
- type: "text_editor_20250124"
867
+ type: "text_editor_20250124",
868
+ cache_control: void 0
826
869
  });
827
870
  break;
828
871
  }
@@ -830,7 +873,8 @@ async function prepareTools({
830
873
  betas.add("computer-use-2024-10-22");
831
874
  anthropicTools2.push({
832
875
  name: "str_replace_editor",
833
- type: "text_editor_20241022"
876
+ type: "text_editor_20241022",
877
+ cache_control: void 0
834
878
  });
835
879
  break;
836
880
  }
@@ -838,7 +882,8 @@ async function prepareTools({
838
882
  betas.add("computer-use-2025-01-24");
839
883
  anthropicTools2.push({
840
884
  name: "str_replace_based_edit_tool",
841
- type: "text_editor_20250429"
885
+ type: "text_editor_20250429",
886
+ cache_control: void 0
842
887
  });
843
888
  break;
844
889
  }
@@ -850,7 +895,8 @@ async function prepareTools({
850
895
  anthropicTools2.push({
851
896
  name: "str_replace_based_edit_tool",
852
897
  type: "text_editor_20250728",
853
- max_characters: args.maxCharacters
898
+ max_characters: args.maxCharacters,
899
+ cache_control: void 0
854
900
  });
855
901
  break;
856
902
  }
@@ -858,7 +904,8 @@ async function prepareTools({
858
904
  betas.add("computer-use-2025-01-24");
859
905
  anthropicTools2.push({
860
906
  name: "bash",
861
- type: "bash_20250124"
907
+ type: "bash_20250124",
908
+ cache_control: void 0
862
909
  });
863
910
  break;
864
911
  }
@@ -866,7 +913,8 @@ async function prepareTools({
866
913
  betas.add("computer-use-2024-10-22");
867
914
  anthropicTools2.push({
868
915
  name: "bash",
869
- type: "bash_20241022"
916
+ type: "bash_20241022",
917
+ cache_control: void 0
870
918
  });
871
919
  break;
872
920
  }
@@ -891,7 +939,8 @@ async function prepareTools({
891
939
  allowed_domains: args.allowedDomains,
892
940
  blocked_domains: args.blockedDomains,
893
941
  citations: args.citations,
894
- max_content_tokens: args.maxContentTokens
942
+ max_content_tokens: args.maxContentTokens,
943
+ cache_control: void 0
895
944
  });
896
945
  break;
897
946
  }
@@ -906,7 +955,8 @@ async function prepareTools({
906
955
  max_uses: args.maxUses,
907
956
  allowed_domains: args.allowedDomains,
908
957
  blocked_domains: args.blockedDomains,
909
- user_location: args.userLocation
958
+ user_location: args.userLocation,
959
+ cache_control: void 0
910
960
  });
911
961
  break;
912
962
  }
@@ -1118,11 +1168,13 @@ function convertToString(data) {
1118
1168
  async function convertToAnthropicMessagesPrompt({
1119
1169
  prompt,
1120
1170
  sendReasoning,
1121
- warnings
1171
+ warnings,
1172
+ cacheControlValidator
1122
1173
  }) {
1123
1174
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
1124
1175
  const betas = /* @__PURE__ */ new Set();
1125
1176
  const blocks = groupIntoBlocks(prompt);
1177
+ const validator = cacheControlValidator || new CacheControlValidator();
1126
1178
  let system = void 0;
1127
1179
  const messages = [];
1128
1180
  async function shouldEnableCitations(providerMetadata) {
@@ -1159,7 +1211,10 @@ async function convertToAnthropicMessagesPrompt({
1159
1211
  system = block.messages.map(({ content, providerOptions }) => ({
1160
1212
  type: "text",
1161
1213
  text: content,
1162
- cache_control: getCacheControl(providerOptions)
1214
+ cache_control: validator.getCacheControl(providerOptions, {
1215
+ type: "system message",
1216
+ canCache: true
1217
+ })
1163
1218
  }));
1164
1219
  break;
1165
1220
  }
@@ -1172,7 +1227,13 @@ async function convertToAnthropicMessagesPrompt({
1172
1227
  for (let j = 0; j < content.length; j++) {
1173
1228
  const part = content[j];
1174
1229
  const isLastPart = j === content.length - 1;
1175
- const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1230
+ const cacheControl = (_a = validator.getCacheControl(part.providerOptions, {
1231
+ type: "user message part",
1232
+ canCache: true
1233
+ })) != null ? _a : isLastPart ? validator.getCacheControl(message.providerOptions, {
1234
+ type: "user message",
1235
+ canCache: true
1236
+ }) : void 0;
1176
1237
  switch (part.type) {
1177
1238
  case "text": {
1178
1239
  anthropicContent.push({
@@ -1260,7 +1321,13 @@ async function convertToAnthropicMessagesPrompt({
1260
1321
  for (let i2 = 0; i2 < content.length; i2++) {
1261
1322
  const part = content[i2];
1262
1323
  const isLastPart = i2 === content.length - 1;
1263
- const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
1324
+ const cacheControl = (_d = validator.getCacheControl(part.providerOptions, {
1325
+ type: "tool result part",
1326
+ canCache: true
1327
+ })) != null ? _d : isLastPart ? validator.getCacheControl(message.providerOptions, {
1328
+ type: "tool result message",
1329
+ canCache: true
1330
+ }) : void 0;
1264
1331
  const output = part.output;
1265
1332
  let contentValue;
1266
1333
  switch (output.type) {
@@ -1270,8 +1337,7 @@ async function convertToAnthropicMessagesPrompt({
1270
1337
  case "text":
1271
1338
  return {
1272
1339
  type: "text",
1273
- text: contentPart.text,
1274
- cache_control: cacheControl
1340
+ text: contentPart.text
1275
1341
  };
1276
1342
  case "image-data": {
1277
1343
  return {
@@ -1280,8 +1346,7 @@ async function convertToAnthropicMessagesPrompt({
1280
1346
  type: "base64",
1281
1347
  media_type: contentPart.mediaType,
1282
1348
  data: contentPart.data
1283
- },
1284
- cache_control: cacheControl
1349
+ }
1285
1350
  };
1286
1351
  }
1287
1352
  case "file-data": {
@@ -1293,8 +1358,7 @@ async function convertToAnthropicMessagesPrompt({
1293
1358
  type: "base64",
1294
1359
  media_type: contentPart.mediaType,
1295
1360
  data: contentPart.data
1296
- },
1297
- cache_control: cacheControl
1361
+ }
1298
1362
  };
1299
1363
  }
1300
1364
  warnings.push({
@@ -1355,7 +1419,13 @@ async function convertToAnthropicMessagesPrompt({
1355
1419
  for (let k = 0; k < content.length; k++) {
1356
1420
  const part = content[k];
1357
1421
  const isLastContentPart = k === content.length - 1;
1358
- const cacheControl = (_f = getCacheControl(part.providerOptions)) != null ? _f : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
1422
+ const cacheControl = (_f = validator.getCacheControl(part.providerOptions, {
1423
+ type: "assistant message part",
1424
+ canCache: true
1425
+ })) != null ? _f : isLastContentPart ? validator.getCacheControl(message.providerOptions, {
1426
+ type: "assistant message",
1427
+ canCache: true
1428
+ }) : void 0;
1359
1429
  switch (part.type) {
1360
1430
  case "text": {
1361
1431
  anthropicContent.push({
@@ -1379,17 +1449,23 @@ async function convertToAnthropicMessagesPrompt({
1379
1449
  });
1380
1450
  if (reasoningMetadata != null) {
1381
1451
  if (reasoningMetadata.signature != null) {
1452
+ validator.getCacheControl(part.providerOptions, {
1453
+ type: "thinking block",
1454
+ canCache: false
1455
+ });
1382
1456
  anthropicContent.push({
1383
1457
  type: "thinking",
1384
1458
  thinking: part.text,
1385
- signature: reasoningMetadata.signature,
1386
- cache_control: cacheControl
1459
+ signature: reasoningMetadata.signature
1387
1460
  });
1388
1461
  } else if (reasoningMetadata.redactedData != null) {
1462
+ validator.getCacheControl(part.providerOptions, {
1463
+ type: "redacted thinking block",
1464
+ canCache: false
1465
+ });
1389
1466
  anthropicContent.push({
1390
1467
  type: "redacted_thinking",
1391
- data: reasoningMetadata.redactedData,
1392
- cache_control: cacheControl
1468
+ data: reasoningMetadata.redactedData
1393
1469
  });
1394
1470
  } else {
1395
1471
  warnings.push({
@@ -1802,10 +1878,12 @@ var AnthropicMessagesLanguageModel = class {
1802
1878
  providerOptions,
1803
1879
  schema: anthropicProviderOptions
1804
1880
  });
1881
+ const cacheControlValidator = new CacheControlValidator();
1805
1882
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1806
1883
  prompt,
1807
1884
  sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1808
- warnings
1885
+ warnings,
1886
+ cacheControlValidator
1809
1887
  });
1810
1888
  const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1811
1889
  const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
@@ -1919,20 +1997,23 @@ var AnthropicMessagesLanguageModel = class {
1919
1997
  jsonResponseTool != null ? {
1920
1998
  tools: [jsonResponseTool],
1921
1999
  toolChoice: { type: "tool", toolName: jsonResponseTool.name },
1922
- disableParallelToolUse: true
2000
+ disableParallelToolUse: true,
2001
+ cacheControlValidator
1923
2002
  } : {
1924
2003
  tools: tools != null ? tools : [],
1925
2004
  toolChoice,
1926
- disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
2005
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse,
2006
+ cacheControlValidator
1927
2007
  }
1928
2008
  );
2009
+ const cacheWarnings = cacheControlValidator.getWarnings();
1929
2010
  return {
1930
2011
  args: {
1931
2012
  ...baseArgs,
1932
2013
  tools: anthropicTools2,
1933
2014
  tool_choice: anthropicToolChoice
1934
2015
  },
1935
- warnings: [...warnings, ...toolWarnings],
2016
+ warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1936
2017
  betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1937
2018
  usesJsonResponseTool: jsonResponseTool != null
1938
2019
  };