Code execution tool
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.
The code execution tool is currently in beta.
This feature requires the beta header: "anthropic-beta": "code-execution-2025-05-22"
Supported models
The code execution tool is available on:
- Claude Opus 4 (
claude-opus-4-20250514
) - Claude Sonnet 4 (
claude-sonnet-4-20250514
) - Claude Sonnet 3.7 (
claude-3-7-sonnet-20250219
) - Claude Haiku 3.5 (
claude-3-5-haiku-latest
)
Quick start
Here’s a simple example that asks Claude to perform a calculation:
How code execution works
When you add the code execution tool to your API request:
- Claude evaluates whether code execution would help answer your question
- Claude writes and executes Python code in a secure sandbox environment
- Code execution may occur multiple times throughout a single request
- Claude provides results with any generated charts, calculations, or analysis
Tool definition
The code execution tool requires no additional parameters:
Response format
Here’s an example response with code execution:
Results
Code 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)
Errors
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 available
pause_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.
Working with Files in Code Execution
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.
Using the Files API with Code Execution requires two beta headers: "anthropic-beta": "code-execution-2025-05-22,files-api-2025-04-14"
Supported file types
The Python environment is capable of working with but not limited to the following file types
- CSV
- Excel (.xlsx, .xls)
- JSON
- XML
- Images (JPEG, PNG, GIF, WebP)
- Text files (.txt, .md, .py, etc)
Loading files for code execution
- Upload your file using the Files API
- Reference the file in your message using a
container_upload
content block - Include the code execution tool in your API request
Retrieving files created by code execution
When Claude creates files during code execution (e.g., saving matplotlib plots, generating CSVs), you can retrieve these files using the Files API:
Containers
The code execution tool runs in a secure, containerized environment designed specifically for Python code execution.
Runtime environment
- Python version: 3.11.12
- Operating system: Linux-based container
- Architecture: x86_64 (AMD64)
Resource limits
- Memory: 1GiB RAM
- Disk space: 5GiB workspace storage
- CPU: 1 CPU
Networking and security
- Internet access: Completely disabled for security
- External connections: No outbound network requests permitted
- Sandbox isolation: Full isolation from host system and other containers
- File access: Limited to workspace directory only
- Expiration: Containers expire 1 hour after creation
Pre-installed libraries
The sandboxed Python environment includes these commonly used libraries:
- Data Science: pandas, numpy, scipy, scikit-learn, statsmodels
- Visualization: matplotlib
- File Processing: pyarrow, openpyxl, xlrd, pillow
- Math & Computing: sympy, mpmath
- Utilities: tqdm, python-dateutil, pytz, joblib
Container reuse
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
Streaming
With streaming enabled, you’ll receive code execution events as they occur:
Batch requests
You 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 pricing
The 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.