@ai-sdk/anthropic 3.0.115 → 3.0.117

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.
@@ -850,6 +850,21 @@ var anthropicFilePartProviderOptions = z3.object({
850
850
  context: z3.string().optional()
851
851
  });
852
852
  var anthropicSystemMessageProviderOptions = z3.object({
853
+ /**
854
+ * Clears this mid-conversation system message after the current turn.
855
+ *
856
+ * Requires the `mid-conversation-system-clear-at-2026-08-21` beta,
857
+ * which is added automatically.
858
+ */
859
+ clearAt: z3.literal("next_user_message").optional(),
860
+ /**
861
+ * Overrides the effort level for the turn following this
862
+ * mid-conversation system message.
863
+ *
864
+ * Requires the `mid-conversation-effort-2026-08-01` beta,
865
+ * which is added automatically.
866
+ */
867
+ effort: z3.enum(["low", "medium", "high", "xhigh", "max"]).optional(),
853
868
  /**
854
869
  * Mid-conversation tool changes. Adds or removes tools from the
855
870
  * conversation's tool set between turns without invalidating the prompt
@@ -877,6 +892,9 @@ var anthropicSystemMessageProviderOptions = z3.object({
877
892
  ])
878
893
  ).optional()
879
894
  });
895
+ var anthropicThinkingBlockBinding = z3.object({
896
+ prefixMismatchBehavior: z3.literal("drop_block")
897
+ });
880
898
  var anthropicLanguageModelOptions = z3.object({
881
899
  /**
882
900
  * Whether to send reasoning to the model.
@@ -898,24 +916,41 @@ var anthropicLanguageModelOptions = z3.object({
898
916
  * When enabled, responses include thinking content blocks showing Claude's thinking process before the final answer.
899
917
  * Requires a minimum budget of 1,024 tokens and counts towards the `max_tokens` limit.
900
918
  */
901
- thinking: z3.discriminatedUnion("type", [
902
- z3.object({
903
- /** for Sonnet 4.6, Opus 4.6, and newer models */
904
- type: z3.literal("adaptive"),
905
- /**
906
- * Controls whether thinking content is included in the response.
907
- * - `"omitted"`: Thinking blocks are present but text is empty (default for Opus 4.7+).
908
- * - `"summarized"`: Thinking content is returned. Required to see reasoning output.
909
- */
910
- display: z3.enum(["omitted", "summarized"]).optional()
911
- }),
912
- z3.object({
913
- /** for models before Opus 4.6, except Sonnet 4.6 still supports it */
914
- type: z3.literal("enabled"),
915
- budgetTokens: z3.number().optional()
916
- }),
919
+ thinking: z3.union([
920
+ z3.discriminatedUnion("type", [
921
+ z3.object({
922
+ /** for Sonnet 4.6, Opus 4.6, and newer models */
923
+ type: z3.literal("adaptive"),
924
+ /**
925
+ * Controls whether thinking content is included in the response.
926
+ * - `"omitted"`: Thinking blocks are present but text is empty (default for Opus 4.7+).
927
+ * - `"summarized"`: Thinking content is returned. Required to see reasoning output.
928
+ * - `"updates"`: Thinking updates are returned between tool calls.
929
+ */
930
+ display: z3.enum(["omitted", "summarized", "updates"]).optional(),
931
+ /**
932
+ * Controls how thinking blocks are bound to an assistant prefix.
933
+ *
934
+ * Requires the `thinking-binding-controls-2026-08-01` beta,
935
+ * which is added automatically.
936
+ */
937
+ blockBinding: anthropicThinkingBlockBinding.optional()
938
+ }),
939
+ z3.object({
940
+ /** for models before Opus 4.6, except Sonnet 4.6 still supports it */
941
+ type: z3.literal("enabled"),
942
+ budgetTokens: z3.number().optional()
943
+ }),
944
+ z3.object({
945
+ type: z3.literal("disabled")
946
+ })
947
+ ]),
948
+ /**
949
+ * Configures prefix mismatch recovery without changing the model's
950
+ * default thinking mode.
951
+ */
917
952
  z3.object({
918
- type: z3.literal("disabled")
953
+ blockBinding: anthropicThinkingBlockBinding
919
954
  })
920
955
  ]).optional(),
921
956
  /**
@@ -2287,8 +2322,9 @@ async function convertToAnthropicMessagesPrompt({
2287
2322
  const type = block.type;
2288
2323
  switch (type) {
2289
2324
  case "system": {
2290
- const content = [];
2325
+ const systemMessages = [];
2291
2326
  let toolChangeCount = 0;
2327
+ let hasMidConversationOptions = false;
2292
2328
  for (const { content: text, providerOptions } of block.messages) {
2293
2329
  const systemMessageOptions = await parseProviderOptions({
2294
2330
  provider: "anthropic",
@@ -2296,7 +2332,10 @@ async function convertToAnthropicMessagesPrompt({
2296
2332
  schema: anthropicSystemMessageProviderOptions
2297
2333
  });
2298
2334
  const toolChanges = (_a = systemMessageOptions == null ? void 0 : systemMessageOptions.toolChanges) != null ? _a : [];
2299
- if (text !== "" || toolChanges.length === 0) {
2335
+ const clearAt = systemMessageOptions == null ? void 0 : systemMessageOptions.clearAt;
2336
+ const effort = systemMessageOptions == null ? void 0 : systemMessageOptions.effort;
2337
+ const content = [];
2338
+ if (text !== "" || toolChanges.length === 0 && clearAt == null && effort == null) {
2300
2339
  content.push({
2301
2340
  type: "text",
2302
2341
  text,
@@ -2316,23 +2355,44 @@ async function convertToAnthropicMessagesPrompt({
2316
2355
  }
2317
2356
  });
2318
2357
  }
2358
+ hasMidConversationOptions || (hasMidConversationOptions = clearAt != null || effort != null);
2359
+ systemMessages.push({
2360
+ role: "system",
2361
+ content,
2362
+ ...clearAt != null && { clear_at: clearAt },
2363
+ ...effort != null && { output_config: { effort } }
2364
+ });
2319
2365
  }
2320
- if (i === 0 || system == null && toolChangeCount === 0) {
2366
+ if (i === 0 || system == null && toolChangeCount === 0 && !hasMidConversationOptions) {
2321
2367
  if (toolChangeCount > 0) {
2322
2368
  warnings.push({
2323
2369
  type: "other",
2324
2370
  message: "tool changes on the initial system message are not supported by Anthropic. Configure the initial tool set via the tools option instead. The tool changes have been ignored."
2325
2371
  });
2326
2372
  }
2327
- system = content.filter(
2328
- (part) => part.type === "text"
2373
+ if (hasMidConversationOptions) {
2374
+ warnings.push({
2375
+ type: "other",
2376
+ message: "clearAt and effort on the initial system message are not supported by Anthropic. Configure these options on a mid-conversation system message instead. The options have been ignored."
2377
+ });
2378
+ }
2379
+ system = systemMessages.flatMap(
2380
+ (message) => message.content.filter(
2381
+ (part) => part.type === "text"
2382
+ )
2329
2383
  );
2330
2384
  } else {
2331
- messages.push({ role: "system", content });
2385
+ messages.push(...systemMessages);
2332
2386
  betas.add("mid-conversation-system-2026-04-07");
2333
2387
  if (toolChangeCount > 0) {
2334
2388
  betas.add("mid-conversation-tool-changes-2026-07-01");
2335
2389
  }
2390
+ if (systemMessages.some((message) => message.clear_at != null)) {
2391
+ betas.add("mid-conversation-system-clear-at-2026-08-21");
2392
+ }
2393
+ if (systemMessages.some((message) => message.output_config != null)) {
2394
+ betas.add("mid-conversation-effort-2026-08-01");
2395
+ }
2336
2396
  }
2337
2397
  break;
2338
2398
  }
@@ -3448,7 +3508,7 @@ var AnthropicMessagesLanguageModel = class {
3448
3508
  providerOptions,
3449
3509
  stream
3450
3510
  }) {
3451
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
3511
+ var _a, _b, _c, _d, _e, _f, _g;
3452
3512
  const warnings = [];
3453
3513
  if (frequencyPenalty != null) {
3454
3514
  warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
@@ -3591,7 +3651,9 @@ var AnthropicMessagesLanguageModel = class {
3591
3651
  cacheControlValidator,
3592
3652
  toolNameMapping
3593
3653
  });
3594
- if (rejectsThinkingDisabledAboveHighEffort && ((_e = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _e.type) === "disabled" && (anthropicOptions.effort === "xhigh" || anthropicOptions.effort === "max")) {
3654
+ const thinking = anthropicOptions == null ? void 0 : anthropicOptions.thinking;
3655
+ const thinkingType = thinking != null && "type" in thinking ? thinking.type : void 0;
3656
+ if (rejectsThinkingDisabledAboveHighEffort && thinkingType === "disabled" && (anthropicOptions.effort === "xhigh" || anthropicOptions.effort === "max")) {
3595
3657
  warnings.push({
3596
3658
  type: "unsupported",
3597
3659
  feature: "providerOptions.anthropic.effort",
@@ -3599,11 +3661,12 @@ var AnthropicMessagesLanguageModel = class {
3599
3661
  });
3600
3662
  anthropicOptions.effort = "high";
3601
3663
  }
3602
- const thinkingType = (_f = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _f.type;
3603
3664
  const isThinking = thinkingType === "enabled" || thinkingType === "adaptive";
3604
3665
  const sendThinking = isThinking || thinkingType === "disabled";
3605
- let thinkingBudget = thinkingType === "enabled" ? (_g = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _g.budgetTokens : void 0;
3606
- const thinkingDisplay = thinkingType === "adaptive" ? (_h = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _h.display : void 0;
3666
+ let thinkingBudget = thinkingType === "enabled" && thinking != null && "budgetTokens" in thinking ? thinking.budgetTokens : void 0;
3667
+ const thinkingDisplay = thinkingType === "adaptive" && thinking != null && "display" in thinking ? thinking.display : void 0;
3668
+ const thinkingBlockBinding = thinking != null && "blockBinding" in thinking ? thinking.blockBinding : void 0;
3669
+ const sendThinkingConfig = sendThinking || thinkingBlockBinding != null;
3607
3670
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
3608
3671
  const baseArgs = {
3609
3672
  // model id:
@@ -3615,11 +3678,16 @@ var AnthropicMessagesLanguageModel = class {
3615
3678
  top_p: topP,
3616
3679
  stop_sequences: stopSequences,
3617
3680
  // provider specific settings:
3618
- ...sendThinking && {
3681
+ ...sendThinkingConfig && {
3619
3682
  thinking: {
3620
- type: thinkingType,
3683
+ ...thinkingType != null && { type: thinkingType },
3621
3684
  ...thinkingBudget != null && { budget_tokens: thinkingBudget },
3622
- ...thinkingDisplay != null && { display: thinkingDisplay }
3685
+ ...thinkingDisplay != null && { display: thinkingDisplay },
3686
+ ...thinkingBlockBinding != null && {
3687
+ block_binding: {
3688
+ prefix_mismatch_behavior: thinkingBlockBinding.prefixMismatchBehavior
3689
+ }
3690
+ }
3623
3691
  }
3624
3692
  },
3625
3693
  ...((anthropicOptions == null ? void 0 : anthropicOptions.effort) || (anthropicOptions == null ? void 0 : anthropicOptions.taskBudget) || useStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) && {
@@ -3656,7 +3724,7 @@ var AnthropicMessagesLanguageModel = class {
3656
3724
  ...(anthropicOptions == null ? void 0 : anthropicOptions.cacheControl) && {
3657
3725
  cache_control: anthropicOptions.cacheControl
3658
3726
  },
3659
- ...((_i = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _i.userId) != null && {
3727
+ ...((_e = anthropicOptions == null ? void 0 : anthropicOptions.metadata) == null ? void 0 : _e.userId) != null && {
3660
3728
  metadata: { user_id: anthropicOptions.metadata.userId }
3661
3729
  },
3662
3730
  // mcp servers:
@@ -3826,6 +3894,12 @@ var AnthropicMessagesLanguageModel = class {
3826
3894
  if (anthropicOptions == null ? void 0 : anthropicOptions.taskBudget) {
3827
3895
  betas.add("task-budgets-2026-03-13");
3828
3896
  }
3897
+ if (thinkingDisplay === "updates") {
3898
+ betas.add("thinking-display-updates-2026-08-18");
3899
+ }
3900
+ if (thinkingBlockBinding != null) {
3901
+ betas.add("thinking-binding-controls-2026-08-01");
3902
+ }
3829
3903
  if ((anthropicOptions == null ? void 0 : anthropicOptions.speed) === "fast") {
3830
3904
  betas.add("fast-mode-2026-02-01");
3831
3905
  }
@@ -3834,7 +3908,7 @@ var AnthropicMessagesLanguageModel = class {
3834
3908
  } else if ((anthropicOptions == null ? void 0 : anthropicOptions.fallbacks) && anthropicOptions.fallbacks.length > 0) {
3835
3909
  betas.add("server-side-fallback-2026-06-01");
3836
3910
  }
3837
- const defaultEagerInputStreaming = stream && ((_j = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _j : true);
3911
+ const defaultEagerInputStreaming = stream && ((_f = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _f : true);
3838
3912
  const {
3839
3913
  tools: anthropicTools2,
3840
3914
  toolChoice: anthropicToolChoice,
@@ -3873,7 +3947,7 @@ var AnthropicMessagesLanguageModel = class {
3873
3947
  ...betas,
3874
3948
  ...toolsBetas,
3875
3949
  ...userSuppliedBetas,
3876
- ...(_k = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _k : []
3950
+ ...(_g = anthropicOptions == null ? void 0 : anthropicOptions.anthropicBeta) != null ? _g : []
3877
3951
  ]),
3878
3952
  usesJsonResponseTool: jsonResponseTool != null,
3879
3953
  toolNameMapping,