Class AnthropicClient.Builder<T extends AnthropicClient, B extends AnthropicClient.Builder<T,B>>
- Type Parameters:
T- the type ofAnthropicClientbeing builtB- the concrete builder type (for method chaining)
- Direct Known Subclasses:
DefaultAnthropicClient.Builder
- Enclosing class:
AnthropicClient
AnthropicClient instances.
Provides a fluent API for configuring HTTP client settings, authentication, API versioning, and logging options. Uses the curiously recurring template pattern (CRTP) to ensure method chaining returns the correct builder subtype.
Required Configuration
baseUrl(String)- The Anthropic API base URLapiKey(String)- The API key for authenticationversion(String)- The API version (e.g., "2023-06-01")
Optional Configuration
httpClientBuilder(HttpClientBuilder)- Custom HTTP client buildertimeout(Duration)- Connection and read timeout (defaults: 15s connect, 60s read)beta(String)- Beta features header for experimental capabilitieslogRequests(Boolean)- Enable raw request logginglogResponses(Boolean)- Enable raw response logginglogger(Logger)- Custom logger instance
Usage Example
AnthropicClient client = DefaultAnthropicClient.builder()
.baseUrl("https://api.anthropic.com/v1")
.apiKey("your-api-key")
.version("2023-06-01")
.timeout(Duration.ofSeconds(30))
.logRequests(true)
.logResponses(true)
.build();
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionorg.slf4j.Logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets the API key for authentication.Sets the base URL for the Anthropic API.Sets the beta features header for accessing experimental API capabilities.abstract Tbuild()Builds and returns a newAnthropicClientinstance.httpClientBuilder(HttpClientBuilder httpClientBuilder) Sets a custom HTTP client builder for configuring the underlying HTTP client.logger(org.slf4j.Logger logger) Sets a custom logger for request and response logging.Enables logging of raw HTTP requests.logRequests(Boolean logRequests) Sets whether to log raw HTTP requests.Enables logging of raw HTTP responses.logResponses(Boolean logResponses) Sets whether to log raw HTTP responses.Sets the timeout duration for both connection and read operations.Sets the API version to use.
-
Field Details
-
httpClientBuilder
-
baseUrl
-
apiKey
-
version
-
beta
-
timeout
-
logger
public org.slf4j.Logger logger -
logRequests
-
logResponses
-
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
build
Builds and returns a newAnthropicClientinstance.- Returns:
- a configured
AnthropicClientinstance
-
httpClientBuilder
Sets a custom HTTP client builder for configuring the underlying HTTP client.If not specified, a default HTTP client builder is loaded via
HttpClientBuilderLoader.- Parameters:
httpClientBuilder- the HTTP client builder to use- Returns:
- this builder for method chaining
-
baseUrl
-
apiKey
-
version
Sets the API version to use.The version is sent via the
anthropic-versionheader. Anthropic guarantees backward compatibility within a version: existing input/output parameters are preserved, though new optional inputs and output values may be added.Recommended version: "2023-06-01" (incremental streaming, named SSE events).
- Parameters:
version- the API version string (e.g., "2023-06-01")- Returns:
- this builder for method chaining
- See Also:
-
beta
Sets the beta features header for accessing experimental API capabilities.Beta features allow access to new model capabilities before they are part of the standard API. These features are subject to change and may be modified or removed in future releases.
The value is sent via the
anthropic-betaheader.- Parameters:
beta- the beta feature identifier(s)- Returns:
- this builder for method chaining
-
timeout
Sets the timeout duration for both connection and read operations.If not specified, defaults are applied:
- Connection timeout: 15 seconds
- Read timeout: 60 seconds
- Parameters:
timeout- the timeout duration to apply to both connection and read operations- Returns:
- this builder for method chaining
-
logRequests
Enables logging of raw HTTP requests.Convenience method equivalent to
logRequests(true).- Returns:
- this builder for method chaining
- See Also:
-
logRequests
Sets whether to log raw HTTP requests.When enabled, the full HTTP request (including headers and body) is logged for debugging purposes. If
nullis provided, defaults tofalse.- Parameters:
logRequests-trueto enable request logging,falseto disable, ornull(defaults tofalse)- Returns:
- this builder for method chaining
-
logResponses
Enables logging of raw HTTP responses.Convenience method equivalent to
logResponses(true).- Returns:
- this builder for method chaining
- See Also:
-
logResponses
Sets whether to log raw HTTP responses.When enabled, the full HTTP response (including headers and body) is logged for debugging purposes. If
nullis provided, defaults tofalse.- Parameters:
logResponses-trueto enable response logging,falseto disable, ornull(defaults tofalse)- Returns:
- this builder for method chaining
-
logger
Sets a custom logger for request and response logging.If not specified, the default LangChain4j logger is used.
- Parameters:
logger- an alternateLoggerto use instead of the default- Returns:
- this builder for method chaining
-