The Anthropic Java SDK provides convenient access to the Anthropic REST API from applications written in Java
The Anthropic Java SDK provides platform adapter modules that enable you to use Claude models on AWS Bedrock and Google Vertex AI, in addition to the direct Anthropic API. Platform adapters handle authentication, request signing, and platform-specific configuration while maintaining the same familiar API interface.
Platform adapters implement the Backend interface to adapt the SDK's core functionality for different cloud platforms:
All backends integrate seamlessly with the SDK's client builders, allowing you to switch platforms with minimal code changes.
The AWS Bedrock adapter enables access to Claude models through Amazon Bedrock, AWS's managed service for foundation models.
The Bedrock adapter requires the anthropic-java-bedrock library dependency in addition to the core SDK.
implementation("com.anthropic:anthropic-java-bedrock:2.11.0")<dependency>
<groupId>com.anthropic</groupId>
<artifactId>anthropic-java-bedrock</artifactId>
<version>2.11.0</version>
</dependency>class BedrockBackend { .api }Location: anthropic-java-bedrock/src/main/kotlin/com/anthropic/bedrock/backends/BedrockBackend.kt
Properties:
val awsCredentialsProvider: AwsCredentialsProvider? - AWS credentials provider for authenticationval apiKey: String? - API key for Bedrock API key authentication (alternative to credentials)val region: Region - AWS region where Bedrock is accessedval awsCredentials: AwsCredentials - Resolved AWS credentials used for request signingStatic Factory Methods:
@JvmStatic fun builder(): Builder { .api }Creates a new builder for constructing a BedrockBackend.
@JvmStatic fun fromEnv(): BedrockBackend { .api }Creates a BedrockBackend configured from environment variables and AWS default provider chains.
The Bedrock adapter supports two authentication methods:
Standard AWS credentials using access keys and secret keys. Credentials can be provided through:
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)~/.aws/credentials)AwsCredentialsProvider implementationsBedrock API keys provide an alternative authentication method. API keys can be:
AWS_BEARER_TOKEN_BEDROCK environment variableBedrockBackend.Builder.apiKey()API keys take precedence over credentials when both are available in the environment.
Use BedrockBackend.fromEnv() to automatically resolve credentials and region:
import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.fromEnv())
.build();This automatically:
AWS_BEARER_TOKEN_BEDROCK if available (takes precedence over credentials)Configure credentials and region explicitly:
import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.regions.Region;
AwsCredentials awsCredentials = AwsBasicCredentials.create(
System.getenv("AWS_ACCESS_KEY_ID"),
System.getenv("AWS_SECRET_ACCESS_KEY"));
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.builder()
.awsCredentials(awsCredentials)
.region(Region.US_EAST_1)
.build())
.build();Use a custom AWS credentials provider with additional features:
import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
AwsCredentialsProvider awsCredentialsProvider =
DefaultCredentialsProvider.builder()
.asyncCredentialUpdateEnabled(true)
.build();
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.builder()
.fromEnv(awsCredentialsProvider)
.build())
.build();Configure using an API key instead of AWS credentials:
import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import software.amazon.awssdk.regions.Region;
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.builder()
.apiKey(myApiKey)
.region(Region.US_EAST_1)
.build())
.build();class BedrockBackend.Builder { .api }Builder Methods:
fun fromEnv(awsCredentialsProvider: AwsCredentialsProvider? = null): Builder { .api }Load configuration from environment variables. If awsCredentialsProvider is provided, uses it for credentials; otherwise uses default provider chain. Region is resolved from AWS default region provider chain. API key is loaded from AWS_BEARER_TOKEN_BEDROCK if available.
fun awsCredentialsProvider(provider: AwsCredentialsProvider): Builder { .api }Set the AWS credentials provider. Cannot be used with apiKey().
fun awsCredentials(credentials: AwsCredentials): Builder { .api }Set AWS credentials directly. Cannot be used with apiKey() or awsCredentialsProvider().
fun apiKey(apiKey: String): Builder { .api }Set the Bedrock API key for authentication. Cannot be used with awsCredentials() or awsCredentialsProvider().
fun region(region: Region): Builder { .api }Set the AWS region. Required for both credential-based and API key-based authentication.
fun build(): BedrockBackend { .api }Build the BedrockBackend instance. Throws if required configuration is missing or conflicting.
AWS_ACCESS_KEY_ID - AWS access key ID (for credentials-based auth)AWS_SECRET_ACCESS_KEY - AWS secret access key (for credentials-based auth)AWS_SESSION_TOKEN - AWS session token (optional, for temporary credentials)AWS_BEARER_TOKEN_BEDROCK - Bedrock API key (for API key-based auth)AWS_REGION - AWS region (used by default region provider chain)AWS_DEFAULT_REGION - Alternative AWS region variableWhen using fromEnv() without a custom provider, credentials are resolved in this order:
aws.accessKeyId, aws.secretAccessKey)AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)~/.aws/credentials)See AWS documentation for complete details.
import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.fromEnv())
.build();
MessageCreateParams params = MessageCreateParams.builder()
.maxTokens(1024L)
.addUserMessage("Hello, Claude on Bedrock")
.model(Model.CLAUDE_SONNET_4_20250514)
.build();
Message message = client.messages().create(params);
System.out.println(message.content().get(0).asText().text());import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.regions.Region;
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.builder()
.awsCredentials(AwsBasicCredentials.create(
"YOUR_ACCESS_KEY",
"YOUR_SECRET_KEY"))
.region(Region.US_WEST_2)
.build())
.build();import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import software.amazon.awssdk.regions.Region;
// From environment variable AWS_BEARER_TOKEN_BEDROCK
AnthropicClient client1 = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.fromEnv())
.build();
// Explicit API key
AnthropicClient client2 = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.builder()
.apiKey(System.getenv("BEDROCK_API_KEY"))
.region(Region.US_EAST_1)
.build())
.build();import com.anthropic.bedrock.backends.BedrockBackend;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
// Use specific AWS profile
AwsCredentialsProvider provider = ProfileCredentialsProvider.builder()
.profileName("production")
.build();
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.builder()
.fromEnv(provider)
.build())
.build();The Bedrock backend currently does not support:
messages().countTokens() method is not supportedAll other API functionality (messages, streaming, tools, prompt caching) works identically to the direct Anthropic API.
The Google Vertex AI adapter enables access to Claude models through Google Cloud Vertex AI, Google Cloud's platform for ML models.
The Vertex AI adapter requires the anthropic-java-vertex library dependency in addition to the core SDK.
implementation("com.anthropic:anthropic-java-vertex:2.11.0")<dependency>
<groupId>com.anthropic</groupId>
<artifactId>anthropic-java-vertex</artifactId>
<version>2.11.0</version>
</dependency>class VertexBackend { .api }Location: anthropic-java-vertex/src/main/kotlin/com/anthropic/vertex/backends/VertexBackend.kt
Properties:
val googleCredentials: GoogleCredentials - Google OAuth2 credentials for authenticationval region: String - Google Cloud region where Vertex AI is accessedval project: String - Google Cloud project IDStatic Factory Methods:
@JvmStatic fun builder(): Builder { .api }Creates a new builder for constructing a VertexBackend.
@JvmStatic fun fromEnv(): VertexBackend { .api }Creates a VertexBackend configured from environment variables and Google Application Default Credentials.
The Vertex AI adapter uses Google OAuth2 credentials for authentication.
The primary authentication method uses Application Default Credentials, which automatically discovers credentials in this order:
GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to a service account key filegcloud auth application-default loginFor service accounts, credentials are loaded from a JSON key file:
{
"type": "service_account",
"project_id": "your-project-id",
"private_key_id": "key-id",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "service-account@your-project.iam.gserviceaccount.com",
"client_id": "123456789",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token"
}Use VertexBackend.fromEnv() to automatically resolve credentials, region, and project:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.fromEnv())
.build();This automatically:
CLOUD_ML_REGION environment variableANTHROPIC_VERTEX_PROJECT_ID environment variableConfigure credentials, region, and project explicitly:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
String accessToken = System.getenv("GOOGLE_ACCESS_TOKEN");
String project = System.getenv("ANTHROPIC_VERTEX_PROJECT_ID");
GoogleCredentials googleCredentials = GoogleCredentials.create(
AccessToken.newBuilder().setTokenValue(accessToken).build());
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.builder()
.googleCredentials(googleCredentials)
.region("us-central1")
.project(project)
.build())
.build();Load credentials from a service account key file:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.FileInputStream;
GoogleCredentials credentials = GoogleCredentials.fromStream(
new FileInputStream("/path/to/service-account-key.json"));
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.builder()
.googleCredentials(credentials)
.region("us-central1")
.project("my-project-id")
.build())
.build();class VertexBackend.Builder { .api }Builder Methods:
fun fromEnv(): Builder { .api }Load configuration from environment variables. Uses Google Application Default Credentials for authentication. Region is loaded from CLOUD_ML_REGION. Project ID is loaded from ANTHROPIC_VERTEX_PROJECT_ID.
fun googleCredentials(credentials: GoogleCredentials): Builder { .api }Set the Google OAuth2 credentials for authentication.
fun region(region: String): Builder { .api }Set the Google Cloud region (e.g., "us-central1", "europe-west1").
fun project(project: String): Builder { .api }Set the Google Cloud project ID.
fun build(): VertexBackend { .api }Build the VertexBackend instance. Throws if required configuration is missing.
GOOGLE_APPLICATION_CREDENTIALS - Path to service account JSON key file (used by ADC)CLOUD_ML_REGION - Google Cloud region for Vertex AIANTHROPIC_VERTEX_PROJECT_ID - Google Cloud project IDGOOGLE_CLOUD_PROJECT - Alternative project ID variable (lower priority)import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.models.messages.Message;
import com.anthropic.models.messages.MessageCreateParams;
import com.anthropic.models.messages.Model;
import com.anthropic.vertex.backends.VertexBackend;
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.fromEnv())
.build();
MessageCreateParams params = MessageCreateParams.builder()
.maxTokens(1024L)
.addUserMessage("Hello, Claude on Vertex AI")
.model(Model.CLAUDE_SONNET_4_20250514)
.build();
Message message = client.messages().create(params);
System.out.println(message.content().get(0).asText().text());import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
import com.google.auth.oauth2.GoogleCredentials;
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.builder()
.googleCredentials(credentials)
.region("us-east4")
.project("my-gcp-project")
.build())
.build();import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
import com.google.auth.oauth2.ServiceAccountCredentials;
import java.io.FileInputStream;
ServiceAccountCredentials credentials = ServiceAccountCredentials.fromStream(
new FileInputStream("/path/to/key.json"));
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.builder()
.googleCredentials(credentials)
.region("us-central1")
.project("my-project")
.build())
.build();import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
// Requires: gcloud auth application-default login
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.fromEnv())
.build();The Vertex AI backend currently does not support:
Token counting is supported on Vertex AI (unlike Bedrock). All other API functionality works identically to the direct Anthropic API.
Platform adapters implement a common Backend interface that can be used to create custom platform integrations.
interface Backend { .api }Location: anthropic-java-core/src/main/kotlin/com/anthropic/core/Backend.kt
The Backend interface defines methods for:
To create a custom backend for a different platform:
import com.anthropic.core.Backend;
class CustomBackend implements Backend {
@Override
public String baseUrl() {
return "https://custom-platform.example.com";
}
// Implement other Backend methods for authentication, signing, etc.
}
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(new CustomBackend())
.build();All backends integrate with the client using the .backend() builder method:
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
// Direct API (default)
AnthropicClient directClient = AnthropicOkHttpClient.fromEnv();
// AWS Bedrock
AnthropicClient bedrockClient = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.fromEnv())
.build();
// Google Vertex AI
AnthropicClient vertexClient = AnthropicOkHttpClient.builder()
.backend(VertexBackend.fromEnv())
.build();Once configured, the client API is identical regardless of backend:
MessageCreateParams params = MessageCreateParams.builder()
.maxTokens(1024L)
.addUserMessage("Hello, Claude")
.model(Model.CLAUDE_SONNET_4_20250514)
.build();
Message message = client.messages().create(params);Using fromEnv() methods provides automatic credential resolution:
AWS Bedrock:
AWS_BEARER_TOKEN_BEDROCK environment variableGoogle Vertex AI:
CLOUD_ML_REGION environment variableANTHROPIC_VERTEX_PROJECT_ID environment variableFor more control, resolve credentials independently and pass them to the builder:
AWS Bedrock:
AwsCredentialsProvider provider = ProfileCredentialsProvider.builder()
.profileName("production")
.build();
BedrockBackend backend = BedrockBackend.builder()
.awsCredentialsProvider(provider)
.region(Region.US_WEST_2)
.build();Google Vertex AI:
GoogleCredentials credentials = ServiceAccountCredentials.fromStream(
new FileInputStream(keyPath));
VertexBackend backend = VertexBackend.builder()
.googleCredentials(credentials)
.region("us-central1")
.project(projectId)
.build();Never hardcode credentials:
// DON'T DO THIS
AwsCredentials creds = AwsBasicCredentials.create("AKIAIOSFODNN7EXAMPLE", "secret");Use environment variables or credential providers:
// DO THIS
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.fromEnv())
.build();Store service account keys securely:
Reuse credentials providers:
// Create once, reuse for multiple backends
AwsCredentialsProvider provider = DefaultCredentialsProvider.builder()
.asyncCredentialUpdateEnabled(true)
.build();
BedrockBackend backend1 = BedrockBackend.builder()
.fromEnv(provider)
.build();Enable credential caching:
// Google credentials with refreshed tokens
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
.createScoped("https://www.googleapis.com/auth/cloud-platform");Handle authentication errors:
try {
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.fromEnv())
.build();
Message message = client.messages().create(params);
} catch (UnauthorizedException e) {
// Handle invalid credentials
System.err.println("Authentication failed: " + e.getMessage());
} catch (PermissionDeniedException e) {
// Handle insufficient permissions
System.err.println("Insufficient permissions: " + e.getMessage());
}Handle region/project errors:
try {
VertexBackend backend = VertexBackend.fromEnv();
} catch (IllegalStateException e) {
// Handle missing CLOUD_ML_REGION or ANTHROPIC_VERTEX_PROJECT_ID
System.err.println("Configuration error: " + e.getMessage());
}Reuse client instances:
// Create once, reuse throughout application
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BedrockBackend.fromEnv())
.build();
// Use for multiple requests
Message msg1 = client.messages().create(params1);
Message msg2 = client.messages().create(params2);Close clients when done:
try (AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(VertexBackend.fromEnv())
.build()) {
Message message = client.messages().create(params);
}Use dependency injection for backends:
public class MyService {
private final AnthropicClient client;
public MyService(Backend backend) {
this.client = AnthropicOkHttpClient.builder()
.backend(backend)
.build();
}
}
// Production
MyService prodService = new MyService(BedrockBackend.fromEnv());
// Testing
MyService testService = new MyService(new MockBackend());Choose the appropriate platform based on your needs:
Use Direct API when:
Use AWS Bedrock when:
Use Google Vertex AI when:
Use configuration classes:
public class BackendConfig {
public static Backend createBackend(String platform) {
switch (platform.toLowerCase()) {
case "bedrock":
return BedrockBackend.fromEnv();
case "vertex":
return VertexBackend.fromEnv();
case "direct":
default:
return null; // Uses default direct API
}
}
}
// Configure via environment variable
String platform = System.getenv("CLAUDE_PLATFORM");
AnthropicClient client = AnthropicOkHttpClient.builder()
.backend(BackendConfig.createBackend(platform))
.build();For more information on client configuration and usage patterns, see Client Setup.
Install with Tessl CLI
npx tessl i tessl/maven-com-anthropic--anthropic-java@2.11.1