@ai-sdk/anthropic 3.0.0-beta.77 → 3.0.0-beta.79

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.
@@ -49,6 +49,29 @@ var anthropicFailedResponseHandler = (0, import_provider_utils.createJsonErrorRe
49
49
  errorToMessage: (data) => data.error.message
50
50
  });
51
51
 
52
+ // src/convert-anthropic-messages-usage.ts
53
+ function convertAnthropicMessagesUsage(usage) {
54
+ var _a, _b;
55
+ const inputTokens = usage.input_tokens;
56
+ const outputTokens = usage.output_tokens;
57
+ const cacheCreationTokens = (_a = usage.cache_creation_input_tokens) != null ? _a : 0;
58
+ const cacheReadTokens = (_b = usage.cache_read_input_tokens) != null ? _b : 0;
59
+ return {
60
+ inputTokens: {
61
+ total: inputTokens + cacheCreationTokens + cacheReadTokens,
62
+ noCache: inputTokens,
63
+ cacheRead: cacheReadTokens,
64
+ cacheWrite: cacheCreationTokens
65
+ },
66
+ outputTokens: {
67
+ total: outputTokens,
68
+ text: void 0,
69
+ reasoning: void 0
70
+ },
71
+ raw: usage
72
+ };
73
+ }
74
+
52
75
  // src/anthropic-messages-api.ts
53
76
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
54
77
  var import_v42 = require("zod/v4");
@@ -2227,7 +2250,7 @@ var AnthropicMessagesLanguageModel = class {
2227
2250
  toolNameMapping
2228
2251
  });
2229
2252
  const isThinking = ((_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.type) === "enabled";
2230
- const thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2253
+ let thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2231
2254
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
2232
2255
  const baseArgs = {
2233
2256
  // model id:
@@ -2319,9 +2342,16 @@ var AnthropicMessagesLanguageModel = class {
2319
2342
  };
2320
2343
  if (isThinking) {
2321
2344
  if (thinkingBudget == null) {
2322
- throw new import_provider3.UnsupportedFunctionalityError({
2323
- functionality: "thinking requires a budget"
2345
+ warnings.push({
2346
+ type: "compatibility",
2347
+ feature: "extended thinking",
2348
+ details: "thinking budget is required when thinking is enabled. using default budget of 1024 tokens."
2324
2349
  });
2350
+ baseArgs.thinking = {
2351
+ type: "enabled",
2352
+ budget_tokens: 1024
2353
+ };
2354
+ thinkingBudget = 1024;
2325
2355
  }
2326
2356
  if (baseArgs.temperature != null) {
2327
2357
  baseArgs.temperature = void 0;
@@ -2347,7 +2377,7 @@ var AnthropicMessagesLanguageModel = class {
2347
2377
  details: "topP is not supported when thinking is enabled"
2348
2378
  });
2349
2379
  }
2350
- baseArgs.max_tokens = maxTokens + thinkingBudget;
2380
+ baseArgs.max_tokens = maxTokens + (thinkingBudget != null ? thinkingBudget : 0);
2351
2381
  }
2352
2382
  if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2353
2383
  if (maxOutputTokens != null) {
@@ -2476,7 +2506,7 @@ var AnthropicMessagesLanguageModel = class {
2476
2506
  });
2477
2507
  }
2478
2508
  async doGenerate(options) {
2479
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2509
+ var _a, _b, _c, _d, _e, _f, _g, _h;
2480
2510
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2481
2511
  ...options,
2482
2512
  stream: false,
@@ -2775,16 +2805,11 @@ var AnthropicMessagesLanguageModel = class {
2775
2805
  finishReason: response.stop_reason,
2776
2806
  isJsonResponseFromTool
2777
2807
  }),
2778
- usage: {
2779
- inputTokens: response.usage.input_tokens,
2780
- outputTokens: response.usage.output_tokens,
2781
- totalTokens: response.usage.input_tokens + response.usage.output_tokens,
2782
- cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
2783
- },
2808
+ usage: convertAnthropicMessagesUsage(response.usage),
2784
2809
  request: { body: args },
2785
2810
  response: {
2786
- id: (_c = response.id) != null ? _c : void 0,
2787
- modelId: (_d = response.model) != null ? _d : void 0,
2811
+ id: (_b = response.id) != null ? _b : void 0,
2812
+ modelId: (_c = response.model) != null ? _c : void 0,
2788
2813
  headers: responseHeaders,
2789
2814
  body: rawResponse
2790
2815
  },
@@ -2792,20 +2817,20 @@ var AnthropicMessagesLanguageModel = class {
2792
2817
  providerMetadata: {
2793
2818
  anthropic: {
2794
2819
  usage: response.usage,
2795
- cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null,
2796
- stopSequence: (_f = response.stop_sequence) != null ? _f : null,
2820
+ cacheCreationInputTokens: (_d = response.usage.cache_creation_input_tokens) != null ? _d : null,
2821
+ stopSequence: (_e = response.stop_sequence) != null ? _e : null,
2797
2822
  container: response.container ? {
2798
2823
  expiresAt: response.container.expires_at,
2799
2824
  id: response.container.id,
2800
- skills: (_h = (_g = response.container.skills) == null ? void 0 : _g.map((skill) => ({
2825
+ skills: (_g = (_f = response.container.skills) == null ? void 0 : _f.map((skill) => ({
2801
2826
  type: skill.type,
2802
2827
  skillId: skill.skill_id,
2803
2828
  version: skill.version
2804
- }))) != null ? _h : null
2829
+ }))) != null ? _g : null
2805
2830
  } : null,
2806
- contextManagement: (_i = mapAnthropicResponseContextManagement(
2831
+ contextManagement: (_h = mapAnthropicResponseContextManagement(
2807
2832
  response.context_management
2808
- )) != null ? _i : null
2833
+ )) != null ? _h : null
2809
2834
  }
2810
2835
  }
2811
2836
  };
@@ -2838,9 +2863,10 @@ var AnthropicMessagesLanguageModel = class {
2838
2863
  });
2839
2864
  let finishReason = "unknown";
2840
2865
  const usage = {
2841
- inputTokens: void 0,
2842
- outputTokens: void 0,
2843
- totalTokens: void 0
2866
+ input_tokens: 0,
2867
+ output_tokens: 0,
2868
+ cache_creation_input_tokens: 0,
2869
+ cache_read_input_tokens: 0
2844
2870
  };
2845
2871
  const contentBlocks = {};
2846
2872
  const mcpToolCalls = {};
@@ -2858,7 +2884,7 @@ var AnthropicMessagesLanguageModel = class {
2858
2884
  controller.enqueue({ type: "stream-start", warnings });
2859
2885
  },
2860
2886
  transform(chunk, controller) {
2861
- var _a2, _b2, _c, _d, _e, _f, _g, _h, _i, _j;
2887
+ var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
2862
2888
  if (options.includeRawChunks) {
2863
2889
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2864
2890
  }
@@ -3297,35 +3323,35 @@ var AnthropicMessagesLanguageModel = class {
3297
3323
  }
3298
3324
  }
3299
3325
  case "message_start": {
3300
- usage.inputTokens = value.message.usage.input_tokens;
3301
- usage.cachedInputTokens = (_b2 = value.message.usage.cache_read_input_tokens) != null ? _b2 : void 0;
3326
+ usage.input_tokens = value.message.usage.input_tokens;
3327
+ usage.cache_read_input_tokens = (_b2 = value.message.usage.cache_read_input_tokens) != null ? _b2 : 0;
3328
+ usage.cache_creation_input_tokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : 0;
3302
3329
  rawUsage = {
3303
3330
  ...value.message.usage
3304
3331
  };
3305
- cacheCreationInputTokens = (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null;
3332
+ cacheCreationInputTokens = (_d = value.message.usage.cache_creation_input_tokens) != null ? _d : null;
3306
3333
  controller.enqueue({
3307
3334
  type: "response-metadata",
3308
- id: (_d = value.message.id) != null ? _d : void 0,
3309
- modelId: (_e = value.message.model) != null ? _e : void 0
3335
+ id: (_e = value.message.id) != null ? _e : void 0,
3336
+ modelId: (_f = value.message.model) != null ? _f : void 0
3310
3337
  });
3311
3338
  return;
3312
3339
  }
3313
3340
  case "message_delta": {
3314
- usage.outputTokens = value.usage.output_tokens;
3315
- usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
3341
+ usage.output_tokens = value.usage.output_tokens;
3316
3342
  finishReason = mapAnthropicStopReason({
3317
3343
  finishReason: value.delta.stop_reason,
3318
3344
  isJsonResponseFromTool
3319
3345
  });
3320
- stopSequence = (_h = value.delta.stop_sequence) != null ? _h : null;
3346
+ stopSequence = (_g = value.delta.stop_sequence) != null ? _g : null;
3321
3347
  container = value.delta.container != null ? {
3322
3348
  expiresAt: value.delta.container.expires_at,
3323
3349
  id: value.delta.container.id,
3324
- skills: (_j = (_i = value.delta.container.skills) == null ? void 0 : _i.map((skill) => ({
3350
+ skills: (_i = (_h = value.delta.container.skills) == null ? void 0 : _h.map((skill) => ({
3325
3351
  type: skill.type,
3326
3352
  skillId: skill.skill_id,
3327
3353
  version: skill.version
3328
- }))) != null ? _j : null
3354
+ }))) != null ? _i : null
3329
3355
  } : null;
3330
3356
  if (value.delta.context_management) {
3331
3357
  contextManagement = mapAnthropicResponseContextManagement(
@@ -3342,7 +3368,7 @@ var AnthropicMessagesLanguageModel = class {
3342
3368
  controller.enqueue({
3343
3369
  type: "finish",
3344
3370
  finishReason,
3345
- usage,
3371
+ usage: convertAnthropicMessagesUsage(usage),
3346
3372
  providerMetadata: {
3347
3373
  anthropic: {
3348
3374
  usage: rawUsage != null ? rawUsage : null,