Package org.springframework.ai.anthropic
Class AnthropicSetup
java.lang.Object
org.springframework.ai.anthropic.AnthropicSetup
Factory class for creating and configuring Anthropic SDK client instances.
This utility class provides static factory methods for creating both synchronous
(AnthropicClient) and asynchronous (AnthropicClientAsync) clients with
comprehensive configuration support. It handles API key detection from environment
variables and provides sensible defaults for timeouts and retry behavior.
Client Types:
- Synchronous Client: Used for blocking API calls via
setupSyncClient(java.lang.String, java.lang.String, java.time.Duration, java.lang.Integer, java.net.Proxy, java.util.Map<java.lang.String, java.lang.String>) - Asynchronous Client: Used for streaming responses via
setupAsyncClient(java.lang.String, java.lang.String, java.time.Duration, java.lang.Integer, java.net.Proxy, java.util.Map<java.lang.String, java.lang.String>)
Environment Variable Support:
ANTHROPIC_API_KEY- Primary API key for authenticationANTHROPIC_AUTH_TOKEN- Alternative authentication tokenANTHROPIC_BASE_URL- Override the default API endpoint
Default Configuration:
- Timeout: 60 seconds
- Max Retries: 2
- User-Agent:
spring-ai-anthropic-sdk
This class is not intended to be instantiated directly. Use the static factory methods to create client instances.
- Since:
- 2.0.0
- Author:
- Soby Chacko
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic com.anthropic.client.AnthropicClientAsyncsetupAsyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders) Creates an asynchronous Anthropic client with the specified configuration.static com.anthropic.client.AnthropicClientAsyncsetupAsyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates an asynchronous Anthropic client whose underlying OkHttp client is instrumented with Micrometer.static com.anthropic.client.AnthropicClientAsyncsetupAsyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, @Nullable ExecutorService dispatcherExecutor) Creates an asynchronous Anthropic client backed by a caller-supplied dispatcher executor.static com.anthropic.client.AnthropicClientAsyncsetupAsyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, @Nullable ExecutorService dispatcherExecutor, List<AnthropicHttpClientBuilderCustomizer> httpClientCustomizers) Creates an asynchronous Anthropic client backed by a caller-supplied dispatcher executor and with optional HTTP client customizers.static com.anthropic.client.AnthropicClientsetupSyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders) Creates a synchronous Anthropic client with the specified configuration.static com.anthropic.client.AnthropicClientsetupSyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates a synchronous Anthropic client whose underlying OkHttp client is instrumented with Micrometer: each HTTP attempt emits an observation (span + metric +traceparentpropagation), and, when aMeterRegistryis supplied, OkHttp connection-pool gauges are bound to it.static com.anthropic.client.AnthropicClientsetupSyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, @Nullable ExecutorService dispatcherExecutor) Creates a synchronous Anthropic client backed by a caller-supplied dispatcher executor (e.g. one built aroundExecutors.newVirtualThreadPerTaskExecutor()on Java 21+).static com.anthropic.client.AnthropicClientsetupSyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, @Nullable ExecutorService dispatcherExecutor, List<AnthropicHttpClientBuilderCustomizer> httpClientCustomizers) Creates a synchronous Anthropic client backed by a caller-supplied dispatcher executor and with optional HTTP client customizers.
-
Method Details
-
setupSyncClient
public static com.anthropic.client.AnthropicClient setupSyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders) Creates a synchronous Anthropic client with the specified configuration. Delegates tosetupSyncClient(String, String, Duration, Integer, Proxy, Map, ObservationRegistry, MeterRegistry)with ano-opobservation registry and no meter registry — i.e. HTTP-layer observability is disabled.- Parameters:
baseUrl- the base URL for the API (null to use default or environment variable)apiKey- the API key (null to detect from environment)timeout- the request timeout (null to use default of 60 seconds)maxRetries- the maximum number of retries (null to use default of 2)proxy- the proxy to use (null for no proxy)customHeaders- additional HTTP headers to include in requests- Returns:
- a configured Anthropic client
-
setupSyncClient
public static com.anthropic.client.AnthropicClient setupSyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates a synchronous Anthropic client whose underlying OkHttp client is instrumented with Micrometer: each HTTP attempt emits an observation (span + metric +traceparentpropagation), and, when aMeterRegistryis supplied, OkHttp connection-pool gauges are bound to it.- Parameters:
baseUrl- the base URL for the API (null to use default or environment variable)apiKey- the API key (null to detect from environment)timeout- the request timeout (null to use default of 60 seconds)maxRetries- the maximum number of retries (null to use default of 2)proxy- the proxy to use (null for no proxy)customHeaders- additional HTTP headers to include in requestsobservationRegistry- the registry the OkHttp observation interceptor reports to; passObservationRegistry.NOOPto disablemeterRegistry- optional; when supplied, OkHttp connection-pool gauges (active/idle connections) are registered- Returns:
- a configured Anthropic client
- Since:
- 2.0.0
-
setupSyncClient
public static com.anthropic.client.AnthropicClient setupSyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, @Nullable ExecutorService dispatcherExecutor) Creates a synchronous Anthropic client backed by a caller-supplied dispatcher executor (e.g. one built aroundExecutors.newVirtualThreadPerTaskExecutor()on Java 21+). The caller owns the executor's lifecycle; closing the resulting client will not shut it down. SeesetupSyncClient(String, String, Duration, Integer, Proxy, Map, ObservationRegistry, MeterRegistry)for the remaining parameter semantics.- Parameters:
dispatcherExecutor- the OkHttp dispatcher executor; null to use the library-managed default- Returns:
- a configured Anthropic client
- Since:
- 2.0.0
-
setupSyncClient
public static com.anthropic.client.AnthropicClient setupSyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, @Nullable ExecutorService dispatcherExecutor, List<AnthropicHttpClientBuilderCustomizer> httpClientCustomizers) Creates a synchronous Anthropic client backed by a caller-supplied dispatcher executor and with optional HTTP client customizers. SeesetupSyncClient(String, String, Duration, Integer, Proxy, Map, ObservationRegistry, MeterRegistry, ExecutorService)for the remaining parameter semantics.- Parameters:
httpClientCustomizers- customizers applied to the underlying OkHttp client builder after Spring AI's own defaults; useful for registering interceptors (e.g. OAuth2 bearer-token injection) or custom TLS configuration- Returns:
- a configured Anthropic client
- Since:
- 2.0.0
-
setupAsyncClient
public static com.anthropic.client.AnthropicClientAsync setupAsyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders) Creates an asynchronous Anthropic client with the specified configuration. Delegates tosetupAsyncClient(String, String, Duration, Integer, Proxy, Map, ObservationRegistry, MeterRegistry)with ano-opobservation registry and no meter registry — i.e. HTTP-layer observability is disabled.- Parameters:
baseUrl- the base URL for the API (null to use default or environment variable)apiKey- the API key (null to detect from environment)timeout- the request timeout (null to use default of 60 seconds)maxRetries- the maximum number of retries (null to use default of 2)proxy- the proxy to use (null for no proxy)customHeaders- additional HTTP headers to include in requests- Returns:
- a configured async Anthropic client
-
setupAsyncClient
public static com.anthropic.client.AnthropicClientAsync setupAsyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry) Creates an asynchronous Anthropic client whose underlying OkHttp client is instrumented with Micrometer. SeesetupSyncClient(String, String, Duration, Integer, Proxy, Map, ObservationRegistry, MeterRegistry)for parameter semantics.- Parameters:
baseUrl- the base URL for the API (null to use default or environment variable)apiKey- the API key (null to detect from environment)timeout- the request timeout (null to use default of 60 seconds)maxRetries- the maximum number of retries (null to use default of 2)proxy- the proxy to use (null for no proxy)customHeaders- additional HTTP headers to include in requestsobservationRegistry- the registry the OkHttp observation interceptor reports to; passObservationRegistry.NOOPto disablemeterRegistry- optional; when supplied, OkHttp connection-pool gauges are registered- Returns:
- a configured async Anthropic client
- Since:
- 2.0.0
-
setupAsyncClient
public static com.anthropic.client.AnthropicClientAsync setupAsyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, @Nullable ExecutorService dispatcherExecutor) Creates an asynchronous Anthropic client backed by a caller-supplied dispatcher executor. The caller owns the executor's lifecycle; closing the resulting client will not shut it down. SeesetupSyncClient(String, String, Duration, Integer, Proxy, Map, ObservationRegistry, MeterRegistry, ExecutorService)for the dispatcher executor's role.- Parameters:
dispatcherExecutor- the OkHttp dispatcher executor; null to use the library-managed default- Returns:
- a configured async Anthropic client
- Since:
- 2.0.0
-
setupAsyncClient
public static com.anthropic.client.AnthropicClientAsync setupAsyncClient(@Nullable String baseUrl, @Nullable String apiKey, @Nullable Duration timeout, @Nullable Integer maxRetries, @Nullable Proxy proxy, @Nullable Map<String, String> customHeaders, io.micrometer.observation.ObservationRegistry observationRegistry, @Nullable io.micrometer.core.instrument.MeterRegistry meterRegistry, @Nullable ExecutorService dispatcherExecutor, List<AnthropicHttpClientBuilderCustomizer> httpClientCustomizers) Creates an asynchronous Anthropic client backed by a caller-supplied dispatcher executor and with optional HTTP client customizers. SeesetupAsyncClient(String, String, Duration, Integer, Proxy, Map, ObservationRegistry, MeterRegistry, ExecutorService)for the remaining parameter semantics.- Parameters:
httpClientCustomizers- customizers applied to the underlying OkHttp client builder after Spring AI's own defaults; useful for registering interceptors (e.g. OAuth2 bearer-token injection) or custom TLS configuration- Returns:
- a configured async Anthropic client
- Since:
- 2.0.0
-