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 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.
      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
    • 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. The async client is used for streaming responses.
      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