@ai-sdk/anthropic 3.0.46 → 3.0.48

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.
@@ -282,19 +282,25 @@ const result = await generateText({
282
282
  console.log(result.providerMetadata?.anthropic?.contextManagement);
283
283
  ```
284
284
 
285
- #### Clear Tool Uses
285
+ #### Context Editing
286
286
 
287
- The `clear_tool_uses_20250919` edit type removes old tool calls from the conversation history:
287
+ Context editing strategies selectively remove specific content types from earlier in the conversation to reduce token usage without losing the overall conversation flow.
288
288
 
289
- - **trigger** - Condition that triggers the clearing (e.g., `{ type: 'input_tokens', value: 10000 }`)
289
+ ##### Clear Tool Uses
290
+
291
+ The `clear_tool_uses_20250919` edit type removes old tool call/result pairs from the conversation history:
292
+
293
+ - **trigger** - Condition that triggers the clearing (e.g., `{ type: 'input_tokens', value: 10000 }` or `{ type: 'tool_uses', value: 10 }`)
290
294
  - **keep** - How many recent tool uses to preserve (e.g., `{ type: 'tool_uses', value: 5 }`)
291
295
  - **clearAtLeast** - Minimum amount to clear (e.g., `{ type: 'input_tokens', value: 1000 }`)
292
296
  - **clearToolInputs** - Whether to clear tool input parameters (boolean)
293
297
  - **excludeTools** - Array of tool names to never clear
294
298
 
295
- #### Clear Thinking
299
+ ##### Clear Thinking
296
300
 
297
- The `clear_thinking_20251015` edit type removes thinking/reasoning content:
301
+ The `clear_thinking_20251015` edit type removes thinking/reasoning blocks from earlier turns, keeping only the most recent ones:
302
+
303
+ - **keep** - How many recent thinking turns to preserve (e.g., `{ type: 'thinking_turns', value: 2 }`) or `'all'` to keep everything
298
304
 
299
305
  ```ts
300
306
  const result = await generateText({
@@ -425,7 +431,6 @@ if (metadata?.appliedEdits) {
425
431
  console.log(`Freed ${edit.clearedInputTokens} tokens`);
426
432
  } else if (edit.type === 'compact_20260112') {
427
433
  console.log('Compaction was applied');
428
- console.log(`Freed ${edit.clearedInputTokens} tokens`);
429
434
  }
430
435
  });
431
436
  }
@@ -1031,7 +1036,7 @@ You can enable code execution using the provider-defined code execution tool:
1031
1036
  import { anthropic } from '@ai-sdk/anthropic';
1032
1037
  import { generateText } from 'ai';
1033
1038
 
1034
- const codeExecutionTool = anthropic.tools.codeExecution_20250825();
1039
+ const codeExecutionTool = anthropic.tools.codeExecution_20260120();
1035
1040
 
1036
1041
  const result = await generateText({
1037
1042
  model: anthropic('claude-opus-4-20250514'),
@@ -1044,9 +1049,10 @@ const result = await generateText({
1044
1049
  ```
1045
1050
 
1046
1051
  <Note>
1047
- Two versions are available: `codeExecution_20250825` (recommended, supports
1048
- Python and Bash with enhanced file operations) and `codeExecution_20250522`
1049
- (supports Bash only).
1052
+ Three versions are available: `codeExecution_20260120` (recommended, does not
1053
+ require a beta header, supports Claude Opus 4.6, Sonnet 4.6, Sonnet 4.5, and
1054
+ Opus 4.5), `codeExecution_20250825` (supports Python and Bash with enhanced
1055
+ file operations), and `codeExecution_20250522` (supports Bash only).
1050
1056
  </Note>
1051
1057
 
1052
1058
  #### Error Handling
@@ -1117,7 +1123,7 @@ const result = await generateText({
1117
1123
  prompt:
1118
1124
  'Get the weather for Tokyo, Sydney, and London, then calculate the average temperature.',
1119
1125
  tools: {
1120
- code_execution: anthropic.tools.codeExecution_20250825(),
1126
+ code_execution: anthropic.tools.codeExecution_20260120(),
1121
1127
 
1122
1128
  getWeather: tool({
1123
1129
  description: 'Get current weather data for a city.',
@@ -1131,7 +1137,7 @@ const result = await generateText({
1131
1137
  // Enable this tool to be called from within code execution
1132
1138
  providerOptions: {
1133
1139
  anthropic: {
1134
- allowedCallers: ['code_execution_20250825'],
1140
+ allowedCallers: ['code_execution_20260120'],
1135
1141
  },
1136
1142
  },
1137
1143
  }),
@@ -1151,7 +1157,7 @@ In this flow:
1151
1157
  <Note>
1152
1158
  Programmatic tool calling requires `claude-sonnet-4-6`, `claude-sonnet-4-5`,
1153
1159
  `claude-opus-4-6`, or `claude-opus-4-5` models and uses the
1154
- `code_execution_20250825` tool.
1160
+ `code_execution_20260120` or `code_execution_20250825` tool.
1155
1161
  </Note>
1156
1162
 
1157
1163
  #### Container Persistence
@@ -1183,7 +1189,7 @@ import { generateText } from 'ai';
1183
1189
  const result = await generateText({
1184
1190
  model: anthropic('claude-sonnet-4-5'),
1185
1191
  tools: {
1186
- code_execution: anthropic.tools.codeExecution_20250825(),
1192
+ code_execution: anthropic.tools.codeExecution_20260120(),
1187
1193
  },
1188
1194
  prompt: 'Create a presentation about renewable energy with 5 slides',
1189
1195
  providerOptions: {
@@ -1210,7 +1216,7 @@ You can also use custom skills by specifying `type: 'custom'`:
1210
1216
  const result = await generateText({
1211
1217
  model: anthropic('claude-sonnet-4-5'),
1212
1218
  tools: {
1213
- code_execution: anthropic.tools.codeExecution_20250825(),
1219
+ code_execution: anthropic.tools.codeExecution_20260120(),
1214
1220
  },
1215
1221
  prompt: 'Use my custom skill to process this data',
1216
1222
  providerOptions: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "3.0.46",
3
+ "version": "3.0.48",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -113,6 +113,10 @@ export type AnthropicToolCallCaller =
113
113
  type: 'code_execution_20250825';
114
114
  tool_id: string;
115
115
  }
116
+ | {
117
+ type: 'code_execution_20260120';
118
+ tool_id: string;
119
+ }
116
120
  | {
117
121
  type: 'direct';
118
122
  };
@@ -354,7 +358,9 @@ export type AnthropicTool =
354
358
  *
355
359
  * @example ['code_execution_20250825']
356
360
  */
357
- allowed_callers?: Array<'code_execution_20250825'>;
361
+ allowed_callers?: Array<
362
+ 'direct' | 'code_execution_20250825' | 'code_execution_20260120'
363
+ >;
358
364
  }
359
365
  | {
360
366
  type: 'code_execution_20250522';
@@ -365,6 +371,10 @@ export type AnthropicTool =
365
371
  type: 'code_execution_20250825';
366
372
  name: string;
367
373
  }
374
+ | {
375
+ type: 'code_execution_20260120';
376
+ name: string;
377
+ }
368
378
  | {
369
379
  name: string;
370
380
  type: 'computer_20250124' | 'computer_20241022';
@@ -597,6 +607,10 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
597
607
  type: z.literal('code_execution_20250825'),
598
608
  tool_id: z.string(),
599
609
  }),
610
+ z.object({
611
+ type: z.literal('code_execution_20260120'),
612
+ tool_id: z.string(),
613
+ }),
600
614
  z.object({
601
615
  type: z.literal('direct'),
602
616
  }),
@@ -870,6 +884,10 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
870
884
  type: z.literal('code_execution_20250825'),
871
885
  tool_id: z.string(),
872
886
  }),
887
+ z.object({
888
+ type: z.literal('code_execution_20260120'),
889
+ tool_id: z.string(),
890
+ }),
873
891
  z.object({
874
892
  type: z.literal('direct'),
875
893
  }),
@@ -913,6 +931,10 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
913
931
  type: z.literal('code_execution_20250825'),
914
932
  tool_id: z.string(),
915
933
  }),
934
+ z.object({
935
+ type: z.literal('code_execution_20260120'),
936
+ tool_id: z.string(),
937
+ }),
916
938
  z.object({
917
939
  type: z.literal('direct'),
918
940
  }),
@@ -294,6 +294,7 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
294
294
  providerToolNames: {
295
295
  'anthropic.code_execution_20250522': 'code_execution',
296
296
  'anthropic.code_execution_20250825': 'code_execution',
297
+ 'anthropic.code_execution_20260120': 'code_execution',
297
298
  'anthropic.computer_20241022': 'computer',
298
299
  'anthropic.computer_20250124': 'computer',
299
300
  'anthropic.text_editor_20241022': 'str_replace_editor',
@@ -353,6 +354,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
353
354
  ...(anthropicOptions?.speed && {
354
355
  speed: anthropicOptions.speed,
355
356
  }),
357
+ ...(anthropicOptions?.cacheControl && {
358
+ cache_control: anthropicOptions.cacheControl,
359
+ }),
356
360
 
357
361
  // structured output:
358
362
  ...(useStructuredOutput &&
@@ -562,7 +566,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
562
566
  !tools?.some(
563
567
  tool =>
564
568
  tool.type === 'provider' &&
565
- tool.id === 'anthropic.code_execution_20250825',
569
+ (tool.id === 'anthropic.code_execution_20250825' ||
570
+ tool.id === 'anthropic.code_execution_20260120'),
566
571
  )
567
572
  ) {
568
573
  warnings.push({
@@ -1241,7 +1246,10 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1241
1246
  firstDelta: boolean;
1242
1247
  providerToolName?: string;
1243
1248
  caller?: {
1244
- type: 'code_execution_20250825' | 'direct';
1249
+ type:
1250
+ | 'code_execution_20250825'
1251
+ | 'code_execution_20260120'
1252
+ | 'direct';
1245
1253
  toolId?: string;
1246
1254
  };
1247
1255
  }
@@ -12,7 +12,9 @@ import { validateTypes } from '@ai-sdk/provider-utils';
12
12
 
13
13
  export interface AnthropicToolOptions {
14
14
  deferLoading?: boolean;
15
- allowedCallers?: Array<'code_execution_20250825'>;
15
+ allowedCallers?: Array<
16
+ 'direct' | 'code_execution_20250825' | 'code_execution_20260120'
17
+ >;
16
18
  }
17
19
 
18
20
  export async function prepareTools({
@@ -119,6 +121,13 @@ export async function prepareTools({
119
121
  });
120
122
  break;
121
123
  }
124
+ case 'anthropic.code_execution_20260120': {
125
+ anthropicTools.push({
126
+ type: 'code_execution_20260120',
127
+ name: 'code_execution',
128
+ });
129
+ break;
130
+ }
122
131
  case 'anthropic.computer_20250124': {
123
132
  betas.add('computer-use-2025-01-24');
124
133
  anthropicTools.push({
@@ -2,6 +2,7 @@ import { bash_20241022 } from './tool/bash_20241022';
2
2
  import { bash_20250124 } from './tool/bash_20250124';
3
3
  import { codeExecution_20250522 } from './tool/code-execution_20250522';
4
4
  import { codeExecution_20250825 } from './tool/code-execution_20250825';
5
+ import { codeExecution_20260120 } from './tool/code-execution_20260120';
5
6
  import { computer_20241022 } from './tool/computer_20241022';
6
7
  import { computer_20250124 } from './tool/computer_20250124';
7
8
  import { computer_20251124 } from './tool/computer_20251124';
@@ -54,6 +55,20 @@ export const anthropicTools = {
54
55
  */
55
56
  codeExecution_20250825,
56
57
 
58
+ /**
59
+ * Claude can analyze data, create visualizations, perform complex calculations,
60
+ * run system commands, create and edit files, and process uploaded files directly within
61
+ * the API conversation.
62
+ *
63
+ * The code execution tool allows Claude to run both Python and Bash commands and manipulate files,
64
+ * including writing code, in a secure, sandboxed environment.
65
+ *
66
+ * This is the recommended version. Does not require a beta header.
67
+ *
68
+ * Supported models: Claude Opus 4.6, Sonnet 4.6, Sonnet 4.5, Opus 4.5
69
+ */
70
+ codeExecution_20260120,
71
+
57
72
  /**
58
73
  * Claude can interact with computer environments through the computer use tool, which
59
74
  * provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction.
@@ -648,10 +648,14 @@ export async function convertToAnthropicMessagesPrompt({
648
648
  | { caller?: { type: string; toolId?: string } }
649
649
  | undefined;
650
650
  const caller = callerOptions?.caller
651
- ? callerOptions.caller.type === 'code_execution_20250825' &&
651
+ ? (callerOptions.caller.type === 'code_execution_20250825' ||
652
+ callerOptions.caller.type ===
653
+ 'code_execution_20260120') &&
652
654
  callerOptions.caller.toolId
653
655
  ? {
654
- type: 'code_execution_20250825' as const,
656
+ type: callerOptions.caller.type as
657
+ | 'code_execution_20250825'
658
+ | 'code_execution_20260120',
655
659
  tool_id: callerOptions.caller.toolId,
656
660
  }
657
661
  : callerOptions.caller.type === 'direct'
@@ -0,0 +1,277 @@
1
+ import {
2
+ createProviderToolFactoryWithOutputSchema,
3
+ lazySchema,
4
+ zodSchema,
5
+ } from '@ai-sdk/provider-utils';
6
+ import { z } from 'zod/v4';
7
+
8
+ export const codeExecution_20260120OutputSchema = lazySchema(() =>
9
+ zodSchema(
10
+ z.discriminatedUnion('type', [
11
+ z.object({
12
+ type: z.literal('code_execution_result'),
13
+ stdout: z.string(),
14
+ stderr: z.string(),
15
+ return_code: z.number(),
16
+ content: z
17
+ .array(
18
+ z.object({
19
+ type: z.literal('code_execution_output'),
20
+ file_id: z.string(),
21
+ }),
22
+ )
23
+ .optional()
24
+ .default([]),
25
+ }),
26
+ z.object({
27
+ type: z.literal('bash_code_execution_result'),
28
+ content: z.array(
29
+ z.object({
30
+ type: z.literal('bash_code_execution_output'),
31
+ file_id: z.string(),
32
+ }),
33
+ ),
34
+ stdout: z.string(),
35
+ stderr: z.string(),
36
+ return_code: z.number(),
37
+ }),
38
+ z.object({
39
+ type: z.literal('bash_code_execution_tool_result_error'),
40
+ error_code: z.string(),
41
+ }),
42
+ z.object({
43
+ type: z.literal('text_editor_code_execution_tool_result_error'),
44
+ error_code: z.string(),
45
+ }),
46
+ z.object({
47
+ type: z.literal('text_editor_code_execution_view_result'),
48
+ content: z.string(),
49
+ file_type: z.string(),
50
+ num_lines: z.number().nullable(),
51
+ start_line: z.number().nullable(),
52
+ total_lines: z.number().nullable(),
53
+ }),
54
+ z.object({
55
+ type: z.literal('text_editor_code_execution_create_result'),
56
+ is_file_update: z.boolean(),
57
+ }),
58
+ z.object({
59
+ type: z.literal('text_editor_code_execution_str_replace_result'),
60
+ lines: z.array(z.string()).nullable(),
61
+ new_lines: z.number().nullable(),
62
+ new_start: z.number().nullable(),
63
+ old_lines: z.number().nullable(),
64
+ old_start: z.number().nullable(),
65
+ }),
66
+ ]),
67
+ ),
68
+ );
69
+
70
+ export const codeExecution_20260120InputSchema = lazySchema(() =>
71
+ zodSchema(
72
+ z.discriminatedUnion('type', [
73
+ z.object({
74
+ type: z.literal('programmatic-tool-call'),
75
+ code: z.string(),
76
+ }),
77
+ z.object({
78
+ type: z.literal('bash_code_execution'),
79
+ command: z.string(),
80
+ }),
81
+ z.discriminatedUnion('command', [
82
+ z.object({
83
+ type: z.literal('text_editor_code_execution'),
84
+ command: z.literal('view'),
85
+ path: z.string(),
86
+ }),
87
+ z.object({
88
+ type: z.literal('text_editor_code_execution'),
89
+ command: z.literal('create'),
90
+ path: z.string(),
91
+ file_text: z.string().nullish(),
92
+ }),
93
+ z.object({
94
+ type: z.literal('text_editor_code_execution'),
95
+ command: z.literal('str_replace'),
96
+ path: z.string(),
97
+ old_str: z.string(),
98
+ new_str: z.string(),
99
+ }),
100
+ ]),
101
+ ]),
102
+ ),
103
+ );
104
+
105
+ const factory = createProviderToolFactoryWithOutputSchema<
106
+ | {
107
+ type: 'programmatic-tool-call';
108
+ /**
109
+ * Programmatic tool calling: Python code to execute when code_execution
110
+ * is used with allowedCallers to trigger client-executed tools.
111
+ */
112
+ code: string;
113
+ }
114
+ | {
115
+ type: 'bash_code_execution';
116
+
117
+ /**
118
+ * Shell command to execute.
119
+ */
120
+ command: string;
121
+ }
122
+ | {
123
+ type: 'text_editor_code_execution';
124
+ command: 'view';
125
+
126
+ /**
127
+ * The path to the file to view.
128
+ */
129
+ path: string;
130
+ }
131
+ | {
132
+ type: 'text_editor_code_execution';
133
+ command: 'create';
134
+
135
+ /**
136
+ * The path to the file to edit.
137
+ */
138
+ path: string;
139
+
140
+ /**
141
+ * The text of the file to edit.
142
+ */
143
+ file_text?: string | null;
144
+ }
145
+ | {
146
+ type: 'text_editor_code_execution';
147
+ command: 'str_replace';
148
+
149
+ /**
150
+ * The path to the file to edit.
151
+ */
152
+ path: string;
153
+
154
+ /**
155
+ * The string to replace.
156
+ */
157
+ old_str: string;
158
+
159
+ /**
160
+ * The new string to replace the old string with.
161
+ */
162
+ new_str: string;
163
+ },
164
+ | {
165
+ /**
166
+ * Programmatic tool calling result: returned when code_execution runs code
167
+ * that calls client-executed tools via allowedCallers.
168
+ */
169
+ type: 'code_execution_result';
170
+
171
+ /**
172
+ * Output from successful execution
173
+ */
174
+ stdout: string;
175
+
176
+ /**
177
+ * Error messages if execution fails
178
+ */
179
+ stderr: string;
180
+
181
+ /**
182
+ * 0 for success, non-zero for failure
183
+ */
184
+ return_code: number;
185
+
186
+ /**
187
+ * Output file Id list
188
+ */
189
+ content: Array<{ type: 'code_execution_output'; file_id: string }>;
190
+ }
191
+ | {
192
+ type: 'bash_code_execution_result';
193
+
194
+ /**
195
+ * Output file Id list
196
+ */
197
+ content: Array<{
198
+ type: 'bash_code_execution_output';
199
+ file_id: string;
200
+ }>;
201
+
202
+ /**
203
+ * Output from successful execution
204
+ */
205
+ stdout: string;
206
+
207
+ /**
208
+ * Error messages if execution fails
209
+ */
210
+ stderr: string;
211
+
212
+ /**
213
+ * 0 for success, non-zero for failure
214
+ */
215
+ return_code: number;
216
+ }
217
+ | {
218
+ type: 'bash_code_execution_tool_result_error';
219
+
220
+ /**
221
+ * Available options: invalid_tool_input, unavailable, too_many_requests,
222
+ * execution_time_exceeded, output_file_too_large.
223
+ */
224
+ error_code: string;
225
+ }
226
+ | {
227
+ type: 'text_editor_code_execution_tool_result_error';
228
+
229
+ /**
230
+ * Available options: invalid_tool_input, unavailable, too_many_requests,
231
+ * execution_time_exceeded, file_not_found.
232
+ */
233
+ error_code: string;
234
+ }
235
+ | {
236
+ type: 'text_editor_code_execution_view_result';
237
+
238
+ content: string;
239
+
240
+ /**
241
+ * The type of the file. Available options: text, image, pdf.
242
+ */
243
+ file_type: string;
244
+
245
+ num_lines: number | null;
246
+ start_line: number | null;
247
+ total_lines: number | null;
248
+ }
249
+ | {
250
+ type: 'text_editor_code_execution_create_result';
251
+
252
+ is_file_update: boolean;
253
+ }
254
+ | {
255
+ type: 'text_editor_code_execution_str_replace_result';
256
+
257
+ lines: string[] | null;
258
+ new_lines: number | null;
259
+ new_start: number | null;
260
+ old_lines: number | null;
261
+ old_start: number | null;
262
+ },
263
+ {
264
+ // no arguments
265
+ }
266
+ >({
267
+ id: 'anthropic.code_execution_20260120',
268
+ inputSchema: codeExecution_20260120InputSchema,
269
+ outputSchema: codeExecution_20260120OutputSchema,
270
+ supportsDeferredResults: true,
271
+ });
272
+
273
+ export const codeExecution_20260120 = (
274
+ args: Parameters<typeof factory>[0] = {},
275
+ ) => {
276
+ return factory(args);
277
+ };