The code execution tool allows Claude to execute Python code in a secure, sandboxed environment. Claude can analyze data, create visualizations, perform complex calculations, and process uploaded files directly within the API conversation.
Supported modelsThe code execution tool is available on:
claude-opus-4-1-20250805
)claude-opus-4-20250514
)claude-sonnet-4-20250514
)claude-3-7-sonnet-20250219
)claude-3-5-haiku-latest
)Here’s a simple example that asks Claude to perform a calculation:
How code execution worksWhen you add the code execution tool to your API request:
The code execution tool requires no additional parameters:
Response formatHere’s an example response with code execution:
ResultsCode execution results include:
stdout
: Output from print statements and successful executionstderr
: Error messages if code execution failsreturn_code
(0 for success, non-zero for failure)If there is an error using the tool there will be a code_execution_tool_result_error
Possible errors include
unavailable
: The code execution tool is unavailablecode_execution_exceeded
: Execution time exceeded the maximum allowedcontainer_expired
: The container is expired and not availablepause_turn
stop reason
The response may include a pause_turn
stop reason, which indicates that the API paused a long-running turn. You may provide the response back as-is in a subsequent request to let Claude continue its turn, or modify the content if you wish to interrupt the conversation.
Code execution can analyze files uploaded via the Files API, such as CSV files, Excel files, and other data formats. This allows Claude to read, process, and generate insights from your data. You can pass multiple files per request.
Supported file typesThe Python environment is capable of working with but not limited to the following file types
container_upload
content blockWhen Claude creates files during code execution (e.g., saving matplotlib plots, generating CSVs), you can retrieve these files using the Files API:
ContainersThe code execution tool runs in a secure, containerized environment designed specifically for Python code execution.
Runtime environmentThe sandboxed Python environment includes these commonly used libraries:
You can reuse an existing container across multiple API requests by providing the container ID from a previous response. This allows you to maintain created files between requests.
Example StreamingWith streaming enabled, you’ll receive code execution events as they occur:
Batch requestsYou can include the code execution tool in the Messages Batches API. Code execution tool calls through the Messages Batches API are priced the same as those in regular Messages API requests.
Usage and pricingThe code execution tool usage is tracked separately from token usage. Execution time is a minimum of 5 minutes. If files are included in the request, execution time is billed even if the tool is not used due to files being preloaded onto the container.
Pricing: $0.05 per session-hour.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4