Code execution tool
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 Bash commands and manipulate files, including writing code, in a secure, sandboxed environment.
The code execution tool is currently in public beta.To use this feature, add the
"code-execution-2025-08-25" beta header to your API requests.Model compatibility
The code execution tool is available on the following models:The current version
code_execution_20250825 supports Bash commands and file operations. A legacy version code_execution_20250522 (Python only) is also available. See Upgrade to latest tool version for migration details.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
- The tool automatically provides Claude with the following capabilities:
- Bash commands: Execute shell commands for system operations and package management
- File operations: Create, view, and edit files directly, including writing code
- Claude can use any combination of these capabilities in a single request
- All operations run in a secure sandbox environment
- Claude provides results with any generated charts, calculations, or analysis
How to use the tool
Execute Bash commands
Ask Claude to check system information and install packages:Create and edit files directly
Claude can create, view, and edit files directly in the sandbox using the file manipulation capabilities:Upload and analyze your own files
To analyze your own data files (CSV, Excel, images, etc.), upload them via the Files API and reference them in your request:Using the Files API with Code Execution requires two beta headers:
"anthropic-beta": "code-execution-2025-08-25,files-api-2025-04-14"- CSV
- Excel (.xlsx, .xls)
- JSON
- XML
- Images (JPEG, PNG, GIF, WebP)
- Text files (.txt, .md, .py, etc)
Upload and analyze files
- Upload your file using the Files API
- Reference the file in your message using a
container_uploadcontent block - Include the code execution tool in your API request
Retrieve generated files
When Claude creates files during code execution, you can retrieve these files using the Files API:Combine operations
A complex workflow using all capabilities:Tool definition
The code execution tool requires no additional parameters:JSON
bash_code_execution: Run shell commandstext_editor_code_execution: View, create, and edit files, including writing code
Response format
The code execution tool can return two types of results depending on the operation:Bash command response
File operation responses
View file:Results
All execution results include:stdout: Output from successful executionstderr: Error messages if execution failsreturn_code: 0 for success, non-zero for failure
- View:
file_type,content,numLines,startLine,totalLines - Create:
is_file_update(whether file already existed) - Edit:
oldStart,oldLines,newStart,newLines,lines(diff format)
Errors
Each tool type can return specific errors: Common errors (all tools):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.
Containers
The code execution tool runs in a secure, containerized environment designed specifically for code execution, with a higher focus on Python.Runtime environment
- Python version: 3.11.12
- Operating system: Linux-based container
- Architecture: x86_64 (AMD64)
Resource limits
- Memory: 5GiB 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
- Workspace scoping: Like Files, containers are scoped to the workspace of the API key
- Expiration: Containers expire 30 days after creation
Pre-installed libraries
The sandboxed Python environment includes these commonly used libraries:- Data Science: pandas, numpy, scipy, scikit-learn, statsmodels
- Visualization: matplotlib, seaborn
- File Processing: pyarrow, openpyxl, xlsxwriter, xlrd, pillow, python-pptx, python-docx, pypdf, pdfplumber, pypdfium2, pdf2image, pdfkit, tabula-py, reportlab[pycairo], Img2pdf
- Math & Computing: sympy, mpmath
- Utilities: tqdm, python-dateutil, pytz, joblib, unzip, unrar, 7zip, bc, rg (ripgrep), fd, sqlite
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
Code execution tool usage is tracked separately from token usage. Execution time has 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. Each organization receives 50 free hours of usage with the code execution tool per day. Additional usage beyond the first 50 hours is billed at $0.05 per hour, per container.Upgrade to latest tool version
By upgrading tocode-execution-2025-08-25, you get access to file manipulation and Bash capabilities, including code in multiple languages. There is no price difference.
What’s changed
Backward compatibility
- All existing Python code execution continues to work exactly as before
- No changes required to existing Python-only workflows
Upgrade steps
To upgrade, you need to make the following changes in your API requests:-
Update the beta header:
-
Update the tool type:
-
Review response handling (if parsing responses programmatically):
- The previous blocks for Python execution responses will no longer be sent
- Instead, new response types for Bash and file operations will be sent (see Response Format section)