Skip to main content
本文档将帮助您开始使用 AWS Bedrock 聊天模型. Amazon Bedrock 是一个完全托管的服务 that offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI. Using Amazon Bedrock, you can easily experiment with and evaluate top FMs for your use case, privately customize them with your data using techniques such as fine-tuning and Retrieval Augmented Generation (RAG), and build agents that execute tasks using your enterprise systems and data sources. Since Amazon Bedrock is serverless, you don’t have to manage any infrastructure, and you can securely integrate and deploy generative AI capabilities into your applications using the AWS services you are already familiar with. AWS Bedrock maintains a Converse API which provides a unified conversational interface for Bedrock models. This API does not yet support custom models. You can see a list of all models that are supported here.
We recommend the Converse API for users who do not need to use custom models. It can be accessed using ChatBedrockConverse.
Anthropic models on BedrockFor Anthropic models specifically, you can use ChatAnthropicBedrock which extends ChatAnthropic and provides the same API while running on AWS Bedrock. 请参阅 ChatAnthropicBedrock section below for details.
有关所有 Bedrock 功能和配置的详细文档,请前往 API reference.

概述

集成详情

模型功能

The below apply to both ChatBedrock and ChatBedrockConverse.

设置

要访问 Bedrock 模型,您需要创建一个 AWS account, set up the Bedrock API service, get an access key ID and secret key, and install the langchain-aws 集成包。

凭证

Head to the AWS docs 注册 AWS and setup your credentials. Alternatively, ChatBedrockConverse will read from the following environment variables by default:
You’ll also need to turn on model access for your account, which you can do by following these instructions. 要启用模型调用的自动追踪,请设置您的 LangSmith API key:

安装

LangChain 的 Bedrock 集成位于 langchain-aws 包中:

实例化

现在我们可以实例化模型对象并生成聊天补全:

调用

流式输出

Note that ChatBedrockConverse emits content blocks while streaming:
You can filter to text using the text property on the output:

Streaming tool calls and structured output

当使用 tool calling or structured output with Anthropic models, tool call arguments stream as partial JSON chunks by default. To reduce latency and get more evenly distributed chunks, you can enable Anthropic’s fine-grained tool streaming beta:
Fine-grained tool streaming is supported on Claude 4.5+ models. 请参阅 Claude documentation 了解更多详情。
当使用 fine-grained tool streaming, you may receive invalid or partial JSON inputs. Make sure to account for these edge cases in your code.

Extended thinking

This guide focuses on implementing Extended Thinking using AWS Bedrock with LangChain’s ChatBedrockConverse integration.

Supported models

Extended Thinking is available for the following Claude models on AWS Bedrock:

How extended thinking works

When extended thinking is turned on, Claude creates thinking content blocks where it outputs its internal reasoning. Claude incorporates insights from this reasoning before crafting a final response. The API response will include thinking content blocks, followed by text content blocks.

Prompt caching

Bedrock supports caching of elements of your prompts, including messages and tools. This allows you to re-use large documents, instructions, few-shot documents, and other data to reduce latency and costs.
Not all models support prompt caching. See Bedrock prompt caching supported models.
To enable caching on an element of a prompt, mark its associated content block using the cachePoint key. See example below:

Citations

Citations can be generated if they are enabled on input documents. Documents can be specified in Bedrock’s native format or LangChain’s standard types:

ChatAnthropicBedrock

For AWS Bedrock users specifically interested in Anthropic models, langchain-aws provides ChatAnthropicBedrock. This class extends ChatAnthropic and provides the same interface while running on AWS Bedrock infrastructure. This takes advantage of the Anthropic SDK’s Bedrock clients.

安装

Install langchain-aws with the anthropic extra to get the required dependencies:

使用方法

ChatAnthropicBedrock supports the same features and parameters as ChatAnthropic. You can initialize it with AWS-specific parameters:
AWS credentials can also be read from environment variables or discovered automatically by boto3:
For detailed documentation on available parameters and features, refer to the ChatAnthropic integration page.

API 参考

有关所有 ChatBedrock, ChatBedrockConverse, and ChatAnthropicBedrock features and configurations head to the API reference.