Class ClientOptions.Builder
-
- All Implemented Interfaces:
public final class ClientOptions.BuilderA builder for ClientOptions.
-
-
Method Summary
Modifier and Type Method Description final ClientOptions.BuilderhttpClient(HttpClient httpClient)The HTTP client to use in the SDK. final ClientOptions.Builderinterceptors(List<Interceptor> interceptors)Wraps the HTTP client using the given interceptors. final ClientOptions.BuildercheckJacksonVersionCompatibility(Boolean checkJacksonVersionCompatibility)Whether to throw an exception if any of the Jackson versions detected at runtime are incompatible with the SDK's minimum supported Jackson version (2.13.4). final ClientOptions.BuilderjsonMapper(JsonMapper jsonMapper)The Jackson JSON mapper to use for serializing and deserializing JSON. final ClientOptions.BuilderstreamHandlerExecutor(Executor streamHandlerExecutor)The executor to use for running AsyncStreamResponse.Handler callbacks. final ClientOptions.Buildersleeper(Sleeper sleeper)The interface to use for delaying execution, like during retries. final ClientOptions.Builderclock(Clock clock)The clock to use for operations that require timing, like retries. final ClientOptions.BuilderbaseUrl(String baseUrl)The base URL to use for every request. final ClientOptions.BuilderbaseUrl(Optional<String> baseUrl)Alias for calling Builder.baseUrl with baseUrl.orElse(null).final ClientOptions.Builderheaders(Headers headers)final ClientOptions.Builderheaders(Map<String, Iterable<String>> headers)final ClientOptions.BuilderqueryParams(QueryParams queryParams)final ClientOptions.BuilderqueryParams(Map<String, Iterable<String>> queryParams)final ClientOptions.BuilderresponseValidation(Boolean responseValidation)Whether to call validateon every response before returning it.final ClientOptions.Buildertimeout(Timeout timeout)Sets the maximum time allowed for various parts of an HTTP call's lifecycle, excluding retries. final ClientOptions.Buildertimeout(Duration timeout)Sets the maximum time allowed for a complete HTTP call, not including retries. final Timeouttimeout()final ClientOptions.BuildermaxRetries(Integer maxRetries)The maximum number of times to retry failed requests, with a short exponential backoff between requests. final ClientOptions.BuilderlogLevel(LogLevel logLevel)The level at which to log request and response information. final ClientOptions.BuilderopenTelemetry(OpenTelemetryOptions openTelemetry)OpenTelemetry tracing of this client's API calls. final ClientOptions.BuilderopenTelemetry(Boolean enabled)Enables or disables the spans of this client's API calls. final ClientOptions.BuilderopenTelemetryProviderName(String openTelemetryProviderName)The gen_ai.provider.nameattribute of this client's spans.final ClientOptions.BuilderopenTelemetryPlatform(String openTelemetryPlatform)The platform in the instrumentation scope of this client's spans. final ClientOptions.BuilderopenTelemetryPlatform(Optional<String> openTelemetryPlatform)Alias for calling Builder.openTelemetryPlatform with openTelemetryPlatform.orElse(null).final ClientOptions.BuilderwebhookKey(String webhookKey)final ClientOptions.BuilderwebhookKey(Optional<String> webhookKey)Alias for calling Builder.webhookKey with webhookKey.orElse(null).final ClientOptions.BuilderaddInterceptor(Interceptor interceptor)Adds a single Interceptor to interceptors. final ClientOptions.BuilderputHeader(String name, String value)final ClientOptions.BuilderputHeaders(String name, Iterable<String> values)final ClientOptions.BuilderputAllHeaders(Headers headers)final ClientOptions.BuilderputAllHeaders(Map<String, Iterable<String>> headers)final ClientOptions.BuilderreplaceHeaders(String name, String value)final ClientOptions.BuilderreplaceHeaders(String name, Iterable<String> values)final ClientOptions.BuilderreplaceAllHeaders(Headers headers)final ClientOptions.BuilderreplaceAllHeaders(Map<String, Iterable<String>> headers)final ClientOptions.BuilderremoveHeaders(String name)final ClientOptions.BuilderremoveAllHeaders(Set<String> names)final ClientOptions.BuilderputQueryParam(String key, String value)final ClientOptions.BuilderputQueryParams(String key, Iterable<String> values)final ClientOptions.BuilderputAllQueryParams(QueryParams queryParams)final ClientOptions.BuilderputAllQueryParams(Map<String, Iterable<String>> queryParams)final ClientOptions.BuilderreplaceQueryParams(String key, String value)final ClientOptions.BuilderreplaceQueryParams(String key, Iterable<String> values)final ClientOptions.BuilderreplaceAllQueryParams(QueryParams queryParams)final ClientOptions.BuilderreplaceAllQueryParams(Map<String, Iterable<String>> queryParams)final ClientOptions.BuilderremoveQueryParams(String key)final ClientOptions.BuilderremoveAllQueryParams(Set<String> keys)final ClientOptions.BuilderfromEnv()Updates configuration using system properties and environment variables. final ClientOptionsbuild()Returns an immutable instance of ClientOptions. -
-
Method Detail
-
httpClient
final ClientOptions.Builder httpClient(HttpClient httpClient)
The HTTP client to use in the SDK.
Use the one published in
anthropic-java-client-okhttpor implement your own.This class takes ownership of the client and closes it when closed.
-
interceptors
final ClientOptions.Builder interceptors(List<Interceptor> interceptors)
Wraps the HTTP client using the given interceptors.
The interceptors are applied to every request, including retries, in order.
Each interceptor receives an HTTP client that delegates to the next interceptor, and the last interceptor receives the underlying HTTP client.
Interceptors observe requests in the canonical Anthropic API shape, with the client's default headers and first-party credentials (
Authorization) already applied, and they observe responses normalized to the Anthropic shape (e.g. Bedrock's binary event stream is decoded to SSE). Backend-specific adaptation — URL rewriting, request signing (e.g. AWS SigV4), and backend credentials — happens below the interceptors and is not visible to them.Because credentials are applied above the interceptors, an interceptor that wants to replace them must use
replaceHeaders, notputHeader(which would append a duplicate header). Interceptors that forward request data to external systems must redact these headers.
-
checkJacksonVersionCompatibility
final ClientOptions.Builder checkJacksonVersionCompatibility(Boolean checkJacksonVersionCompatibility)
Whether to throw an exception if any of the Jackson versions detected at runtime are incompatible with the SDK's minimum supported Jackson version (2.13.4).
Defaults to true. Use extreme caution when disabling this option. There is no guarantee that the SDK will work correctly when using an incompatible Jackson version.
-
jsonMapper
final ClientOptions.Builder jsonMapper(JsonMapper jsonMapper)
The Jackson JSON mapper to use for serializing and deserializing JSON.
Defaults to com.anthropic.core.jsonMapper. The default is usually sufficient and rarely needs to be overridden.
-
streamHandlerExecutor
final ClientOptions.Builder streamHandlerExecutor(Executor streamHandlerExecutor)
The executor to use for running AsyncStreamResponse.Handler callbacks.
Defaults to a dedicated cached thread pool.
This class takes ownership of the executor and shuts it down, if possible, when closed.
-
sleeper
final ClientOptions.Builder sleeper(Sleeper sleeper)
The interface to use for delaying execution, like during retries.
This is primarily useful for using fake delays in tests.
Defaults to real execution delays.
This class takes ownership of the sleeper and closes it when closed.
-
clock
final ClientOptions.Builder clock(Clock clock)
The clock to use for operations that require timing, like retries.
This is primarily useful for using a fake clock in tests.
Defaults to Clock.systemUTC.
-
baseUrl
final ClientOptions.Builder baseUrl(String baseUrl)
The base URL to use for every request.
Defaults to the production environment:
https://api.anthropic.com.
-
baseUrl
final ClientOptions.Builder baseUrl(Optional<String> baseUrl)
Alias for calling Builder.baseUrl with
baseUrl.orElse(null).
-
headers
final ClientOptions.Builder headers(Headers headers)
-
queryParams
final ClientOptions.Builder queryParams(QueryParams queryParams)
-
queryParams
final ClientOptions.Builder queryParams(Map<String, Iterable<String>> queryParams)
-
responseValidation
final ClientOptions.Builder responseValidation(Boolean responseValidation)
Whether to call
validateon every response before returning it.Setting this to
trueis not forwards compatible with new types from the API for existing fields.Defaults to false, which means the shape of the response will not be validated upfront. Instead, validation will only occur for the parts of the response that are accessed.
-
timeout
final ClientOptions.Builder timeout(Timeout timeout)
Sets the maximum time allowed for various parts of an HTTP call's lifecycle, excluding retries.
Defaults to Timeout.default.
-
timeout
final ClientOptions.Builder timeout(Duration timeout)
Sets the maximum time allowed for a complete HTTP call, not including retries.
See Timeout.request for more details.
For fine-grained control, pass a Timeout object.
-
maxRetries
final ClientOptions.Builder maxRetries(Integer maxRetries)
The maximum number of times to retry failed requests, with a short exponential backoff between requests.
Only the following error types are retried:
Connection errors (for example, due to a network connectivity problem)
408 Request Timeout
409 Conflict
429 Rate Limit
5xx Internal
The API may also explicitly instruct the SDK to retry or not retry a request.
Defaults to 2.
-
logLevel
final ClientOptions.Builder logLevel(LogLevel logLevel)
The level at which to log request and response information.
fromEnv will set the level from environment variables. See LogLevel.fromEnv.
Defaults to LogLevel.fromEnv.
-
openTelemetry
final ClientOptions.Builder openTelemetry(OpenTelemetryOptions openTelemetry)
OpenTelemetry tracing of this client's API calls. Each call emits a span when the application has registered a tracer provider with GlobalOpenTelemetry, or has set OpenTelemetryOptions.tracerProvider.
fromEnv sets the options that a system property or an environment variable sets. See OpenTelemetryOptions.fromEnv.
-
openTelemetry
final ClientOptions.Builder openTelemetry(Boolean enabled)
Enables or disables the spans of this client's API calls.
A short form of openTelemetry with OpenTelemetryOptions.ofEnabled.
-
openTelemetryProviderName
final ClientOptions.Builder openTelemetryProviderName(String openTelemetryProviderName)
The
gen_ai.provider.nameattribute of this client's spans. The client builder sets it from its backend. See com.anthropic.backends.Backend.openTelemetryProviderName.
-
openTelemetryPlatform
final ClientOptions.Builder openTelemetryPlatform(String openTelemetryPlatform)
The platform in the instrumentation scope of this client's spans. The client of another platform sets the platform's name, and its spans then have the scope
com.anthropic.sdk.java.<platform>, in place ofcom.anthropic.sdk.java.
-
openTelemetryPlatform
final ClientOptions.Builder openTelemetryPlatform(Optional<String> openTelemetryPlatform)
Alias for calling Builder.openTelemetryPlatform with
openTelemetryPlatform.orElse(null).
-
webhookKey
final ClientOptions.Builder webhookKey(String webhookKey)
-
webhookKey
final ClientOptions.Builder webhookKey(Optional<String> webhookKey)
Alias for calling Builder.webhookKey with
webhookKey.orElse(null).
-
addInterceptor
final ClientOptions.Builder addInterceptor(Interceptor interceptor)
Adds a single Interceptor to interceptors.
-
putHeader
final ClientOptions.Builder putHeader(String name, String value)
-
putHeaders
final ClientOptions.Builder putHeaders(String name, Iterable<String> values)
-
putAllHeaders
final ClientOptions.Builder putAllHeaders(Headers headers)
-
putAllHeaders
final ClientOptions.Builder putAllHeaders(Map<String, Iterable<String>> headers)
-
replaceHeaders
final ClientOptions.Builder replaceHeaders(String name, String value)
-
replaceHeaders
final ClientOptions.Builder replaceHeaders(String name, Iterable<String> values)
-
replaceAllHeaders
final ClientOptions.Builder replaceAllHeaders(Headers headers)
-
replaceAllHeaders
final ClientOptions.Builder replaceAllHeaders(Map<String, Iterable<String>> headers)
-
removeHeaders
final ClientOptions.Builder removeHeaders(String name)
-
removeAllHeaders
final ClientOptions.Builder removeAllHeaders(Set<String> names)
-
putQueryParam
final ClientOptions.Builder putQueryParam(String key, String value)
-
putQueryParams
final ClientOptions.Builder putQueryParams(String key, Iterable<String> values)
-
putAllQueryParams
final ClientOptions.Builder putAllQueryParams(QueryParams queryParams)
-
putAllQueryParams
final ClientOptions.Builder putAllQueryParams(Map<String, Iterable<String>> queryParams)
-
replaceQueryParams
final ClientOptions.Builder replaceQueryParams(String key, String value)
-
replaceQueryParams
final ClientOptions.Builder replaceQueryParams(String key, Iterable<String> values)
-
replaceAllQueryParams
final ClientOptions.Builder replaceAllQueryParams(QueryParams queryParams)
-
replaceAllQueryParams
final ClientOptions.Builder replaceAllQueryParams(Map<String, Iterable<String>> queryParams)
-
removeQueryParams
final ClientOptions.Builder removeQueryParams(String key)
-
removeAllQueryParams
final ClientOptions.Builder removeAllQueryParams(Set<String> keys)
-
fromEnv
final ClientOptions.Builder fromEnv()
Updates configuration using system properties and environment variables.
See this table for the available options:
System properties take precedence over environment variables.
Also sets the options of
openTelemetrythat a system property or an environment variable sets, and leaves its other options as they are. See OpenTelemetryOptions.fromEnv.
-
build
final ClientOptions build()
Returns an immutable instance of ClientOptions.
Further updates to this Builder will not mutate the returned instance.
The following fields are required:
.httpClient()
-
-
-
-