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

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,19 @@
1
1
  # @ai-sdk/anthropic
2
2
 
3
+ ## 3.0.0-beta.77
4
+
5
+ ### Patch Changes
6
+
7
+ - b2dbfbf: add context_management for anthropic
8
+
9
+ ## 3.0.0-beta.76
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [53f3368]
14
+ - @ai-sdk/provider@3.0.0-beta.25
15
+ - @ai-sdk/provider-utils@4.0.0-beta.44
16
+
3
17
  ## 3.0.0-beta.75
4
18
 
5
19
  ### 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.75" : "0.0.0-test";
34
+ var VERSION = true ? "3.0.0-beta.77" : "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,
@@ -2211,7 +2285,44 @@ 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) {
@@ -2258,6 +2369,9 @@ var AnthropicMessagesLanguageModel = class {
2258
2369
  if ((anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0) {
2259
2370
  betas.add("mcp-client-2025-04-04");
2260
2371
  }
2372
+ if (contextManagement) {
2373
+ betas.add("context-management-2025-06-27");
2374
+ }
2261
2375
  if ((anthropicOptions == null ? void 0 : anthropicOptions.container) && anthropicOptions.container.skills && anthropicOptions.container.skills.length > 0) {
2262
2376
  betas.add("code-execution-2025-08-25");
2263
2377
  betas.add("skills-2025-10-02");
@@ -2369,7 +2483,7 @@ var AnthropicMessagesLanguageModel = class {
2369
2483
  });
2370
2484
  }
2371
2485
  async doGenerate(options) {
2372
- var _a, _b, _c, _d, _e, _f, _g, _h;
2486
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2373
2487
  const { args, warnings, betas, usesJsonResponseTool, toolNameMapping } = await this.getArgs({
2374
2488
  ...options,
2375
2489
  stream: false,
@@ -2695,7 +2809,10 @@ var AnthropicMessagesLanguageModel = class {
2695
2809
  skillId: skill.skill_id,
2696
2810
  version: skill.version
2697
2811
  }))) != null ? _h : null
2698
- } : null
2812
+ } : null,
2813
+ contextManagement: (_i = mapAnthropicResponseContextManagement(
2814
+ response.context_management
2815
+ )) != null ? _i : null
2699
2816
  }
2700
2817
  }
2701
2818
  };
@@ -2734,6 +2851,7 @@ var AnthropicMessagesLanguageModel = class {
2734
2851
  };
2735
2852
  const contentBlocks = {};
2736
2853
  const mcpToolCalls = {};
2854
+ let contextManagement = null;
2737
2855
  let rawUsage = void 0;
2738
2856
  let cacheCreationInputTokens = null;
2739
2857
  let stopSequence = null;
@@ -3216,6 +3334,11 @@ var AnthropicMessagesLanguageModel = class {
3216
3334
  version: skill.version
3217
3335
  }))) != null ? _j : null
3218
3336
  } : null;
3337
+ if (value.delta.context_management) {
3338
+ contextManagement = mapAnthropicResponseContextManagement(
3339
+ value.delta.context_management
3340
+ );
3341
+ }
3219
3342
  rawUsage = {
3220
3343
  ...rawUsage,
3221
3344
  ...value.usage
@@ -3232,7 +3355,8 @@ var AnthropicMessagesLanguageModel = class {
3232
3355
  usage: rawUsage != null ? rawUsage : null,
3233
3356
  cacheCreationInputTokens,
3234
3357
  stopSequence,
3235
- container
3358
+ container,
3359
+ contextManagement
3236
3360
  }
3237
3361
  }
3238
3362
  });
@@ -3327,6 +3451,27 @@ function getModelCapabilities(modelId) {
3327
3451
  };
3328
3452
  }
3329
3453
  }
3454
+ function mapAnthropicResponseContextManagement(contextManagement) {
3455
+ return contextManagement ? {
3456
+ appliedEdits: contextManagement.applied_edits.map((edit) => {
3457
+ const strategy = edit.type;
3458
+ switch (strategy) {
3459
+ case "clear_tool_uses_20250919":
3460
+ return {
3461
+ type: edit.type,
3462
+ clearedToolUses: edit.cleared_tool_uses,
3463
+ clearedInputTokens: edit.cleared_input_tokens
3464
+ };
3465
+ case "clear_thinking_20251015":
3466
+ return {
3467
+ type: edit.type,
3468
+ clearedThinkingTurns: edit.cleared_thinking_turns,
3469
+ clearedInputTokens: edit.cleared_input_tokens
3470
+ };
3471
+ }
3472
+ }).filter((edit) => edit !== void 0)
3473
+ } : null;
3474
+ }
3330
3475
 
3331
3476
  // src/tool/bash_20241022.ts
3332
3477
  var import_provider_utils13 = require("@ai-sdk/provider-utils");