CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-dev-langchain4j--langchain4j-bedrock

AWS Bedrock integration for LangChain4j enabling Java applications to interact with various LLM providers through a unified interface

Overview
Eval results
Files

setup.mddocs/reference/

AWS Setup

Configure AWS credentials and permissions for Bedrock access.

Maven Dependency

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-bedrock</artifactId>
    <version>1.11.0</version>
</dependency>

AWS Credentials

The integration uses AWS SDK for Java v2, which discovers credentials through the default credential provider chain:

  1. Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
  2. System properties: aws.accessKeyId, aws.secretAccessKey
  3. AWS credentials file: ~/.aws/credentials
  4. IAM instance profile: For EC2 instances
  5. Container credentials: For ECS/Fargate

Environment Variables

export AWS_ACCESS_KEY_ID=your_access_key
export AWS_SECRET_ACCESS_KEY=your_secret_key
export AWS_DEFAULT_REGION=us-east-1

Credentials File

Create ~/.aws/credentials:

[default]
aws_access_key_id = your_access_key
aws_secret_access_key = your_secret_key

Create ~/.aws/config:

[default]
region = us-east-1

Programmatic Credentials

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeClient;

AwsBasicCredentials credentials = AwsBasicCredentials.create("accessKey", "secretKey");

BedrockRuntimeClient client = BedrockRuntimeClient.builder()
    .region(Region.US_EAST_1)
    .credentialsProvider(StaticCredentialsProvider.create(credentials))
    .build();

BedrockChatModel model = BedrockChatModel.builder()
    .client(client)
    .modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
    .build();

IAM Permissions

Basic Permissions (Chat & Embeddings)

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "bedrock:InvokeModel",
        "bedrock:InvokeModelWithResponseStream"
      ],
      "Resource": "arn:aws:bedrock:*::foundation-model/*"
    }
  ]
}

With Guardrails

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "bedrock:InvokeModel",
        "bedrock:InvokeModelWithResponseStream",
        "bedrock:ApplyGuardrail"
      ],
      "Resource": [
        "arn:aws:bedrock:*::foundation-model/*",
        "arn:aws:bedrock:*:*:guardrail/*"
      ]
    }
  ]
}

Specific Model Access

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "bedrock:InvokeModel",
        "bedrock:InvokeModelWithResponseStream"
      ],
      "Resource": [
        "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0",
        "arn:aws:bedrock:us-east-1::foundation-model/amazon.nova-pro-v1:0"
      ]
    }
  ]
}

Regions

AWS Bedrock is available in select regions:

Common regions:

  • us-east-1 (US East, N. Virginia)
  • us-west-2 (US West, Oregon)
  • eu-central-1 (Europe, Frankfurt)
  • ap-southeast-1 (Asia Pacific, Singapore)

Setting region:

import software.amazon.awssdk.regions.Region;

BedrockChatModel model = BedrockChatModel.builder()
    .region(Region.US_EAST_1)
    .modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
    .build();

Model Access

Some models require requesting access in the AWS Bedrock console:

  1. Navigate to AWS Bedrock Console
  2. Go to Model access
  3. Select models and Request access
  4. Wait for approval (usually instant for most models)

Connection Configuration

Timeout

import java.time.Duration;

BedrockChatModel model = BedrockChatModel.builder()
    .modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
    .timeout(Duration.ofSeconds(120))
    .build();

Retries

BedrockChatModel model = BedrockChatModel.builder()
    .modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
    .maxRetries(3)
    .build();

Custom HTTP Client

import software.amazon.awssdk.http.apache.ApacheHttpClient;

BedrockRuntimeClient client = BedrockRuntimeClient.builder()
    .region(Region.US_EAST_1)
    .httpClientBuilder(ApacheHttpClient.builder()
        .maxConnections(100)
        .connectionTimeout(Duration.ofSeconds(10))
    )
    .build();

BedrockChatModel model = BedrockChatModel.builder()
    .client(client)
    .modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
    .build();

Logging

Enable AWS SDK Logging

Add to logback.xml:

<logger name="software.amazon.awssdk" level="DEBUG"/>

Enable Request/Response Logging

BedrockChatModel model = BedrockChatModel.builder()
    .modelId("anthropic.claude-3-5-sonnet-20241022-v2:0")
    .logRequests(true)   // WARNING: Logs entire request including sensitive data
    .logResponses(true)
    .build();

Warning: Request logging will log the entire request body, including messages and potentially sensitive information. Use only in development environments.

Troubleshooting

AccessDeniedException:

  • Check IAM permissions
  • Verify model access has been requested/granted in AWS Console
  • Confirm credentials are valid

ThrottlingException:

  • Implement retry logic with exponential backoff
  • Request quota increase if needed

ResourceNotFoundException:

  • Verify model ID is correct
  • Check model is available in your region
  • Confirm model access has been granted

Related:

  • Error Handling Guide
  • Model Reference
  • Chat Models Quick Start

Install with Tessl CLI

npx tessl i tessl/maven-dev-langchain4j--langchain4j-bedrock@1.11.0

docs

index.md

README.md

tile.json