Today, Amazon Bedrock has introduced in preview two capabilities that help reduce costs and latency for generative AI applications:
Amazon Bedrock Intelligent Prompt Routing – When invoking a model, you can now use a combination of foundation models (FMs) from the same model family to help optimize for quality and cost. For example, with the Anthropic’s Claude model family, Amazon Bedrock can intelligently route requests between Claude 3.5 Sonnet and Claude 3 Haiku depending on the complexity of the prompt. Similarly, Amazon Bedrock can route requests between Meta Llama 3.1 70B and 8B. The prompt router predicts which model will provide the best performance for each request while optimizing the quality of response and cost. This is particularly useful for applications such as customer service assistants, where uncomplicated queries can be handled by smaller, faster, and more cost-effective models, and complex queries are routed to more capable models. Intelligent Prompt Routing can reduce costs by up to 30 percent without compromising on accuracy.
Amazon Bedrock now supports prompt caching – You can now cache frequently used context in prompts across multiple model invocations. This is especially valuable for applications that repeatedly use the same context, such as document Q&A systems where users ask multiple questions about the same document or coding assistants that need to maintain context about code files. The cached context remains available for up to 5 minutes after each access. Prompt caching in Amazon Bedrock can reduce costs by up to 90% and latency by up to 85% for supported models.
These features make it easier to reduce latency and balance performance with cost efficiency. Let’s look at how you can use them in your applications.
Using Amazon Bedrock Intelligent Prompt Routing in the console
Amazon Bedrock Intelligent Prompt Routing uses advanced prompt matching and model understanding techniques to predict the performance of each model for every request, optimizing for quality of responses and cost. During the preview, you can use the default prompt routers for Anthropic’s Claude and Meta Llama model families.
Intelligent prompt routing can be accessed through the AWS Management Console, the AWS Command Line Interface (AWS CLI), and the AWS SDKs. In the Amazon Bedrock console, I choose Prompt routers in the Foundation models section of the navigation pane.
I choose the Anthropic Prompt Router default router to get more information.
From the configuration of the prompt router, I see that it’s routing requests between Claude 3.5 Sonnet and Claude 3 Haiku using cross-Region inference profiles. The routing criteria defines the quality difference between the response of the largest model and the smallest model for each prompt as predicted by the router internal model at runtime. The fallback model, used when none of the chosen models meet the desired performance criteria, is Anthropic’s Claude 3.5 Sonnet.
I choose Open in Playground to chat using the prompt router and enter this prompt:
Alice has N brothers and she also has M sisters. How many sisters does Alice’s brothers have?
The result is quickly provided. I choose the new Router metrics icon on the right to see which model was selected by the prompt router. In this case, because the question is rather complex, Anthropic’s Claude 3.5 Sonnet was used.
Now I ask a straightforward question to the same prompt router:
Describe the purpose of a 'hello world' program in one line.
This time, Anthropic’s Claude 3 Haiku has been selected by the prompt router.
I select the Meta Prompt Router to check its configuration. It’s using the cross-Region inference profiles for Llama 3.1 70B and 8B with the 70B model as fallback.
Prompt routers are integrated with other Amazon Bedrock capabilities, such as Amazon Bedrock Knowledge Bases and Amazon Bedrock Agents, or when performing evaluations. For example, here I create a model evaluation to help me compare, for my use case, a prompt router to another model or prompt router.
To use a prompt router in an application, I need to set the prompt router Amazon Resource Name (ARN) as model ID in the Amazon Bedrock API. Let’s see how this works with the AWS CLI and an AWS SDK.
Using Amazon Bedrock Intelligent Prompt Routing with the AWS CLI
The Amazon Bedrock API has been extended to handle prompt routers. For example, I can list the existing prompt routes in an AWS Region using ListPromptRouters:
In output, I receive a summary of the existing prompt routers, similar to what I saw in the console.
Here’s the full output of the previous command:
{ "promptRouterSummaries": [ { "promptRouterName": "Anthropic Prompt Router", "routingCriteria": { "responseQualityDifference": 0.26 }, "description": "Routes requests among models in the Claude family", "createdAt": "2024-11-20T00:00:00+00:00", "updatedAt": "2024-11-20T00:00:00+00:00", "promptRouterArn": "arn:aws:bedrock:us-east-1:123412341234:default-prompt-router/anthropic.claude:1", "models": [ { "modelArn": "arn:aws:bedrock:us-east-1:123412341234:inference-profile/us.anthropic.claude-3-haiku-20240307-v1:0" }, { "modelArn": "arn:aws:bedrock:us-east-1:123412341234:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0" } ], "fallbackModel": { "modelArn": "arn:aws:bedrock:us-east-1:123412341234:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0" }, "status": "AVAILABLE", "type": "default" }, { "promptRouterName": "Meta Prompt Router", "routingCriteria": { "responseQualityDifference": 0.0 }, "description": "Routes requests among models in the LLaMA family", "createdAt": "2024-11-20T00:00:00+00:00", "updatedAt": "2024-11-20T00:00:00+00:00", "promptRouterArn": "arn:aws:bedrock:us-east-1:123412341234:default-prompt-router/meta.llama:1", "models": [ { "modelArn": "arn:aws:bedrock:us-east-1:123412341234:inference-profile/us.meta.llama3-1-8b-instruct-v1:0" }, { "modelArn": "arn:aws:bedrock:us-east-1:123412341234:inference-profile/us.meta.llama3-1-70b-instruct-v1:0" } ], "fallbackModel": { "modelArn": "arn:aws:bedrock:us-east-1:123412341234:inference-profile/us.meta.llama3-1-70b-instruct-v1:0" }, "status": "AVAILABLE", "type": "default" } ] }I can get information about a specific prompt router using GetPromptRouter with a prompt router ARN. For example, for the Meta Llama model family:
To use a prompt router with Amazon Bedrock, I set the prompt router ARN as model ID when making API calls. For example, here I use the Anthropic Prompt Router with the AWS CLI and the Amazon Bedrock Converse API:
In output, invocations using a prompt router include a new trace section that tells which model was actually used. In this case, it’s Anthropic’s Claude 3.5 Sonnet:
{ "output": { "message": { "role": "assistant", "content": [ { "text": "To solve this problem, let's think it through step-by-step:\n\n1) First, we need to understand the relationships:\n - Alice has N brothers\n - Alice has M sisters\n\n2) Now, we need to consider who Alice's brothers' sisters are:\n - Alice herself is a sister to all her brothers\n - All of Alice's sisters are also sisters to Alice's brothers\n\n3) So, the total number of sisters that Alice's brothers have is:\n - The number of Alice's sisters (M)\n - Plus Alice herself (+1)\n\n4) Therefore, the answer can be expressed as: M + 1\n\nThus, Alice's brothers have M + 1 sisters." } ] } }, . . . "trace": { "promptRouter": { "invokedModelId": "arn:aws:bedrock:us-east-1:123412341234:inference-profile/us.anthropic.claude-3-5-sonnet-20240620-v1:0" } } }Using Amazon Bedrock Intelligent Prompt Routing with an AWS SDK
Using an AWS SDK with a prompt router is similar to the previous command line experience. When invoking a model, I set the model ID to the prompt model ARN. For example, in this Python code I’m using the Meta Llama router with the ConverseStream API:
This script prints the response text and the content of the trace in response metadata. For this uncomplicated request, the faster and more affordable model has been selected by the prompt router:
Using prompt caching with an AWS SDK
You can use prompt caching with the Amazon Bedrock Converse API. When you tag content for caching and send it to the model for the first time, the model processes the input and saves the intermediate results in a cache. For subsequent requests containing the same content, the model loads the preprocessed results from the cache, significantly reducing both costs and latency.
You can implement prompt caching in your applications with a few steps:
- Identify the portions of your prompts that are frequently reused.
- Tag these sections for caching in the list of messages using the new cachePoint block.
- Monitor cache usage and latency improvements in the response metadata usage section.
Here’s an example of implementing prompt caching when working with documents.
First, I download three decision guides in PDF format from the AWS website. These guides help choose the AWS services that fit your use case.
Then, I use a Python script to ask three questions about the documents. In the code, I create a converse() function to handle the conversation with the model. The first time I call the function, I include a list of documents and a flag to add a cachePoint block.
import json import boto3 MODEL_ID = "us.anthropic.claude-3-5-sonnet-20241022-v2:0" AWS_REGION = "us-west-2" bedrock_runtime = boto3.client( "bedrock-runtime", region_name=AWS_REGION, ) DOCS = [ "bedrock-or-sagemaker.pdf", "generative-ai-on-aws-how-to-choose.pdf", "machine-learning-on-aws-how-to-choose.pdf", ] messages = [] def converse(new_message, docs=[], cache=False): if len(messages) == 0 or messages[-1]["role"] != "user": messages.append({"role": "user", "content": []}) for doc in docs: print(f"Adding document: {doc}") name, format = doc.rsplit('.', maxsplit=1) with open(doc, "rb") as f: bytes = f.read() messages[-1]["content"].append({ "document": { "name": name, "format": format, "source": {"bytes": bytes}, } }) messages[-1]["content"].append({"text": new_message}) if cache: messages[-1]["content"].append({"cachePoint": {"type": "default"}}) response = bedrock_runtime.converse( modelId=MODEL_ID, messages=messages, ) output_message = response["output"]["message"] response_text = output_message["content"][0]["text"] print("Response text:") print(response_text) print("Usage:") print(json.dumps(response["usage"], indent=2)) messages.append(output_message) converse("Compare AWS Trainium and AWS Inferentia in 20 words or less.", docs=DOCS, cache=True) converse("Compare Amazon Textract and Amazon Transcribe in 20 words or less.") converse("Compare Amazon Q Business and Amazon Q Developer in 20 words or less.")For each invocation, the script prints the response and the usage counters.
The usage section of the response contains two new counters: cacheReadInputTokenCount and cacheWriteInputTokenCount. The total number of tokens for an invocation is the sum of the input and output tokens plus the tokens read and written into the cache.
Each invocation processes a list of messages. The messages in the first invocation contain the documents, the first question, and the cache point. Because the messages preceding the cache point aren’t currently in the cache, they’re written to cache. According to the usage counters, 29,841 tokens have been written into the cache.
For the next invocations, the previous response and the new question are appended to the list of messages. The messages before the cachePoint are not changed and found in the cache.
As expected, we can tell from the usage counters that the same number of tokens previously written is now read from the cache.
In my tests, the next invocations take 55 percent less time to complete compared to the first one. Depending on your use case (for example, with more cached content), prompt caching can improve latency up to 85 percent.
Depending on the model, you can set more than one cache point in a list of messages. To find the right cache points for your use case, try different configurations and look at the effect on the reported usage.
Things to know
Amazon Bedrock Intelligent Prompt Routing is available in preview today in US East (N. Virginia) and US West (Oregon) AWS Regions. During the preview, you can use the default prompt routers, and there is no additional cost for using a prompt router. You pay the cost of the selected model. You can use prompt routers with other Amazon Bedrock capabilities such as performing evaluations, using knowledge bases, and configuring agents.
Because the internal model used by the prompt routers needs to understand the complexity of a prompt, intelligent prompt routing currently only supports English language prompts.
Amazon Bedrock support for prompt caching is available in preview in US West (Oregon) for Anthropic’s Claude 3.5 Sonnet V2 and Claude 3.5 Haiku. Prompt caching is also available in US East (N. Virginia) for Amazon Nova Micro, Amazon Nova Lite, and Amazon Nova Pro.
With prompt caching, cache reads receive a 90 percent discount compared to noncached input tokens. There are no additional infrastructure charges for cache storage. When using Anthropic models, you pay an additional cost for tokens written in the cache. There are no additional costs for cache writes with Amazon Nova models. For more information, see Amazon Bedrock pricing.
When using prompt caching, content is cached for up to 5 minutes, with each cache hit resetting this countdown. Prompt caching has been implemented to transparently support cross-Region inference. In this way, your applications can get the cost optimization and latency benefit of prompt caching with the flexibility of cross-Region inference.
These new capabilities make it easier to build cost-effective and high-performing generative AI applications. By intelligently routing requests and caching frequently used content, you can significantly reduce your costs while maintaining and even improving application performance.
To learn more and start using these new capabilities today, visit the Amazon Bedrock documentation and send feedback to AWS re:Post for Amazon Bedrock. You can find deep-dive technical content and discover how our Builder communities are using Amazon Bedrock at community.aws.
— Danilo