A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/code-execution-tool below:

Code execution tool - Anthropic

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 models

The code execution tool is available on:

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:

  1. Claude evaluates whether code execution would help answer your question
  2. Claude writes and executes Python code in a secure sandbox environment
  3. Code execution may occur multiple times throughout a single request
  4. Claude provides results with any generated charts, calculations, or analysis

The code execution tool requires no additional parameters:

Response format

Here’s an example response with code execution:

Results

Code execution results include:

Errors

If there is an error using the tool there will be a code_execution_tool_result_error

Possible errors include

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.

Supported file types

The Python environment is capable of working with but not limited to the following file types

Loading files for code execution
  1. Upload your file using the Files API
  2. Reference the file in your message using a container_upload content block
  3. 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 Resource limits Networking and security Pre-installed libraries

The sandboxed Python environment includes these commonly used libraries:

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.


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