Skip to content

Commit 2164cdf

Browse files
feat(anthropic): add the new code_execution tool (#12900)
## Background #12794 anthropic silently released a new code execution tool (which is suggested for programmatic tool calling) ref here: https://platform.claude.com/docs/en/agents-and-tools/tool-use/programmatic-tool-calling ## Summary adds the new tool to the registry, updates the api schema, makes sure the the conversion from prompts to responses is correctly mapped ## Manual Verification verified by running the examples - `examples/ai-functions/src/generate-text/anthropic/code-execution-20260120.ts` - `examples/ai-functions/src/stream-text/anthropic/code-execution-20260120.ts` - `examples/ai-functions/src/generate-text/anthropic/programmatic-tool-calling.ts` - `http://localhost:3000/chat/anthropic-programmatic-tool-calling` ## Checklist - [x] Tests have been added / updated (for bug fixes / features) - [x] Documentation has been added / updated (for bug fixes / features) - [x] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review) ## Related Issues fixe #12794
1 parent 8acb431 commit 2164cdf

18 files changed

+2107
-20
lines changed

.changeset/five-spiders-develop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/anthropic': patch
3+
---
4+
5+
feat(anthropic): add the new code_execution tool

content/providers/01-ai-sdk-providers/05-anthropic.mdx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ You can enable code execution using the provider-defined code execution tool:
10361036
import { anthropic } from '@ai-sdk/anthropic';
10371037
import { generateText } from 'ai';
10381038

1039-
const codeExecutionTool = anthropic.tools.codeExecution_20250825();
1039+
const codeExecutionTool = anthropic.tools.codeExecution_20260120();
10401040

10411041
const result = await generateText({
10421042
model: anthropic('claude-opus-4-20250514'),
@@ -1049,9 +1049,10 @@ const result = await generateText({
10491049
```
10501050

10511051
<Note>
1052-
Two versions are available: `codeExecution_20250825` (recommended, supports
1053-
Python and Bash with enhanced file operations) and `codeExecution_20250522`
1054-
(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).
10551056
</Note>
10561057

10571058
#### Error Handling
@@ -1122,7 +1123,7 @@ const result = await generateText({
11221123
prompt:
11231124
'Get the weather for Tokyo, Sydney, and London, then calculate the average temperature.',
11241125
tools: {
1125-
code_execution: anthropic.tools.codeExecution_20250825(),
1126+
code_execution: anthropic.tools.codeExecution_20260120(),
11261127

11271128
getWeather: tool({
11281129
description: 'Get current weather data for a city.',
@@ -1136,7 +1137,7 @@ const result = await generateText({
11361137
// Enable this tool to be called from within code execution
11371138
providerOptions: {
11381139
anthropic: {
1139-
allowedCallers: ['code_execution_20250825'],
1140+
allowedCallers: ['code_execution_20260120'],
11401141
},
11411142
},
11421143
}),
@@ -1156,7 +1157,7 @@ In this flow:
11561157
<Note>
11571158
Programmatic tool calling requires `claude-sonnet-4-6`, `claude-sonnet-4-5`,
11581159
`claude-opus-4-6`, or `claude-opus-4-5` models and uses the
1159-
`code_execution_20250825` tool.
1160+
`code_execution_20260120` or `code_execution_20250825` tool.
11601161
</Note>
11611162

11621163
#### Container Persistence
@@ -1188,7 +1189,7 @@ import { generateText } from 'ai';
11881189
const result = await generateText({
11891190
model: anthropic('claude-sonnet-4-5'),
11901191
tools: {
1191-
code_execution: anthropic.tools.codeExecution_20250825(),
1192+
code_execution: anthropic.tools.codeExecution_20260120(),
11921193
},
11931194
prompt: 'Create a presentation about renewable energy with 5 slides',
11941195
providerOptions: {
@@ -1215,7 +1216,7 @@ You can also use custom skills by specifying `type: 'custom'`:
12151216
const result = await generateText({
12161217
model: anthropic('claude-sonnet-4-5'),
12171218
tools: {
1218-
code_execution: anthropic.tools.codeExecution_20250825(),
1219+
code_execution: anthropic.tools.codeExecution_20260120(),
12191220
},
12201221
prompt: 'Use my custom skill to process this data',
12211222
providerOptions: {

examples/ai-e2e-next/agent/anthropic/programmatic-tool-calling-agent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { InferAgentUIMessage, ToolLoopAgent } from 'ai';
88
import { z } from 'zod';
99

1010
export const anthropicProgrammaticToolCallingAgent = new ToolLoopAgent({
11-
model: anthropic('claude-sonnet-4-5'),
11+
model: anthropic('claude-opus-4-6'),
1212
callOptionsSchema: z.object({
1313
containerId: z.string().optional(),
1414
}),
1515
tools: {
16-
code_execution: anthropic.tools.codeExecution_20250825(),
16+
code_execution: anthropic.tools.codeExecution_20260120(),
1717
rollDie: rollDieToolWithProgrammaticCalling,
1818
},
1919

examples/ai-e2e-next/tool/roll-die-tool-with-programmatic-calling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const rollDieToolWithProgrammaticCalling = tool({
88
}),
99
providerOptions: {
1010
anthropic: {
11-
allowedCallers: ['code_execution_20250825'],
11+
allowedCallers: ['code_execution_20260120'],
1212
},
1313
},
1414
execute: async ({ player }) => {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { anthropic } from '@ai-sdk/anthropic';
2+
import { generateText } from 'ai';
3+
import { run } from '../../lib/run';
4+
5+
run(async () => {
6+
const result = await generateText({
7+
model: anthropic('claude-sonnet-4-5'),
8+
prompt:
9+
'Write a Python script to calculate fibonacci number' +
10+
' and then execute it to find the 10th fibonacci number',
11+
tools: {
12+
code_execution: anthropic.tools.codeExecution_20260120(),
13+
},
14+
});
15+
16+
console.dir(result.content, { depth: Infinity });
17+
});

examples/ai-functions/src/generate-text/anthropic/programmatic-tool-calling.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ run(async () => {
2222
'However, one player is cheating by using a loaded die. ' +
2323
'Use the rollDie tool to determine the outcome of each roll.',
2424
tools: {
25-
code_execution: anthropic.tools.codeExecution_20250825(),
25+
code_execution: anthropic.tools.codeExecution_20260120(),
2626

2727
rollDie: tool({
2828
description: 'Roll a die and return the result.',
@@ -45,7 +45,7 @@ run(async () => {
4545
},
4646
providerOptions: {
4747
anthropic: {
48-
allowedCallers: ['code_execution_20250825'],
48+
allowedCallers: ['code_execution_20260120'],
4949
},
5050
},
5151
}),
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { anthropic } from '@ai-sdk/anthropic';
2+
import { streamText } from 'ai';
3+
import { run } from '../../lib/run';
4+
5+
run(async () => {
6+
const result = streamText({
7+
model: anthropic('claude-sonnet-4-5'),
8+
prompt:
9+
'Write a Python script to calculate fibonacci number' +
10+
' and then execute it to find the 10th fibonacci number',
11+
tools: {
12+
code_execution: anthropic.tools.codeExecution_20260120(),
13+
},
14+
});
15+
16+
for await (const part of result.fullStream) {
17+
switch (part.type) {
18+
case 'text-delta': {
19+
process.stdout.write(part.text);
20+
break;
21+
}
22+
23+
case 'tool-call': {
24+
process.stdout.write(
25+
`\n\nTool call: '${part.toolName}'\nInput: ${JSON.stringify(part.input, null, 2)}\n`,
26+
);
27+
break;
28+
}
29+
30+
case 'tool-result': {
31+
process.stdout.write(
32+
`\nTool result: '${part.toolName}'\nOutput: ${JSON.stringify(part.output, null, 2)}\n`,
33+
);
34+
break;
35+
}
36+
37+
case 'error': {
38+
console.error('\n\nCode execution error:', part.error);
39+
break;
40+
}
41+
}
42+
}
43+
44+
process.stdout.write('\n\n');
45+
});

0 commit comments

Comments
 (0)