Skip to content

Unexpected Delay in Amazon Bedrock Response

0

I am using Amazon Bedrock LLM models via LangChain, specifically with ChatBedrockConverse from the langchain_aws module. My FastAPI application calls the bedrockConverse function using the following setup:

from langchain_aws import ChatBedrockConverse
import boto3
from botocore.config import Config

inference_config = {
    "temperature": chat_request.temperature,
    "max_tokens": chat_request.max_tokens,
    "top_p": chat_request.top_p,
    "model": chat_request.model,  # nova-lite
    "stop": chat_request.stop,
}

ps_application = {"region_name": "us-east-1"}
config = Config(connect_timeout=5, read_timeout=X_second, retries={"max_attempts": 1})
bedrock_runtime = boto3.client("bedrock-runtime", config=config, **ps_application)
llm = ChatBedrockConverse(client=bedrock_runtime, **inference_config)
response = llm.invoke(messages)

In most cases, responses are received within the expected 2-3 seconds, but occasionally, the response time takes X_second + 2-3 seconds (instead expected 2-3 seconds). My understanding is that read_timeout=X_second should specify the maximum time to wait for a response before a timeout exception is raised. However, why it is increasing my response time.

Could you clarify why this happens and whether this behavior is expected?

1 Answer
1

The behavior you're experiencing with occasional longer response times is likely related to how the AWS SDK and Bedrock service handle timeouts and retries.

The read_timeout parameter you've set in the Config object does indeed specify the maximum time to wait for a response. However, this timeout is applied at the socket level for each individual request attempt, not for the entire operation including retries.

When a read timeout occurs, the AWS SDK's default retry mechanism comes into play. Even though you've set "max_attempts" to 1 in your configuration, the SDK may still perform a single retry after the initial attempt times out. This can lead to situations where you experience a delay of approximately X_seconds (your set timeout) plus the time for a successful retry (2-3 seconds in your case).

To address this issue and achieve more consistent response times, you could try the following approaches:

  1. Increase the read_timeout value to accommodate the maximum expected response time from Bedrock. This may help avoid unnecessary timeouts and retries.

  2. Implement a streaming approach using Bedrock's streaming API. This can help you process responses as they come in, potentially reducing overall latency.

  3. Consider using the 'latency' parameter in the PerformanceConfiguration settings for Bedrock models. Setting this to 'optimized' may help improve response times, though it's not guaranteed to solve intermittent delays.

  4. If you're dealing with large prompts or expecting large responses, ensure your timeout values are sufficiently high to accommodate these without triggering unnecessary retries.

Remember that some variability in response times is normal when working with large language models, especially for complex queries or during periods of high service load. Monitoring your application's performance and adjusting configurations accordingly will help optimize your experience with Bedrock.
Sources
Solving 'Read Timed Out' Error and High Latencies in Amazon Bedrock with AWS Java SDK Client | AWS re:Post
Bedrock Converse API - Consistent Timeout Issue with Larger models like Anthropic Sonnet 3.5 and Meta Llama 405B | AWS re:Post
PerformanceConfiguration - Amazon Bedrock

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.