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.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.
-
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
-