Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5665543
feat(anthropic): update code execution tool to version 20250825 with …
ellispinsky Oct 12, 2025
17cb21b
fix(anthropic): update version from minor to patch for code execution…
ellispinsky Oct 13, 2025
081a508
Merge branch 'main' into feat/anthropic-code-execution-20250825
lgrammel Oct 14, 2025
d83114b
revert
lgrammel Oct 14, 2025
8557448
docs
lgrammel Oct 14, 2025
ffcd948
docs
lgrammel Oct 14, 2025
c7e9414
fx
lgrammel Oct 14, 2025
5065992
tool spec1
lgrammel Oct 14, 2025
49f616a
spec2
lgrammel Oct 14, 2025
51a5af9
types1
lgrammel Oct 14, 2025
8c75b93
types2
lgrammel Oct 14, 2025
e067773
types3
lgrammel Oct 14, 2025
c37ef5d
types4
lgrammel Oct 14, 2025
c2d5f08
ex1
lgrammel Oct 14, 2025
8187dc9
test1
lgrammel Oct 14, 2025
61eb304
stream
lgrammel Oct 14, 2025
89675a4
refactor
lgrammel Oct 14, 2025
5d93678
ex2
lgrammel Oct 14, 2025
bd197cd
ex3
lgrammel Oct 14, 2025
1f36110
test2
lgrammel Oct 14, 2025
750cb6b
refactor
lgrammel Oct 14, 2025
d75166e
refactor
lgrammel Oct 14, 2025
18750e5
generate
lgrammel Oct 14, 2025
f0f07ed
fix
lgrammel Oct 14, 2025
e427ee4
clean
lgrammel Oct 14, 2025
72aef19
generate input
lgrammel Oct 14, 2025
b11fe09
input tool mapping
lgrammel Oct 14, 2025
3be1418
no mapping
lgrammel Oct 14, 2025
5ea0d0c
example4
lgrammel Oct 14, 2025
3561362
example
lgrammel Oct 14, 2025
e028810
Merge branch 'main' into feat/anthropic-code-execution-20250825
lgrammel Oct 14, 2025
7f94b32
clean
lgrammel Oct 14, 2025
6c9275f
extract
lgrammel Oct 14, 2025
50a5307
output mapping
lgrammel Oct 14, 2025
4b843bb
map inputs back
lgrammel Oct 14, 2025
722e642
types
lgrammel Oct 14, 2025
251a3b0
view
lgrammel Oct 14, 2025
442c0c5
view
lgrammel Oct 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nine-eggs-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/anthropic': minor
---

Add support for 2025-08-25 code execution tool
13 changes: 12 additions & 1 deletion content/providers/01-ai-sdk-providers/05-anthropic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,13 @@ You can enable code execution using the provider-defined code execution tool:
```ts
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const codeExecutionTool = anthropic.tools.codeExecution_20250522();

// Latest version with enhanced Bash support and file operations
const codeExecutionTool = anthropic.tools.codeExecution_20250825();

// Or use the previous version if needed
// const codeExecutionTool = anthropic.tools.codeExecution_20250522();

const result = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt:
Expand All @@ -558,6 +564,11 @@ const result = await generateText({
});
```

### Available Versions

- **`codeExecution_20250825()`** - Latest version with enhanced Bash command support and file operations (recommended)
- **`codeExecution_20250522()`** - Previous version

#### Error Handling

Code execution errors are handled differently depending on whether you're using streaming or non-streaming:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { anthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
import 'dotenv/config';

async function main() {
// Using the latest code execution tool with enhanced Bash and file operation support
const codeExecutionTool = anthropic.tools.codeExecution_20250825();

const result = await generateText({
model: anthropic('claude-opus-4-20250514'),
prompt:
'Calculate the mean and standard deviation of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] using Python',
tools: {
code_execution: codeExecutionTool,
},
});

console.log('Result:', result.text);
console.log('\nTool Calls:', JSON.stringify(result.toolCalls, null, 2));
console.log('\nTool Results:', JSON.stringify(result.toolResults, null, 2));
}

main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { anthropic } from '@ai-sdk/anthropic';
import { streamText } from 'ai';
import 'dotenv/config';

async function main() {
// Using the latest code execution tool with enhanced Bash and file operation support
const codeExecutionTool = anthropic.tools.codeExecution_20250825();

const result = streamText({
model: anthropic('claude-opus-4-20250514'),
prompt:
'Write a Python script to calculate fibonacci numbers and then execute it to find the 10th fibonacci number',
tools: {
code_execution: codeExecutionTool,
},
});

process.stdout.write('\nAssistant: ');

for await (const part of result.fullStream) {
switch (part.type) {
case 'text-delta': {
process.stdout.write(part.text);
break;
}

case 'tool-call': {
process.stdout.write(
`\n\nTool call: '${part.toolName}'\nInput: ${JSON.stringify(part.input, null, 2)}\n`,
);
break;
}

case 'tool-result': {
process.stdout.write(
`\nTool result: '${part.toolName}'\nOutput: ${JSON.stringify(part.output, null, 2)}\n`,
);
break;
}

case 'error': {
console.error('\n\nCode execution error:', part.error);
break;
}
}
}

process.stdout.write('\n\n');
console.log('Finish reason:', await result.finishReason);
console.log('Usage:', await result.usage);
}

main().catch(console.error);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"build:packages": "turbo build --filter=@ai-sdk/* --filter=ai",
"changeset": "changeset",
"clean": "turbo clean",
"dev": "turbo dev --no-cache --concurrency 16 --continue",
"dev": "turbo dev --no-cache --concurrency 20 --continue",
"lint": "turbo lint",
"prepare": "husky install",
"update-references": "update-ts-references",
Expand Down
65 changes: 65 additions & 0 deletions packages/anthropic/src/anthropic-messages-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,29 @@ export interface AnthropicCodeExecutionToolResultContent {
cache_control: AnthropicCacheControl | undefined;
}

export interface AnthropicTextEditorCodeExecutionToolResultContent {
type: 'text_editor_code_execution_tool_result';
tool_use_id: string;
content: {
type: 'text_editor_code_execution_create_result';
is_file_update: boolean;
};
cache_control: AnthropicCacheControl | undefined;
}

export interface AnthropicBashCodeExecutionToolResultContent {
type: 'bash_code_execution_tool_result';
tool_use_id: string;
content: {
type: 'bash_code_execution_result';
stdout: string;
stderr: string;
return_code: number;
content: unknown[];
};
cache_control: AnthropicCacheControl | undefined;
}

export interface AnthropicWebFetchToolResultContent {
type: 'web_fetch_tool_result';
tool_use_id: string;
Expand Down Expand Up @@ -170,6 +193,10 @@ export type AnthropicTool =
type: 'code_execution_20250522';
name: string;
}
| {
type: 'code_execution_20250825';
name: string;
}
| {
name: string;
type: 'computer_20250124' | 'computer_20241022';
Expand Down Expand Up @@ -345,6 +372,25 @@ export const anthropicMessagesResponseSchema = lazySchema(() =>
}),
]),
}),
z.object({
type: z.literal('text_editor_code_execution_tool_result'),
tool_use_id: z.string(),
content: z.object({
type: z.literal('text_editor_code_execution_create_result'),
is_file_update: z.boolean(),
}),
}),
z.object({
type: z.literal('bash_code_execution_tool_result'),
tool_use_id: z.string(),
content: z.object({
type: z.literal('bash_code_execution_result'),
stdout: z.string(),
stderr: z.string(),
return_code: z.number(),
content: z.array(z.unknown()),
}),
}),
]),
),
stop_reason: z.string().nullish(),
Expand Down Expand Up @@ -463,6 +509,25 @@ export const anthropicMessagesChunkSchema = lazySchema(() =>
}),
]),
}),
z.object({
type: z.literal('text_editor_code_execution_tool_result'),
tool_use_id: z.string(),
content: z.object({
type: z.literal('text_editor_code_execution_create_result'),
is_file_update: z.boolean(),
}),
}),
z.object({
type: z.literal('bash_code_execution_tool_result'),
tool_use_id: z.string(),
content: z.object({
type: z.literal('bash_code_execution_result'),
stdout: z.string(),
stderr: z.string(),
return_code: z.number(),
content: z.array(z.unknown()),
}),
}),
]),
}),
z.object({
Expand Down
71 changes: 69 additions & 2 deletions packages/anthropic/src/anthropic-messages-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
if (
part.name === 'web_search' ||
part.name === 'code_execution' ||
part.name === 'web_fetch'
part.name === 'web_fetch' ||
part.name === 'text_editor_code_execution' ||
part.name === 'bash_code_execution'
) {
content.push({
type: 'tool-call',
Expand Down Expand Up @@ -589,6 +591,34 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
}
break;
}
case 'text_editor_code_execution_tool_result': {
content.push({
type: 'tool-result',
toolCallId: part.tool_use_id,
toolName: 'text_editor_code_execution',
result: {
type: part.content.type,
is_file_update: part.content.is_file_update,
},
providerExecuted: true,
});
break;
}
case 'bash_code_execution_tool_result': {
content.push({
type: 'tool-result',
toolCallId: part.tool_use_id,
toolName: 'bash_code_execution',
result: {
type: part.content.type,
stdout: part.content.stdout,
stderr: part.content.stderr,
return_code: part.content.return_code,
},
providerExecuted: true,
});
break;
}
}
}

Expand Down Expand Up @@ -678,6 +708,8 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
| 'web_fetch_tool_result'
| 'web_search_tool_result'
| 'code_execution_tool_result'
| 'text_editor_code_execution_tool_result'
| 'bash_code_execution_tool_result'
| undefined = undefined;

const generateId = this.generateId;
Expand Down Expand Up @@ -773,7 +805,10 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
if (
value.content_block.name === 'web_fetch' ||
value.content_block.name === 'web_search' ||
value.content_block.name === 'code_execution'
value.content_block.name === 'code_execution' ||
value.content_block.name ===
'text_editor_code_execution' ||
value.content_block.name === 'bash_code_execution'
) {
contentBlocks[value.index] = {
type: 'tool-call',
Expand Down Expand Up @@ -919,6 +954,38 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
return;
}

case 'text_editor_code_execution_tool_result': {
const part = value.content_block;
controller.enqueue({
type: 'tool-result',
toolCallId: part.tool_use_id,
toolName: 'text_editor_code_execution',
result: {
type: part.content.type,
is_file_update: part.content.is_file_update,
},
providerExecuted: true,
});
return;
}

case 'bash_code_execution_tool_result': {
const part = value.content_block;
controller.enqueue({
type: 'tool-result',
toolCallId: part.tool_use_id,
toolName: 'bash_code_execution',
result: {
type: part.content.type,
stdout: part.content.stdout,
stderr: part.content.stderr,
return_code: part.content.return_code,
},
providerExecuted: true,
});
return;
}

default: {
const _exhaustiveCheck: never = contentBlockType;
throw new Error(
Expand Down
8 changes: 8 additions & 0 deletions packages/anthropic/src/anthropic-prepare-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export async function prepareTools({
});
break;
}
case 'anthropic.code_execution_20250825': {
betas.add('code-execution-2025-08-25');
anthropicTools.push({
type: 'code_execution_20250825',
name: 'code_execution',
});
break;
}
case 'anthropic.computer_20250124': {
betas.add('computer-use-2025-01-24');
anthropicTools.push({
Expand Down
15 changes: 15 additions & 0 deletions packages/anthropic/src/anthropic-tools.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { bash_20241022 } from './tool/bash_20241022';
import { bash_20250124 } from './tool/bash_20250124';
import { codeExecution_20250522 } from './tool/code-execution_20250522';
import { codeExecution_20250825 } from './tool/code-execution_20250825';
import { computer_20241022 } from './tool/computer_20241022';
import { computer_20250124 } from './tool/computer_20250124';
import { textEditor_20241022 } from './tool/text-editor_20241022';
Expand Down Expand Up @@ -43,6 +44,20 @@ export const anthropicTools = {
*/
codeExecution_20250522,

/**
* Claude can analyze data, create visualizations, perform complex calculations,
* run system commands, create and edit files, and process uploaded files directly within
* the API conversation.
*
* The code execution tool allows Claude to run both Python and Bash commands and manipulate files,
* including writing code, in a secure, sandboxed environment.
*
* This is the latest version with enhanced Bash support and file operations.
*
* Tool name must be `code_execution`.
*/
codeExecution_20250825,

/**
* Claude can interact with computer environments through the computer use tool, which
* provides screenshot capabilities and mouse/keyboard control for autonomous desktop interaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { anthropicFilePartProviderOptions } from './anthropic-messages-options';
import { getCacheControl } from './get-cache-control';
import { codeExecution_20250522OutputSchema } from './tool/code-execution_20250522';
import { codeExecution_20250825OutputSchema } from './tool/code-execution_20250825';
import { webFetch_20250910OutputSchema } from './tool/web-fetch-20250910';
import { webSearch_20250305OutputSchema } from './tool/web-search_20250305';

Expand Down
Loading
Loading