Class AnthropicClient.Builder<T extends AnthropicClient, B extends AnthropicClient.Builder<T,B>>

java.lang.Object
dev.langchain4j.model.anthropic.internal.client.AnthropicClient.Builder<T,B>
Type Parameters:
T - the type of AnthropicClient being built
B - the concrete builder type (for method chaining)
Direct Known Subclasses:
DefaultAnthropicClient.Builder
Enclosing class:
AnthropicClient

public abstract static class AnthropicClient.Builder<T extends AnthropicClient, B extends AnthropicClient.Builder<T,B>> extends Object
Builder class for constructing 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

Optional Configuration

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 Details

    • httpClientBuilder

      public HttpClientBuilder httpClientBuilder
    • baseUrl

      public String baseUrl
    • apiKey

      public String apiKey
    • version

      public String version
    • beta

      public String beta
    • timeout

      public Duration timeout
    • logger

      public org.slf4j.Logger logger
    • logRequests

      public Boolean logRequests
    • logResponses

      public Boolean logResponses
  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • build

      public abstract T build()
      Builds and returns a new AnthropicClient instance.
      Returns:
      a configured AnthropicClient instance
    • httpClientBuilder

      public B httpClientBuilder(HttpClientBuilder 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

      public B baseUrl(String baseUrl)
      Sets the base URL for the Anthropic API.
      Parameters:
      baseUrl - the API base URL (e.g., "https://api.anthropic.com/v1")
      Returns:
      this builder for method chaining
    • apiKey

      public B apiKey(String apiKey)
      Sets the API key for authentication.

      The key is sent via the x-api-key header with every request.

      Parameters:
      apiKey - the Anthropic API key
      Returns:
      this builder for method chaining
    • version

      public B version(String version)
      Sets the API version to use.

      The version is sent via the anthropic-version header. 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

      public B beta(String 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-beta header.

      Parameters:
      beta - the beta feature identifier(s)
      Returns:
      this builder for method chaining
    • timeout

      public B timeout(Duration 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

      public B logRequests()
      Enables logging of raw HTTP requests.

      Convenience method equivalent to logRequests(true).

      Returns:
      this builder for method chaining
      See Also:
    • logRequests

      public B logRequests(Boolean logRequests)
      Sets whether to log raw HTTP requests.

      When enabled, the full HTTP request (including headers and body) is logged for debugging purposes. If null is provided, defaults to false.

      Parameters:
      logRequests - true to enable request logging, false to disable, or null (defaults to false)
      Returns:
      this builder for method chaining
    • logResponses

      public B logResponses()
      Enables logging of raw HTTP responses.

      Convenience method equivalent to logResponses(true).

      Returns:
      this builder for method chaining
      See Also:
    • logResponses

      public B logResponses(Boolean logResponses)
      Sets whether to log raw HTTP responses.

      When enabled, the full HTTP response (including headers and body) is logged for debugging purposes. If null is provided, defaults to false.

      Parameters:
      logResponses - true to enable response logging, false to disable, or null (defaults to false)
      Returns:
      this builder for method chaining
    • logger

      public B logger(org.slf4j.Logger logger)
      Sets a custom logger for request and response logging.

      If not specified, the default LangChain4j logger is used.

      Parameters:
      logger - an alternate Logger to use instead of the default
      Returns:
      this builder for method chaining