Anthropic Claude Messages API - Amazon Bedrock

Anthropic Claude Messages API

This section provides inference parameters and code examples for using the Anthropic Claude Messages API.

Anthropic Claude Messages API overview

You can use the Messages API to create chat bots or virtual assistant applications. The API manages the conversational exchanges between a user and an Anthropic Claude model (assistant).

Tip

This topic shows how to uses the Anthropic Claude messages API with the base inference operations (InvokeModel or InvokeModelWithResponseStream). However, we recommend that you use the Converse API to implement messages in your application. The Converse API provides a unified set of parameters that work across all models that support messages. For more information, see Carry out a conversation with the Converse API operations.

Anthropic trains Claude models to operate on alternating user and assistant conversational turns. When creating a new message, you specify the prior conversational turns with the messages parameter. The model then generates the next Message in the conversation.

Each input message must be an object with a role and content. You can specify a single user-role message, or you can include multiple user and assistant messages. The first message must always use the user role.

If you are using the technique of prefilling the response from Claude (filling in the beginning of Claude's response by using a final assistant role Message), Claude will respond by picking up from where you left off. With this technique, Claude will still return a response with the assistant role.

If the final message uses the assistant role, the response content will continue immediately from the content in that message. You can use this to constrain part of the model's response.

Example with a single user message:

[{"role": "user", "content": "Hello, Claude"}]

Example with multiple conversational turns:

[ {"role": "user", "content": "Hello there."}, {"role": "assistant", "content": "Hi, I'm Claude. How can I help you?"}, {"role": "user", "content": "Can you explain LLMs in plain English?"}, ]

Example with a partially-filled response from Claude:

[ {"role": "user", "content": "Please describe yourself using only JSON"}, {"role": "assistant", "content": "Here is my JSON description:\n{"}, ]

Each input message content may be either a single string or an array of content blocks, where each block has a specific type. Using a string is shorthand for an array of one content block of type "text". The following input messages are equivalent:

{"role": "user", "content": "Hello, Claude"}
{"role": "user", "content": [{"type": "text", "text": "Hello, Claude"}]}

For information about creating prompts for Anthropic Claude models, see Intro to prompting in the Anthropic Claude documentation. If you have existing Text Completion prompts that you want to migrate to the messages API, see Migrating from Text Completions.

System prompts

You can also include a system prompt in the request. A system prompt lets you provide context and instructions to Anthropic Claude, such as specifying a particular goal or role. Specify a system prompt in the system field, as shown in the following example.

"system": "You are Claude, an AI assistant created by Anthropic to be helpful, harmless, and honest. Your goal is to provide informative and substantive responses to queries while avoiding potential harms."

For more information, see System prompts in the Anthropic documentation.

Multimodal prompts

A multimodal prompt combines multiple modalities (images and text) in a single prompt. You specify the modalities in the content input field. The following example shows how you could ask Anthropic Claude to describe the content of a supplied image. For example code, see Multimodal code examples.

{ "anthropic_version": "bedrock-2023-05-31", "max_tokens": 1024, "messages": [ { "role": "user", "content": [ { "type": "image", "source": { "type": "base64", "media_type": "image/jpeg", "data": "iVBORw..." } }, { "type": "text", "text": "What's in these images?" } ] } ] }
Note

The following restrictions pertain to the content field:

  • You can include up to 20 images. Each image's size, height, and width must be no more than 3.75 MB, 8,000 px, and 8,000 px, respectively.

  • You can include up to five documents. Each document's size must be no more than 4.5 MB.

  • You can only include images and documents if the role is user.

Each image you include in a request counts towards your token usage. For more information, see Image costs in the Anthropic documentation.

Tool use (function calling)

With Anthropic Claude 3 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 specifed 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" }

Computer use (Beta)

Computer use is a new Anthropic Claude model capability (in beta) available with Claude 3.5 Sonnet v2 only. 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 API’s 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 (computer_20241022, bash_20241022, and text_editor_20241022) 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).

The tools parameter has been updated to accept polymorphic tool types; a new tool.type property is being added to distinguish them. type is optional; if omitted, the tool is assumed to be a custom tool (previously the only tool type supported). Additionally, a new parameter, anthropic_beta, has been added, with a corresponding enum value: computer-use-2024-10-22. Only requests made with this parameter and enum can use the new computer use tools. It can be specified as follows: "anthropic_beta": ["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 } }

Supported models

You can use the Messages API with the following Anthropic Claude models.

  • Anthropic Claude Instant v1.2

  • Anthropic Claude 2 v2

  • Anthropic Claude 2 v2.1

  • Anthropic Claude 3 Sonnet

  • Anthropic Claude 3.5 Sonnet

  • Anthropic Claude 3.5 Sonnet v2

  • Anthropic Claude 3 Haiku

  • Anthropic Claude 3 Opus

Request and Response

The request body is passed in the body field of a request to InvokeModel or InvokeModelWithResponseStream. The maximum size of the payload you can send in a request is 20MB.

For more information, see https://docs.anthropic.com/claude/reference/messages_post.

Request

Anthropic Claude has the following inference parameters for a messages inference call.

{ "anthropic_version": "bedrock-2023-05-31", "anthropic_beta": ["computer-use-2024-10-22"] "max_tokens": int, "system": string, "messages": [ { "role": string, "content": [ { "type": "image", "source": { "type": "base64", "media_type": "image/jpeg", "data": "content image bytes" } }, { "type": "text", "text": "content text" } ] } ], "temperature": float, "top_p": float, "top_k": int, "tools": [ { "type": "custom", "name": string, "description": string, "input_schema": json }, { "type": "computer_20241022", "name": "computer", "display_height_px": int, "display_width_px": int, "display_number": 0 int }, { "type": "bash_20241022", "name": "bash" }, { "type": "text_editor_20241022", "name": "str_replace_editor" } ], "tool_choice": { "type" : string, "name" : string, }, "stop_sequences": [string] }

The following are required parameters.

  • anthropic_version – (Required) The anthropic version. The value must be bedrock-2023-05-31.

  • anthropic_beta – (Required, if using the computer use API) The anthropic beta to use. To use the computer use API, the value must be computer-use-2024-10-22.

  • max_tokens – (Required) The maximum number of tokens to generate before stopping.

    Note that Anthropic Claude models might stop generating tokens before reaching the value of max_tokens. Different Anthropic Claude models have different maximum values for this parameter. For more information, see Model comparison.

  • messages – (Required) The input messages.

    • role – The role of the conversation turn. Valid values are user and assistant.

    • content – (required) The content of the conversation turn.

      • type – (required) The type of the content. Valid values are image and text.

        If you specify image, you must also specify the image source in the following format

        source – (required) The content of the conversation turn.

        • type – (required) The encoding type for the image. You can specify base64.

        • media_type – (required) The type of the image. You can specify the following image formats.

          • image/jpeg

          • image/png

          • image/webp

          • image/gif

        • data – (required) The base64 encoded image bytes for the image. The maximum image size is 3.75MB. The maximum height and width of an image is 8000 pixels.

        If you specify text, you must also specify the prompt in text.

The following are optional parameters.

  • system – (Optional) The system prompt for the request.

    A system prompt is a way of providing context and instructions to Anthropic Claude, such as specifying a particular goal or role. For more information, see System prompts in the Anthropic documentation.

    Note

    You can use system prompts with Anthropic Claude version 2.1 or higher.

  • stop_sequences – (Optional) Custom text sequences that cause the model to stop generating. Anthropic Claude models normally stop when they have naturally completed their turn, in this case the value of the stop_reason response field is end_turn. If you want the model to stop generating when it encounters custom strings of text, you can use the stop_sequences parameter. If the model encounters one of the custom text strings, the value of the stop_reason response field is stop_sequence and the value of stop_sequence contains the matched stop sequence.

    The maximum number of entries is 8191.

  • temperature – (Optional) The amount of randomness injected into the response.

    Default Minimum Maximum

    1

    0

    1

  • top_p – (Optional) Use nucleus sampling.

    In nucleus sampling, Anthropic Claude computes the cumulative distribution over all the options for each subsequent token in decreasing probability order and cuts it off once it reaches a particular probability specified by top_p. You should alter either temperature or top_p, but not both.

    Default Minimum Maximum

    0.999

    0

    1

  • top_k – (Optional) Only sample from the top K options for each subsequent token.

    Use top_k to remove long tail low probability responses.

    Default Minimum Maximum

    Disabled by default

    0

    500

  • tools – (Optional) Definitions of tools that the model may use.

    Note

    Requires an Anthropic Claude 3 model.

    If you include tools in your request, the model may return tool_use content blocks that represent the model's use of those tools. You can then run those tools using the tool input generated by the model and then optionally return results back to the model using tool_result content blocks.

    You can pass the following tool types:

    Custom

    Definition for a custom tool.

    • (optional) type – The type of the tool. If defined, use the value custom.

    • name – The name of the tool.

    • description – (optional, but strongly recommended) The description of the tool.

    • input_schema – The JSON schema for the tool.

    Computer

    Definition for the computer tool that you use with the computer use API.

    • type – The value must be computer_20241022.

    • name – The value must be computer.

    • (Required) display_height_px – The height of the display being controlled by the model, in pixels..

      Default Minimum Maximum

      None

      1

      No maximum

    • (Required) display_width_px – The width of the display being controlled by the model, in pixels.

      Default Minimum Maximum

      None

      1

      No maximum

    • (Optional) display_number – The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.

      Default Minimum Maximum

      None

      0

      N

    bash

    Definition for the bash tool that you use with the computer use API.

    • (optional) type – The value must be bash_20241022.

    • name – The value must be bash. the tool.

    text editor

    Definition for the text editor tool that you use with the computer use API.

    • (optional) type – The value must be text_editor_20241022.

    • name – The value must be str_replace_editor. the tool.

  • tool_choice – (Optional) Specifices how the model should use the provided tools. The model can use a specific tool, any available tool, or decide by itself.

    Note

    Requires an Anthropic Claude 3 model.

    • type – The type of tool choice. Possible values are any (use any available tool), auto (the model decides), and tool (use the specified tool).

    • name – (Optional) The name of the tool to use. Required if you specify tool in the type field.

Response

The Anthropic Claude model returns the following fields for a messages inference call.

{ "id": string, "model": string, "type" : "message", "role" : "assistant", "content": [ { "type": string, "text": string } ], "stop_reason": string, "stop_sequence": string, "tool_use" : { "type": string, "id" : string, "input" : json }, "usage": { "input_tokens": integer, "output_tokens": integer } }
  • id – The unique identifier for the response. The format and length of the ID might change over time.

  • model – The ID for the Anthropic Claude model that made the request.

  • stop_reason – The reason why Anthropic Claude stopped generating the response.

    • end_turn – The model reached a natural stopping point

    • max_tokens – The generated text exceeded the value of the max_tokens input field or exceeded the maximum number of tokens that the model supports.' .

    • stop_sequence – The model generated one of the stop sequences that you specified in the stop_sequences input field.

  • stop_sequence – The stop sequence that ended the generation.

  • type – The type of response. The value is always message.

  • role – The conversational role of the generated message. The value is always assistant.

  • content – The content generated by the model. Returned as an array. There are two types of content, text and tool_use.

    • text – A text response.

      • type – This value is text. The type of the content.

      • text – The text of the content.

    • tool_use – A request to from the model to use a tool.

      • type – This value is text.The type of the content.

      • id – The ID for the tool that the model is requesting use of.

      • input – The input parameters to pass to the tool.

  • usage – Container for the number of tokens that you supplied in the request and the number tokens of that the model generated in the response.

    • input_tokens – The number of input tokens in the request.

    • output_tokens – The number tokens of that the model generated in the response.

    • stop_sequence – The model generated one of the stop sequences that you specified in the stop_sequences input field.

Code examples

The following code examples show how to use the messages API.

Messages code example

This example shows how to send a single turn user message and a user turn with a prefilled assistant message to an Anthropic Claude 3 Sonnet model.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate a message with Anthropic Claude (on demand). """ import boto3 import json import logging from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def generate_message(bedrock_runtime, model_id, system_prompt, messages, max_tokens): body=json.dumps( { "anthropic_version": "bedrock-2023-05-31", "max_tokens": max_tokens, "system": system_prompt, "messages": messages } ) response = bedrock_runtime.invoke_model(body=body, modelId=model_id) response_body = json.loads(response.get('body').read()) return response_body def main(): """ Entrypoint for Anthropic Claude message example. """ try: bedrock_runtime = boto3.client(service_name='bedrock-runtime') model_id = 'anthropic.claude-3-sonnet-20240229-v1:0' system_prompt = "Please respond only with emoji." max_tokens = 1000 # Prompt with user turn only. user_message = {"role": "user", "content": "Hello World"} messages = [user_message] response = generate_message (bedrock_runtime, model_id, system_prompt, messages, max_tokens) print("User turn only.") print(json.dumps(response, indent=4)) # Prompt with both user turn and prefilled assistant response. #Anthropic Claude continues by using the prefilled assistant text. assistant_message = {"role": "assistant", "content": "<emoji>"} messages = [user_message, assistant_message] response = generate_message(bedrock_runtime, model_id,system_prompt, messages, max_tokens) print("User turn and prefilled assistant response.") print(json.dumps(response, indent=4)) except ClientError as err: message=err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) if __name__ == "__main__": main()

Multimodal code examples

The following examples show how to pass an image and prompt text in a multimodal message to an Anthropic Claude 3 Sonnet model.

Multimodal prompt with InvokeModel

The following example shows how to send a multimodal prompt to Anthropic Claude 3 Sonnet with InvokeModel.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to run a multimodal prompt with Anthropic Claude (on demand) and InvokeModel. """ import json import logging import base64 import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens): """ Invokes a model with a multimodal prompt. Args: bedrock_runtime: The Amazon Bedrock boto3 client. model_id (str): The model ID to use. messages (JSON) : The messages to send to the model. max_tokens (int) : The maximum number of tokens to generate. Returns: None. """ body = json.dumps( { "anthropic_version": "bedrock-2023-05-31", "max_tokens": max_tokens, "messages": messages } ) response = bedrock_runtime.invoke_model( body=body, modelId=model_id) response_body = json.loads(response.get('body').read()) return response_body def main(): """ Entrypoint for Anthropic Claude multimodal prompt example. """ try: bedrock_runtime = boto3.client(service_name='bedrock-runtime') model_id = 'anthropic.claude-3-sonnet-20240229-v1:0' max_tokens = 1000 input_image = "/path/to/image" input_text = "What's in this image?" # Read reference image from file and encode as base64 strings. with open(input_image, "rb") as image_file: content_image = base64.b64encode(image_file.read()).decode('utf8') message = {"role": "user", "content": [ {"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": content_image}}, {"type": "text", "text": input_text} ]} messages = [message] response = run_multi_modal_prompt( bedrock_runtime, model_id, messages, max_tokens) print(json.dumps(response, indent=4)) except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) if __name__ == "__main__": main()

Streaming multimodal prompt with InvokeModelWithResponseStream

The following example shows how to stream the response from a multimodal prompt sent to Anthropic Claude 3 Sonnet with InvokeModelWithResponseStream.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to stream the response from Anthropic Claude Sonnet (on demand) for a multimodal request. """ import json import base64 import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) def stream_multi_modal_prompt(bedrock_runtime, model_id, input_text, image, max_tokens): """ Streams the response from a multimodal prompt. Args: bedrock_runtime: The Amazon Bedrock boto3 client. model_id (str): The model ID to use. input_text (str) : The prompt text image (str) : The path to an image that you want in the prompt. max_tokens (int) : The maximum number of tokens to generate. Returns: None. """ with open(image, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) body = json.dumps({ "anthropic_version": "bedrock-2023-05-31", "max_tokens": max_tokens, "messages": [ { "role": "user", "content": [ {"type": "text", "text": input_text}, {"type": "image", "source": {"type": "base64", "media_type": "image/jpeg", "data": encoded_string.decode('utf-8')}} ] } ] }) response = bedrock_runtime.invoke_model_with_response_stream( body=body, modelId=model_id) for event in response.get("body"): chunk = json.loads(event["chunk"]["bytes"]) if chunk['type'] == 'message_delta': print(f"\nStop reason: {chunk['delta']['stop_reason']}") print(f"Stop sequence: {chunk['delta']['stop_sequence']}") print(f"Output tokens: {chunk['usage']['output_tokens']}") if chunk['type'] == 'content_block_delta': if chunk['delta']['type'] == 'text_delta': print(chunk['delta']['text'], end="") def main(): """ Entrypoint for Anthropic Claude Sonnet multimodal prompt example. """ model_id = "anthropic.claude-3-sonnet-20240229-v1:0" input_text = "What can you tell me about this image?" image = "/path/to/image" max_tokens = 100 try: bedrock_runtime = boto3.client('bedrock-runtime') stream_multi_modal_prompt( bedrock_runtime, model_id, input_text, image, max_tokens) except ClientError as err: message = err.response["Error"]["Message"] logger.error("A client error occurred: %s", message) print("A client error occured: " + format(message)) if __name__ == "__main__": main()