Skip to content

LLM calls hangs and succeed on the second attempt

0

We are experiencing an intermittent issue where some AWS Bedrock Runtime API calls fail to complete on the first attempt and must wait for the full configured read_timeout duration (currently 45 seconds) before boto3 automatically retries and succeeds immediately (in 1–5 seconds).

We use ChatBedrockConverseAsync methods (ainvoke, astream), but the boto3 Bedrock client is synchronous only.

Unfortunately, the issue reproduction is non-deterministic — it cannot be reproduced on demand. It occurs about 10% of the time during normal traffic. No specific input triggers the issue reliably.

This is not a timeout configuration issue — the exact same input prompts succeed in 2–5 seconds about 90–95% of the time, but 5–10% of requests require the full timeout + retry cycle.

The pattern strongly suggests that Bedrock is accepting the request but not processing or responding on the first attempt, forcing the client to wait for the timeout before the retry succeeds.


Application Architecture Overview

Application Type: AI-powered RAG (Retrieval-Augmented Generation) system.

Technology Stack

  • Runtime: Python 3.11
  • Framework: FastAPI (async web framework)
  • AI Orchestration: LangGraph (state machine for multi-step AI workflows)
  • LLM Integration: LangChain AWS (langchain-aws package)
  • Deployment: Docker containers on AWS EKS

Docker Configuration

Gunicorn (WSGI → ASGI bridge)

CMD ["gunicorn", "app.main:app", \
     "--workers", "8", \                    # 8 worker processes
     "--worker-class", "uvicorn.workers.UvicornWorker", \
     "--bind", "0.0.0.0:8000", \
     "--timeout", "300", \                  # Worker timeout
     "--graceful-timeout", "30"]

Bedrock Runtime boto3 Client Configuration

Configuration Rationale:

SettingValueDescription
connect_timeout3sNetwork-level timeout for TCP handshake
read_timeout45sApplication-level timeout for Bedrock to return first response byte
max_attempts2Allow one automatic retry on timeout
max_pool_connections50Support high concurrency per worker

The Bedrock Runtime client is initialized in the FastAPI Application Lifecycle.


Models Used

  • "us.anthropic.claude-sonnet-4-20250514-v1:0"
  • "us.anthropic.claude-3-7-sonnet-20250219-v1:0"
  • "us.anthropic.claude-3-5-haiku-20241022-v1:0"

Average input tokens per call: ~3500 Max expected tokens per minute (Sonnet 4): ~300K Peak request rate: ~100 req/min


Hung Request Characteristics

Hung requests have no distinguishing characteristics:

  • Small prompts (~1K tokens) hang just as often as large ones (~5K tokens)
  • The first message in a conversation hangs as often as the 20th
  • Simple queries hang as often as complex ones

Additional Information

  • AWS Region: us-east-1
2 Answers
0

Hello,

I understand that you are experiencing an intermittent issue where some AWS Bedrock Runtime API calls fail to complete on the first attempt and must wait for the full configured read_timeout duration (currently 45 seconds) before boto3 automatically retries and succeeds immediately (in 1–5 seconds).

The issue you're experiencing with AWS Bedrock Runtime API calls appears to be related to how the boto3 client handles timeouts and retries when working with large language models. This is a known behavior pattern with Bedrock and other LLM services.

Additionally, please note that Bedrock is an on-demand service[1], and some variation in response times can occur based on overall service load, routing decisions, and customer demand for the given model at that timeframe.

As a potential workaround, you might experiment with different read_timeout values. For Lambda functions, it's recommended to set the boto3 read_timeout to a lower value than the function execution timeout, giving you time to take additional action.

To investigate it further, please reach out to AWS Support [2] with your detailed query and following details, and we will be happy to assist you further.

  1. Please mention some specific timestamps when you observed this issue for the mentioned models
  2. Please share the model invocation logs [3].
  3. Request IDs for a few specific incidents:
    • The original request ID that timed out
    • The successful retry request ID

—References—

[1] https://aws.amazon.com/bedrock/sla/

[2] Creating support cases and case management: https://docs.aws.amazon.com/awssupport/latest/user/case-management.html#creating-a-support-case

[3] https://docs.aws.amazon.com/bedrock/latest/userguide/model-invocation-logging.html

AWS

answered a year ago

EXPERT

reviewed 10 months ago

-1

The issue you're experiencing with AWS Bedrock Runtime API calls appears to be related to how the boto3 client handles timeouts and retries when working with large language models. This is a known behavior pattern with Bedrock and other LLM services.

What's happening is that occasionally (in your case about 5-10% of the time), the Bedrock service accepts your request but doesn't process or respond within the expected timeframe. This forces your boto3 client to wait for the full read_timeout period (45 seconds) before automatically retrying the request, at which point it succeeds quickly (1-5 seconds).

This behavior is consistent with how the AWS SDK handles timeouts. When a read timeout occurs, the SDK's retry mechanism comes into play. Even with your configuration of max_attempts=2, the SDK will perform a single retry after the initial attempt times out. This explains why you see a pattern of waiting for the full timeout period followed by a quick successful response.

Some potential approaches to address this issue:

  1. Implement streaming responses: Amazon Bedrock provides the ability to stream responses using InvokeModelWithResponseStream, which allows you to process and consume generated text in chunks as it becomes available. This enables faster response to the client and allows at least partial responses even if timeouts occur.

  2. Adjust timeout configuration: While you've already set reasonable timeouts, you might experiment with different read_timeout values. For Lambda functions, it's recommended to set the boto3 read_timeout to a lower value than the function execution timeout, giving you time to take additional action.

  3. Implement custom retry logic: Instead of relying solely on boto3's built-in retry mechanism, you could implement your own retry logic with exponential backoff for specific error conditions.

  4. Monitor and optimize: Use CloudWatch Metrics to track your API call patterns and set up alerts for when you're approaching rate limits. This might help identify if the issue is related to service quotas.

  5. Consider cross-region inference: If the issue persists, you might want to implement load balancing to another region with cross-region inference capabilities.

The non-deterministic nature of the issue is typical for LLM services, which can experience variable processing times based on model complexity, service load, and other factors. Since your application is already using async patterns with FastAPI and LangChain, you're on the right track for handling these types of intermittent delays.
Sources
Designing Serverless Integration Patterns for Large Language Models (LLMs) | AWS Compute Blog
Unexpected Delay in Amazon Bedrock Response | AWS re:Post
504 Gateway Time-out nginx - only using EB to call Bedrock, not using local Flask server | AWS re:Post
Solving 'Read Timed Out' Error and High Latencies in Amazon Bedrock with AWS Java SDK Client | AWS re:Post

answered a year 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.