Tool use - Amazon Bedrock

Tool use

With Anthropic Claude models, you can specify a tool that the model can use to answer a message. For example, you could specify a tool that gets the most popular song on a radio station. If the user passes the message What's the most popular song on WZPZ?, the model determines that the tool you specified can help answer the question. In its response, the model requests that you run the tool on its behalf. You then run the tool and pass the tool result to the model, which then generates a response for the original message. For more information, see Tool use (function calling) in the Anthropic Claude documentation.

Tip

We recommend that you use the Converse API for integrating tool use into your application. For more information, see Use a tool to complete an Amazon Bedrock model response.

You specify the tools that you want to make available to a model in the tools field. The following example is for a tool that gets the most popular songs on a radio station.

[ { "name": "top_song", "description": "Get the most popular song played on a radio station.", "input_schema": { "type": "object", "properties": { "sign": { "type": "string", "description": "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKRP." } }, "required": [ "sign" ] } } ]

When the model needs a tool to generate a response to a message, it returns information about the requested tool, and the input to the tool, in the message content field. It also sets the stop reason for the response to tool_use.

{ "id": "msg_bdrk_01USsY5m3XRUF4FCppHP8KBx", "type": "message", "role": "assistant", "model": "claude-3-sonnet-20240229", "stop_sequence": null, "usage": { "input_tokens": 375, "output_tokens": 36 }, "content": [ { "type": "tool_use", "id": "toolu_bdrk_01SnXQc6YVWD8Dom5jz7KhHy", "name": "top_song", "input": { "sign": "WZPZ" } } ], "stop_reason": "tool_use" }

In your code, you call the tool on the tools behalf. You then pass the tool result (tool_result) in a user message to the model.

{ "role": "user", "content": [ { "type": "tool_result", "tool_use_id": "toolu_bdrk_01SnXQc6YVWD8Dom5jz7KhHy", "content": "Elemental Hotel" } ] }

In its response, the model uses the tool result to generate a response for the original message.

{ "id": "msg_bdrk_012AaqvTiKuUSc6WadhUkDLP", "type": "message", "role": "assistant", "model": "claude-3-sonnet-20240229", "content": [ { "type": "text", "text": "According to the tool, the most popular song played on radio station WZPZ is \"Elemental Hotel\"." } ], "stop_reason": "end_turn" }

Fine-grained tool streaming

Fine-grained tool streaming is an Anthropic Claude model capability available with Claude Sonnet 4, and Claude Opus 4. With fine-grained tool streaming, Claude developers can stream tool use parameters without buffering or JSON validation, reducing the latency to begin receiving large parameters.

Note

When using fine-grained tool streaming, you may potentially receive invalid or partial JSON inputs. Please make sure to account for these edge cases in your code.

To use this feature, simply add the header fine-grained-tool-streaming-2025-05-14 to a tool use request.

Here’s an example of how to specify the fine-grained tool streaming header:

{ "anthropic_version": "bedrock-2023-05-31", "max_tokens": 1024, "anthropic_beta": ["fine-grained-tool-streaming-2025-05-14"], "messages": [ { "role": "user", "content": "Can you write a long poem and make a file called poem.txt?" } ], "tools": [ { "name": "make_file", "description": "Write text to a file", "input_schema": { "type": "object", "properties": { "filename": { "type": "string", "description": "The filename to write text to" }, "lines_of_text": { "type": "array", "description": "An array of lines of text to write to the file" } }, "required": [ "filename", "lines_of_text" ] } } ] }

In this example, fine-grained tool streaming enables Claude to stream the lines of a long poem into the tool call make_file without buffering to validate if the lines_of_text parameter is valid JSON. This means you can see the parameter stream as it arrives, without having to wait for the entire parameter to buffer and validate.

With fine-grained tool streaming, tool use chunks start streaming faster, and are often longer and contain fewer word breaks. This is due to differences in chunking behavior.

For example, without fine-grained streaming (15s delay):

Chunk 1: '{"' Chunk 2: 'query": "Ty' Chunk 3: 'peScri' Chunk 4: 'pt 5.0 5.1 ' Chunk 5: '5.2 5' Chunk 6: '.3' Chunk 8: ' new f' Chunk 9: 'eatur' ...

With fine-grained streaming (3s delay):

Chunk 1: '{"query": "TypeScript 5.0 5.1 5.2 5.3' Chunk 2: ' new features comparison'
Note

Because fine-grained streaming sends parameters without buffering or JSON validation, there is no guarantee that the resulting stream will complete in a valid JSON string. Particularly, if the stop reason max_tokens is reached, the stream may end midway through a parameter and may be incomplete. You will generally have to write specific support to handle when max_tokens is reached.

Computer use (Beta)

Computer use is an Anthropic Claude model capability (in beta) available with Claude 3.5 Sonnet v2, Claude 3.7 Sonnet, Claude Sonnet 4, and Claude Opus 4. With computer use, Claude can help you automate tasks through basic GUI actions.

Warning

Computer use feature is made available to you as a ‘Beta Service’ as defined in the AWS Service Terms. It is subject to your Agreement with AWS and the AWS Service Terms, and the applicable model EULA. Please be aware that the Computer Use API poses unique risks that are distinct from standard API features or chat interfaces. These risks are heightened when using the Computer Use API to interact with the Internet. To minimize risks, consider taking precautions such as:

  • Operate computer use functionality in a dedicated Virtual Machine or container with minimal privileges to prevent direct system attacks or accidents.

  • To prevent information theft, avoid giving the Computer Use API access to sensitive accounts or data.

  • Limiting the computer use APIs internet access to required domains to reduce exposure to malicious content.

  • To ensure proper oversight, keep a human in the loop for sensitive tasks (such as making decisions that could have meaningful real-world consequences) and for anything requiring affirmative consent (such as accepting cookies, executing financial transactions, or agreeing to terms of service).

Any content that you enable Claude to see or access can potentially override instructions or cause Claude to make mistakes or perform unintended actions. Taking proper precautions, such as isolating Claude from sensitive surfaces, is essential — including to avoid risks related to prompt injection. Before enabling or requesting permissions necessary to enable computer use features in your own products, please inform end users of any relevant risks, and obtain their consent as appropriate.

The computer use API offers several pre-defined computer use tools for you to use. You can then create a prompt with your request, such as “send an email to Ben with the notes from my last meeting” and a screenshot (when required). The response contains a list of tool_use actions in JSON format (for example, scroll_down, left_button_press, screenshot). Your code runs the computer actions and provides Claude with screenshot showcasing outputs (when requested).

Since the release of Claude 3.5 v2, the tools parameter has been updated to accept polymorphic tool types; a tool.type property was added to distinguish them. type is optional; if omitted, the tool is assumed to be a custom tool (previously the only tool type supported). To access computer use, you must use the anthropic_beta parameter, with a corresponding enum, whose value depends on the model version in use. See the following table for more information.

Only requests made with this parameter and enum can use the computer use tools. It can be specified as follows: "anthropic_beta": ["computer-use-2025-01-24"].

Model Beta header

Claude Opus 4.1

Claude Opus 4

Claude Sonnet 4

Claude 3.7 Sonnet

computer-use-2025-01-24
Claude 3.5 Sonnet v2 computer-use-2024-10-22

For more information, see Computer use (beta) in the Anthropic documentation.

The following is an example response that assumes the request contained a screenshot of your desktop with a Firefox icon.

{ "id": "msg_123", "type": "message", "role": "assistant", "model": "anthropic.claude-3-5-sonnet-20241022-v2:0", "content": [ { "type": "text", "text": "I see the Firefox icon. Let me click on it and then navigate to a weather website." }, { "type": "tool_use", "id": "toolu_123", "name": "computer", "input": { "action": "mouse_move", "coordinate": [ 708, 736 ] } }, { "type": "tool_use", "id": "toolu_234", "name": "computer", "input": { "action": "left_click" } } ], "stop_reason": "tool_use", "stop_sequence": null, "usage": { "input_tokens": 3391, "output_tokens": 132 } }

Anthropic defined tools

Anthropic provides a set of tools to enable certain Claude models to effectively use computers. When specifying an Anthropic defined tool, the description and tool_schema fields are not necessary or allowed. Anthropic defined tools are defined by Anthropic, but you must explicitly evaluate the results of the tool and return the tool_results to Claude. As with any tool, the model does not automatically execute the tool. Each Anthropic defined tool has versions optimized for specific models Claude 3.5 Sonnet (new) and Claude 3.7 Sonnet:

Model

Tool

Notes

Claude Claude Opus 4.1

Claude Claude Opus 4

Claude Sonnet 4

{ "type": "text_editor_20250124", "name": "str_replace_based_edit_tool" }

Update to existing str_replace_editor tool

Claude 3.7 Sonnet

{ "type": "computer_20250124", "name": "computer" }

Includes new actions for more precise control

Claude 3.7 Sonnet

{ "type": "text_editor_20250124", "name": "str_replace_editor" }

Same capabilities as 20241022 version

Claude 3.5 Sonnet v2

{ "type": "bash_20250124", "name": "bash" }

Same capabilities as 20241022 version

Claude 3.5 Sonnet v2

{ "type": "text_editor_20241022", "name": "str_replace_editor" }

Claude 3.5 Sonnet v2

{ "type": "bash_20241022", "name": "bash" }

Claude 3.5 Sonnet v2

{ "type": "computer_20241022", "name": "computer" }

The type field identifies the tool and its parameters for validation purposes, the name field is the tool name exposed to the model.

If you want to prompt the model to use one of these tools, you can explicitly refer the tool by the name field. The name field must be unique within the tool list; you cannot define a tool with the same name as an Anthropic defined tool in the same API call.

Cost considerations for tool use

Tool use requests are priced based on the following factors:

  1. The total number of input tokens sent to the model (including in the tools parameter).

  2. The number of output tokens generated.

Tools are priced the same as all other Claude API requests, but do include additional tokens per request. The additional tokens from tool use come from the following:

  • The tools parameter in the API requests. For example, tool names, descriptions, and schemas.

  • Any tool_use content blocks in API requests and responses.

  • Any tool_result content blocks in API requests.

When you use tools, the Anthropic models automatically include a special system prompt that enables tool use. The number of tool use tokens required for each model is listed in the following table. This table excludes the additional tokens described previously. Note that this table assumes at least one tool is provided. If no tools are provided, then a tool choice of none uses 0 additional system prompt tokens.

Model Tool choice Tool use system prompt token count

Claude Opus 4.1

Claude Opus 4

Claude Sonnet 4

Claude 3.7 Sonnet

Claude 3.5 Sonnet v2

auto or none 346

Claude Opus 4.1

Claude Opus 4

Claude Sonnet 4

Claude 3.7 Sonnet

Claude 3.5 Sonnet v2

any or tool 313

Claude 3.5 Sonnet

auto or none 294

Claude 3.5 Sonnet

any or tool 261

Claude 3 Opus

auto or none 530

Claude 3 Opus

any or tool 281

Claude 3 Sonnet

auto or none 159

Claude 3 Sonnet

any or tool 235

Claude 3 Haiku

auto or none 264

Claude 3 Haiku

any or tool 340