Class AnthropicSetup

java.lang.Object
org.springframework.ai.anthropic.AnthropicSetup

public final class AnthropicSetup extends Object
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:

Environment Variable Support:

  • ANTHROPIC_API_KEY - Primary API key for authentication
  • ANTHROPIC_AUTH_TOKEN - Alternative authentication token
  • ANTHROPIC_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 Type
    Method
    Description
    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.
    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.
    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.
    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.
    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.
    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 + traceparent propagation), and, when a MeterRegistry is supplied, OkHttp connection-pool gauges are bound to it.
    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 around Executors.newVirtualThreadPerTaskExecutor() on Java 21+).
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 to setupSyncClient(String, String, Duration, Integer, Proxy, Map, ObservationRegistry, MeterRegistry) with a no-op observation 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 + traceparent propagation), and, when a MeterRegistry is 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 requests
      observationRegistry - the registry the OkHttp observation interceptor reports to; pass ObservationRegistry.NOOP to disable
      meterRegistry - 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 around Executors.newVirtualThreadPerTaskExecutor() on Java 21+). The caller owns the executor's lifecycle; closing the resulting client will not shut it down. See setupSyncClient(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. See setupSyncClient(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 to setupAsyncClient(String, String, Duration, Integer, Proxy, Map, ObservationRegistry, MeterRegistry) with a no-op observation 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. See setupSyncClient(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 requests
      observationRegistry - the registry the OkHttp observation interceptor reports to; pass ObservationRegistry.NOOP to disable
      meterRegistry - 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. See setupSyncClient(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. See setupAsyncClient(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