Class AnthropicClient.Builder

java.lang.Object
com.tnsai.llm.providers.AnthropicClient.Builder
Enclosing class:
AnthropicClient

public static class AnthropicClient.Builder extends Object
Builder for creating AnthropicClient instances.

Example usage:


 AnthropicClient client = AnthropicClient.builder()
     .apiKey("sk-...")
     .model("claude-sonnet-4-20250514")
     .temperature(0.7f)
     .maxTokens(4096)
     .beta("advanced-tool-use-2025-11-20")
     .build();
 
  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • baseUrl

      public AnthropicClient.Builder baseUrl(String baseUrl)
      Sets the API base URL. Defaults to Anthropic's API.
    • apiKey

      public AnthropicClient.Builder apiKey(String apiKey)
      Sets the API key. If not set, will use ANTHROPIC_API_KEY environment variable.
    • model

      public AnthropicClient.Builder model(String model)
      Sets the model to use.
    • temperature

      public AnthropicClient.Builder temperature(float temperature)
      Sets the temperature for response generation.
    • topP

      public AnthropicClient.Builder topP(float topP)
      Sets the top-p (nucleus sampling) value.
    • maxTokens

      public AnthropicClient.Builder maxTokens(int maxTokens)
      Sets the maximum number of tokens to generate.
    • beta

      public AnthropicClient.Builder beta(String beta)
      Adds a beta feature identifier.
    • betas

      public AnthropicClient.Builder betas(List<String> betas)
      Adds multiple beta feature identifiers.
    • withAdvancedToolUse

      public AnthropicClient.Builder withAdvancedToolUse()
      Enables advanced tool use features (tool search, programmatic calling).
    • withWebSearch

      public AnthropicClient.Builder withWebSearch()
      Enables web search capability.
    • withPromptCaching

      public AnthropicClient.Builder withPromptCaching()
      Enables prompt caching for reduced latency and ~75% lower input-token cost on repeated prompts (issue #27). Adds the prompt-caching beta header AND turns on cache_control marker injection — these only work together, requiring users to remember both is a footgun.

      Equivalent to .enableEphemeralCaching(true).

    • enableEphemeralCaching

      public AnthropicClient.Builder enableEphemeralCaching(boolean enabled)
      Toggles cache_control marker injection on the system prompt and the last cacheLastNTurns(int) user messages in history (issue #27). Auto-adds the prompt-caching beta header at build time when enabled.
      Since:
      0.5.0 (issue #27)
    • cacheLastNTurns

      public AnthropicClient.Builder cacheLastNTurns(int n)
      Upper bound on how many of the last user messages in history get a cache_control marker (issue #27). Capped internally to MAX_CACHE_BREAKPOINTS - 1 (currently 3) so total breakpoints — system (1) + history (N) — never exceed Anthropic's 4-per-request limit. Default: 3.
      Throws:
      IllegalArgumentException - if negative
      Since:
      0.5.0 (issue #27)
    • withComputerUse

      public AnthropicClient.Builder withComputerUse()
      Enables computer use capabilities.
    • build

      public AnthropicClient build()
      Builds the AnthropicClient instance.