Class AnthropicClient

java.lang.Object
com.tnsai.llm.providers.AbstractLLMClient
com.tnsai.llm.providers.AnthropicClient
All Implemented Interfaces:
com.tnsai.llm.LLMClient

public class AnthropicClient extends AbstractLLMClient
Anthropic Claude LLM client implementation. Provides access to Claude models via the Messages API.

Supported models:

  • claude-sonnet-4-20250514 - Claude Sonnet 4 (recommended)
  • claude-3-5-sonnet-20241022 - Claude 3.5 Sonnet v2
  • claude-3-5-haiku-20241022 - Claude 3.5 Haiku (fast)
  • claude-3-opus-20240229 - Claude 3 Opus (most capable)
  • claude-3-sonnet-20240229 - Claude 3 Sonnet
  • claude-3-haiku-20240307 - Claude 3 Haiku (fastest)

Includes built-in retry mechanism for resilience against network issues and transient API errors.

Configuration via environment variables:

  • ANTHROPIC_API_KEY - Required API key for authentication
  • ANTHROPIC_BASE_URL - Optional, defaults to https://api.anthropic.com
See Also:
  • Field Details

    • BETA_ADVANCED_TOOL_USE

      public static final String BETA_ADVANCED_TOOL_USE
      Beta header for advanced tool use features (tool search, programmatic calling)
      See Also:
    • BETA_PROMPT_CACHING

      public static final String BETA_PROMPT_CACHING
      Beta header for prompt caching
      See Also:
    • BETA_COMPUTER_USE

      public static final String BETA_COMPUTER_USE
      Beta header for computer use
      See Also:
    • TOOL_TYPE_SEARCH_REGEX

      public static final String TOOL_TYPE_SEARCH_REGEX
      Tool type for regex-based tool search
      See Also:
    • TOOL_TYPE_CODE_EXECUTION

      public static final String TOOL_TYPE_CODE_EXECUTION
      Tool type for code execution (programmatic tool calling)
      See Also:
  • Constructor Details

    • AnthropicClient

      public AnthropicClient(String model)
      Creates an AnthropicClient with the specified model. API key is read from ANTHROPIC_API_KEY environment variable.
      Parameters:
      model - The model to use (e.g., "claude-sonnet-4-20250514")
    • AnthropicClient

      public AnthropicClient(String model, float temperature)
      Creates an AnthropicClient with the specified model and temperature.
      Parameters:
      model - The model to use
      temperature - The temperature for response generation (0.0-1.0)
    • AnthropicClient

      public AnthropicClient(String model, float temperature, Integer maxTokens)
      Creates an AnthropicClient with the specified model, temperature, and max tokens.
      Parameters:
      model - The model to use
      temperature - The temperature for response generation (0.0-1.0)
      maxTokens - The maximum number of tokens to generate (required by Claude)
    • AnthropicClient

      public AnthropicClient(String model, float temperature, Float topP, Integer maxTokens)
      Creates an AnthropicClient with the specified model, temperature, topP, and max tokens.
      Parameters:
      model - The model to use
      temperature - The temperature for response generation (0.0-1.0)
      topP - The top-p (nucleus sampling) value (0.0-1.0)
      maxTokens - The maximum number of tokens to generate (required by Claude)
    • AnthropicClient

      public AnthropicClient(String baseUrl, String model, float temperature, Integer maxTokens, String apiKey)
      Creates an AnthropicClient with full configuration.
      Parameters:
      baseUrl - The base URL for the API (null for default)
      model - The model to use
      temperature - The temperature for response generation (0.0-1.0)
      maxTokens - The maximum number of tokens to generate
      apiKey - The API key (null to read from environment)
    • AnthropicClient

      public AnthropicClient(String baseUrl, String model, float temperature, Float topP, Integer maxTokens, String apiKey, List<String> betas)
      Creates an AnthropicClient with full configuration including beta features.
      Parameters:
      baseUrl - The base URL for the API (null for default)
      model - The model to use
      temperature - The temperature for response generation (0.0-1.0)
      topP - The top-p (nucleus sampling) value (0.0-1.0, null for default)
      maxTokens - The maximum number of tokens to generate
      apiKey - The API key (null to read from environment)
      betas - List of beta feature identifiers to enable
    • AnthropicClient

      public AnthropicClient(String baseUrl, String model, float temperature, Float topP, Integer maxTokens, String apiKey, List<String> betas, boolean enableEphemeralCaching, int cacheLastNTurns)
      Full constructor — see the 7-arg overload for the historical contract. Adds two parameters for Anthropic prompt caching (issue #27):
      Parameters:
      enableEphemeralCaching - when true, inject cache_control: ephemeral markers on the system prompt and the last cacheLastNTurns user messages in history. Auto-adds BETA_PROMPT_CACHING to the betas list — the markers are silent no-ops without the beta header. Default false so existing call sites see no behaviour change.
      cacheLastNTurns - upper bound on how many of the last user messages in history get a cache_control marker. Capped internally so total breakpoints (system + history) never exceed Anthropic's per-request limit of 4. Default 3.
      Since:
      0.5.0 (issue #27)
  • Method Details

    • builder

      public static AnthropicClient.Builder builder()
      Creates a new builder for AnthropicClient.
      Returns:
      a new Builder instance
    • chat

      public com.tnsai.llm.ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String,Object>>> history, Optional<List<Map<String,Object>>> tools)
    • streamChat

      public Stream<String> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String,Object>>> history, Optional<List<Map<String,Object>>> tools)
    • getBetas

      public List<String> getBetas()
      Returns the list of beta features enabled for this client.
      Returns:
      unmodifiable list of beta feature identifiers
    • hasBetas

      public boolean hasBetas()
      Returns true if any beta features are enabled.
      Returns:
      true if betas are configured
    • createToolSearchTool

      public static Map<String,Object> createToolSearchTool()
      Creates a tool search tool definition for dynamic tool discovery.
    • createCodeExecutionTool

      public static Map<String,Object> createCodeExecutionTool()
      Creates a code execution tool definition for programmatic tool calling.
    • createWebSearchTool

      public static Map<String,Object> createWebSearchTool()
      Creates a web search tool definition.
    • createWebSearchTool

      public static Map<String,Object> createWebSearchTool(List<String> allowedDomains, List<String> blockedDomains)
      Creates a web search tool definition with domain filtering.
    • createProgrammaticTool

      public static Map<String,Object> createProgrammaticTool(String name, String description, Map<String,Object> inputSchema)
      Creates a tool definition that can be called by code execution.
    • createDeferredTool

      public static Map<String,Object> createDeferredTool(String name, String description, Map<String,Object> inputSchema)
      Creates a deferred tool definition for tool search.
    • hasAdvancedToolUse

      public boolean hasAdvancedToolUse()
      Checks if this client has advanced tool use enabled.
    • hasWebSearch

      public boolean hasWebSearch()
      Checks if this client has web search enabled.
    • hasPromptCaching

      public boolean hasPromptCaching()
      Checks if this client has prompt caching enabled.