@ai-sdk/anthropic 3.0.0-beta.76 → 3.0.0-beta.78

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 3.0.0-beta.78
4
+
5
+ ### Patch Changes
6
+
7
+ - 9e1e758: fix(anthropic): use default thinking budget when unspecified
8
+
9
+ ## 3.0.0-beta.77
10
+
11
+ ### Patch Changes
12
+
13
+ - b2dbfbf: add context_management for anthropic
14
+
3
15
  ## 3.0.0-beta.76
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -40,6 +40,58 @@ interface AnthropicMessageMetadata {
40
40
  version: string;
41
41
  }> | null;
42
42
  } | null;
43
+ /**
44
+ * Context management response.
45
+ *
46
+ * Information about context management strategies applied during the request.
47
+ */
48
+ contextManagement: {
49
+ /**
50
+ * List of context management edits that were applied.
51
+ * Each item in the array is a specific type of context management edit.
52
+ */
53
+ appliedEdits: Array<
54
+ /**
55
+ * Represents an edit where a certain number of tool uses and input tokens were cleared.
56
+ */
57
+ {
58
+ /**
59
+ * The type of context management edit applied.
60
+ * Possible value: 'clear_tool_uses_20250919'
61
+ */
62
+ type: 'clear_tool_uses_20250919';
63
+ /**
64
+ * Number of tool uses that were cleared by this edit.
65
+ * Minimum: 0
66
+ */
67
+ clearedToolUses: number;
68
+ /**
69
+ * Number of input tokens cleared by this edit.
70
+ * Minimum: 0
71
+ */
72
+ clearedInputTokens: number;
73
+ }
74
+ /**
75
+ * Represents an edit where a certain number of thinking turns and input tokens were cleared.
76
+ */
77
+ | {
78
+ /**
79
+ * The type of context management edit applied.
80
+ * Possible value: 'clear_thinking_20251015'
81
+ */
82
+ type: 'clear_thinking_20251015';
83
+ /**
84
+ * Number of thinking turns that were cleared by this edit.
85
+ * Minimum: 0
86
+ */
87
+ clearedThinkingTurns: number;
88
+ /**
89
+ * Number of input tokens cleared by this edit.
90
+ * Minimum: 0
91
+ */
92
+ clearedInputTokens: number;
93
+ }>;
94
+ } | null;
43
95
  }
44
96
 
45
97
  type AnthropicMessagesModelId = 'claude-3-5-haiku-20241022' | 'claude-3-5-haiku-latest' | 'claude-3-7-sonnet-20250219' | 'claude-3-7-sonnet-latest' | 'claude-3-haiku-20240307' | 'claude-haiku-4-5-20251001' | 'claude-haiku-4-5' | 'claude-opus-4-0' | 'claude-opus-4-1-20250805' | 'claude-opus-4-1' | 'claude-opus-4-20250514' | 'claude-opus-4-5' | 'claude-opus-4-5-20251101' | 'claude-sonnet-4-0' | 'claude-sonnet-4-20250514' | 'claude-sonnet-4-5-20250929' | 'claude-sonnet-4-5' | (string & {});
@@ -83,6 +135,34 @@ declare const anthropicProviderOptions: z.ZodObject<{
83
135
  medium: "medium";
84
136
  high: "high";
85
137
  }>>;
138
+ contextManagement: z.ZodOptional<z.ZodObject<{
139
+ edits: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
140
+ type: z.ZodLiteral<"clear_tool_uses_20250919">;
141
+ trigger: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
142
+ type: z.ZodLiteral<"input_tokens">;
143
+ value: z.ZodNumber;
144
+ }, z.core.$strip>, z.ZodObject<{
145
+ type: z.ZodLiteral<"tool_uses">;
146
+ value: z.ZodNumber;
147
+ }, z.core.$strip>]>>;
148
+ keep: z.ZodOptional<z.ZodObject<{
149
+ type: z.ZodLiteral<"tool_uses">;
150
+ value: z.ZodNumber;
151
+ }, z.core.$strip>>;
152
+ clearAtLeast: z.ZodOptional<z.ZodObject<{
153
+ type: z.ZodLiteral<"input_tokens">;
154
+ value: z.ZodNumber;
155
+ }, z.core.$strip>>;
156
+ clearToolInputs: z.ZodOptional<z.ZodBoolean>;
157
+ excludeTools: z.ZodOptional<z.ZodArray<z.ZodString>>;
158
+ }, z.core.$strip>, z.ZodObject<{
159
+ type: z.ZodLiteral<"clear_thinking_20251015">;
160
+ keep: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"all">, z.ZodObject<{
161
+ type: z.ZodLiteral<"thinking_turns">;
162
+ value: z.ZodNumber;
163
+ }, z.core.$strip>]>>;
164
+ }, z.core.$strip>]>>;
165
+ }, z.core.$strip>>;
86
166
  }, z.core.$strip>;
87
167
  type AnthropicProviderOptions = z.infer<typeof anthropicProviderOptions>;
88
168
 
package/dist/index.d.ts CHANGED
@@ -40,6 +40,58 @@ interface AnthropicMessageMetadata {
40
40
  version: string;
41
41
  }> | null;
42
42
  } | null;
43
+ /**
44
+ * Context management response.
45
+ *
46
+ * Information about context management strategies applied during the request.
47
+ */
48
+ contextManagement: {
49
+ /**
50
+ * List of context management edits that were applied.
51
+ * Each item in the array is a specific type of context management edit.
52
+ */
53
+ appliedEdits: Array<
54
+ /**
55
+ * Represents an edit where a certain number of tool uses and input tokens were cleared.
56
+ */
57
+ {
58
+ /**
59
+ * The type of context management edit applied.
60
+ * Possible value: 'clear_tool_uses_20250919'
61
+ */
62
+ type: 'clear_tool_uses_20250919';
63
+ /**
64
+ * Number of tool uses that were cleared by this edit.
65
+ * Minimum: 0
66
+ */
67
+ clearedToolUses: number;
68
+ /**
69
+ * Number of input tokens cleared by this edit.
70
+ * Minimum: 0
71
+ */
72
+ clearedInputTokens: number;
73
+ }
74
+ /**
75
+ * Represents an edit where a certain number of thinking turns and input tokens were cleared.
76
+ */
77
+ | {
78
+ /**
79
+ * The type of context management edit applied.
80
+ * Possible value: 'clear_thinking_20251015'
81
+ */
82
+ type: 'clear_thinking_20251015';
83
+ /**
84
+ * Number of thinking turns that were cleared by this edit.
85
+ * Minimum: 0
86
+ */
87
+ clearedThinkingTurns: number;
88
+ /**
89
+ * Number of input tokens cleared by this edit.
90
+ * Minimum: 0
91
+ */
92
+ clearedInputTokens: number;
93
+ }>;
94
+ } | null;
43
95
  }
44
96
 
45
97
  type AnthropicMessagesModelId = 'claude-3-5-haiku-20241022' | 'claude-3-5-haiku-latest' | 'claude-3-7-sonnet-20250219' | 'claude-3-7-sonnet-latest' | 'claude-3-haiku-20240307' | 'claude-haiku-4-5-20251001' | 'claude-haiku-4-5' | 'claude-opus-4-0' | 'claude-opus-4-1-20250805' | 'claude-opus-4-1' | 'claude-opus-4-20250514' | 'claude-opus-4-5' | 'claude-opus-4-5-20251101' | 'claude-sonnet-4-0' | 'claude-sonnet-4-20250514' | 'claude-sonnet-4-5-20250929' | 'claude-sonnet-4-5' | (string & {});
@@ -83,6 +135,34 @@ declare const anthropicProviderOptions: z.ZodObject<{
83
135
  medium: "medium";
84
136
  high: "high";
85
137
  }>>;
138
+ contextManagement: z.ZodOptional<z.ZodObject<{
139
+ edits: z.ZodArray<z.ZodDiscriminatedUnion<[z.ZodObject<{
140
+ type: z.ZodLiteral<"clear_tool_uses_20250919">;
141
+ trigger: z.ZodOptional<z.ZodDiscriminatedUnion<[z.ZodObject<{
142
+ type: z.ZodLiteral<"input_tokens">;
143
+ value: z.ZodNumber;
144
+ }, z.core.$strip>, z.ZodObject<{
145
+ type: z.ZodLiteral<"tool_uses">;
146
+ value: z.ZodNumber;
147
+ }, z.core.$strip>]>>;
148
+ keep: z.ZodOptional<z.ZodObject<{
149
+ type: z.ZodLiteral<"tool_uses">;
150
+ value: z.ZodNumber;
151
+ }, z.core.$strip>>;
152
+ clearAtLeast: z.ZodOptional<z.ZodObject<{
153
+ type: z.ZodLiteral<"input_tokens">;
154
+ value: z.ZodNumber;
155
+ }, z.core.$strip>>;
156
+ clearToolInputs: z.ZodOptional<z.ZodBoolean>;
157
+ excludeTools: z.ZodOptional<z.ZodArray<z.ZodString>>;
158
+ }, z.core.$strip>, z.ZodObject<{
159
+ type: z.ZodLiteral<"clear_thinking_20251015">;
160
+ keep: z.ZodOptional<z.ZodUnion<readonly [z.ZodLiteral<"all">, z.ZodObject<{
161
+ type: z.ZodLiteral<"thinking_turns">;
162
+ value: z.ZodNumber;
163
+ }, z.core.$strip>]>>;
164
+ }, z.core.$strip>]>>;
165
+ }, z.core.$strip>>;
86
166
  }, z.core.$strip>;
87
167
  type AnthropicProviderOptions = z.infer<typeof anthropicProviderOptions>;
88
168
 
package/dist/index.js CHANGED
@@ -31,7 +31,7 @@ var import_provider4 = require("@ai-sdk/provider");
31
31
  var import_provider_utils22 = require("@ai-sdk/provider-utils");
32
32
 
33
33
  // src/version.ts
34
- var VERSION = true ? "3.0.0-beta.76" : "0.0.0-test";
34
+ var VERSION = true ? "3.0.0-beta.78" : "0.0.0-test";
35
35
 
36
36
  // src/anthropic-messages-language-model.ts
37
37
  var import_provider3 = require("@ai-sdk/provider");
@@ -294,6 +294,22 @@ var anthropicMessagesResponseSchema = (0, import_provider_utils2.lazySchema)(
294
294
  version: import_v42.z.string()
295
295
  })
296
296
  ).nullish()
297
+ }).nullish(),
298
+ context_management: import_v42.z.object({
299
+ applied_edits: import_v42.z.array(
300
+ import_v42.z.union([
301
+ import_v42.z.object({
302
+ type: import_v42.z.literal("clear_tool_uses_20250919"),
303
+ cleared_tool_uses: import_v42.z.number(),
304
+ cleared_input_tokens: import_v42.z.number()
305
+ }),
306
+ import_v42.z.object({
307
+ type: import_v42.z.literal("clear_thinking_20251015"),
308
+ cleared_thinking_turns: import_v42.z.number(),
309
+ cleared_input_tokens: import_v42.z.number()
310
+ })
311
+ ])
312
+ )
297
313
  }).nullish()
298
314
  })
299
315
  )
@@ -576,6 +592,22 @@ var anthropicMessagesChunkSchema = (0, import_provider_utils2.lazySchema)(
576
592
  version: import_v42.z.string()
577
593
  })
578
594
  ).nullish()
595
+ }).nullish(),
596
+ context_management: import_v42.z.object({
597
+ applied_edits: import_v42.z.array(
598
+ import_v42.z.union([
599
+ import_v42.z.object({
600
+ type: import_v42.z.literal("clear_tool_uses_20250919"),
601
+ cleared_tool_uses: import_v42.z.number(),
602
+ cleared_input_tokens: import_v42.z.number()
603
+ }),
604
+ import_v42.z.object({
605
+ type: import_v42.z.literal("clear_thinking_20251015"),
606
+ cleared_thinking_turns: import_v42.z.number(),
607
+ cleared_input_tokens: import_v42.z.number()
608
+ })
609
+ ])
610
+ )
579
611
  }).nullish()
580
612
  }),
581
613
  usage: import_v42.z.looseObject({
@@ -706,7 +738,46 @@ var anthropicProviderOptions = import_v43.z.object({
706
738
  /**
707
739
  * @default 'high'
708
740
  */
709
- effort: import_v43.z.enum(["low", "medium", "high"]).optional()
741
+ effort: import_v43.z.enum(["low", "medium", "high"]).optional(),
742
+ contextManagement: import_v43.z.object({
743
+ edits: import_v43.z.array(
744
+ import_v43.z.discriminatedUnion("type", [
745
+ import_v43.z.object({
746
+ type: import_v43.z.literal("clear_tool_uses_20250919"),
747
+ trigger: import_v43.z.discriminatedUnion("type", [
748
+ import_v43.z.object({
749
+ type: import_v43.z.literal("input_tokens"),
750
+ value: import_v43.z.number()
751
+ }),
752
+ import_v43.z.object({
753
+ type: import_v43.z.literal("tool_uses"),
754
+ value: import_v43.z.number()
755
+ })
756
+ ]).optional(),
757
+ keep: import_v43.z.object({
758
+ type: import_v43.z.literal("tool_uses"),
759
+ value: import_v43.z.number()
760
+ }).optional(),
761
+ clearAtLeast: import_v43.z.object({
762
+ type: import_v43.z.literal("input_tokens"),
763
+ value: import_v43.z.number()
764
+ }).optional(),
765
+ clearToolInputs: import_v43.z.boolean().optional(),
766
+ excludeTools: import_v43.z.array(import_v43.z.string()).optional()
767
+ }),
768
+ import_v43.z.object({
769
+ type: import_v43.z.literal("clear_thinking_20251015"),
770
+ keep: import_v43.z.union([
771
+ import_v43.z.literal("all"),
772
+ import_v43.z.object({
773
+ type: import_v43.z.literal("thinking_turns"),
774
+ value: import_v43.z.number()
775
+ })
776
+ ]).optional()
777
+ })
778
+ ])
779
+ )
780
+ }).optional()
710
781
  });
711
782
 
712
783
  // src/anthropic-prepare-tools.ts
@@ -2008,6 +2079,8 @@ function mapAnthropicStopReason({
2008
2079
  return isJsonResponseFromTool ? "stop" : "tool-calls";
2009
2080
  case "max_tokens":
2010
2081
  return "length";
2082
+ case "model_context_window_exceeded":
2083
+ return "length";
2011
2084
  default:
2012
2085
  return "unknown";
2013
2086
  }
@@ -2131,6 +2204,7 @@ var AnthropicMessagesLanguageModel = class {
2131
2204
  description: "Respond with a JSON object.",
2132
2205
  inputSchema: responseFormat.schema
2133
2206
  } : void 0;
2207
+ const contextManagement = anthropicOptions == null ? void 0 : anthropicOptions.contextManagement;
2134
2208
  const cacheControlValidator = new CacheControlValidator();
2135
2209
  const toolNameMapping = (0, import_provider_utils12.createToolNameMapping)({
2136
2210
  tools,
@@ -2160,7 +2234,7 @@ var AnthropicMessagesLanguageModel = class {
2160
2234
  toolNameMapping
2161
2235
  });
2162
2236
  const isThinking = ((_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.type) === "enabled";
2163
- const thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2237
+ let thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
2164
2238
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
2165
2239
  const baseArgs = {
2166
2240
  // model id:
@@ -2211,13 +2285,57 @@ var AnthropicMessagesLanguageModel = class {
2211
2285
  },
2212
2286
  // prompt:
2213
2287
  system: messagesPrompt.system,
2214
- messages: messagesPrompt.messages
2288
+ messages: messagesPrompt.messages,
2289
+ ...contextManagement && {
2290
+ context_management: {
2291
+ edits: contextManagement.edits.map((edit) => {
2292
+ const strategy = edit.type;
2293
+ switch (strategy) {
2294
+ case "clear_tool_uses_20250919":
2295
+ return {
2296
+ type: edit.type,
2297
+ ...edit.trigger !== void 0 && {
2298
+ trigger: edit.trigger
2299
+ },
2300
+ ...edit.keep !== void 0 && { keep: edit.keep },
2301
+ ...edit.clearAtLeast !== void 0 && {
2302
+ clear_at_least: edit.clearAtLeast
2303
+ },
2304
+ ...edit.clearToolInputs !== void 0 && {
2305
+ clear_tool_inputs: edit.clearToolInputs
2306
+ },
2307
+ ...edit.excludeTools !== void 0 && {
2308
+ exclude_tools: edit.excludeTools
2309
+ }
2310
+ };
2311
+ case "clear_thinking_20251015":
2312
+ return {
2313
+ type: edit.type,
2314
+ ...edit.keep !== void 0 && { keep: edit.keep }
2315
+ };
2316
+ default:
2317
+ warnings.push({
2318
+ type: "other",
2319
+ message: `Unknown context management strategy: ${strategy}`
2320
+ });
2321
+ return void 0;
2322
+ }
2323
+ }).filter((edit) => edit !== void 0)
2324
+ }
2325
+ }
2215
2326
  };
2216
2327
  if (isThinking) {
2217
2328
  if (thinkingBudget == null) {
2218
- throw new import_provider3.UnsupportedFunctionalityError({
2219
- functionality: "thinking requires a budget"
2329
+ warnings.push({
2330
+ type: "compatibility",
2331
+ feature: "extended thinking",
2332
+ details: "thinking budget is required when thinking is enabled. using default budget of 1024 tokens."
2220
2333
  });
2334
+ baseArgs.thinking = {
2335
+ type: "enabled",
2336
+ budget_tokens: 1024
2337
+ };
2338
+ thinkingBudget = 1024;
2221
2339
  }
2222
2340
  if (baseArgs.temperature != null) {
2223
2341
  baseArgs.temperature = void 0;
@@ -2243,7 +2361,7 @@ var AnthropicMessagesLanguageModel = class {
2243
2361
  details: "topP is not supported when thinking is enabled"
2244
2362
  });
2245
2363
  }
2246
- baseArgs.max_tokens = maxTokens + thinkingBudget;
2364
+ baseArgs.max_tokens = maxTokens + (thinkingBudget != null ? thinkingBudget : 0);
2247
2365
  }
2248
2366
  if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2249
2367
  if (maxOutputTokens != null) {
@@ -2258,6 +2376,9 @@ var AnthropicMessagesLanguageModel = class {
2258
2376
  if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
2259
2377
  betas.add("mcp-client-2025-04-04");
2260
2378
  }
2379
+ if (contextManagement) {
2380
+ betas.add("context-management-2025-06-27");
2381
+ }
2261
2382
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2262
2383
  betas.add("code-execution-2025-08-25");
2263
2384
  betas.add("skills-2025-10-02");
@@ -2369,7 +2490,7 @@ var AnthropicMessagesLanguageModel = class {
2369
2490
  });
2370
2491
  }
2371
2492
  async doGenerate(options) {
2372
- var _a, _b, _c, _d, _e, _f, _g, _h;
2493
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2373
2494
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2374
2495
  ...options,
2375
2496
  stream: false,
@@ -2695,7 +2816,10 @@ var AnthropicMessagesLanguageModel = class {
2695
2816
  skillId: skill.skill_id,
2696
2817
  version: skill.version
2697
2818
  }))) != null ? _h : null
2698
- } : null
2819
+ } : null,
2820
+ contextManagement: (_i = mapAnthropicResponseContextManagement(
2821
+ response.context_management
2822
+ )) != null ? _i : null
2699
2823
  }
2700
2824
  }
2701
2825
  };
@@ -2734,6 +2858,7 @@ var AnthropicMessagesLanguageModel = class {
2734
2858
  };
2735
2859
  const contentBlocks = {};
2736
2860
  const mcpToolCalls = {};
2861
+ let contextManagement = null;
2737
2862
  let rawUsage = void 0;
2738
2863
  let cacheCreationInputTokens = null;
2739
2864
  let stopSequence = null;
@@ -3216,6 +3341,11 @@ var AnthropicMessagesLanguageModel = class {
3216
3341
  version: skill.version
3217
3342
  }))) != null ? _j : null
3218
3343
  } : null;
3344
+ if (value.delta.context_management) {
3345
+ contextManagement = mapAnthropicResponseContextManagement(
3346
+ value.delta.context_management
3347
+ );
3348
+ }
3219
3349
  rawUsage = {
3220
3350
  ...rawUsage,
3221
3351
  ...value.usage
@@ -3232,7 +3362,8 @@ var AnthropicMessagesLanguageModel = class {
3232
3362
  usage: rawUsage != null ? rawUsage : null,
3233
3363
  cacheCreationInputTokens,
3234
3364
  stopSequence,
3235
- container
3365
+ container,
3366
+ contextManagement
3236
3367
  }
3237
3368
  }
3238
3369
  });
@@ -3327,6 +3458,27 @@ function getModelCapabilities(modelId) {
3327
3458
  };
3328
3459
  }
3329
3460
  }
3461
+ function mapAnthropicResponseContextManagement(contextManagement) {
3462
+ return contextManagement ? {
3463
+ appliedEdits: contextManagement.applied_edits.map((edit) => {
3464
+ const strategy = edit.type;
3465
+ switch (strategy) {
3466
+ case "clear_tool_uses_20250919":
3467
+ return {
3468
+ type: edit.type,
3469
+ clearedToolUses: edit.cleared_tool_uses,
3470
+ clearedInputTokens: edit.cleared_input_tokens
3471
+ };
3472
+ case "clear_thinking_20251015":
3473
+ return {
3474
+ type: edit.type,
3475
+ clearedThinkingTurns: edit.cleared_thinking_turns,
3476
+ clearedInputTokens: edit.cleared_input_tokens
3477
+ };
3478
+ }
3479
+ }).filter((edit) => edit !== void 0)
3480
+ } : null;
3481
+ }
3330
3482
 
3331
3483
  // src/tool/bash_20241022.ts
3332
3484
  var import_provider_utils13 = require("@ai-sdk/provider-utils");