Package com.tnsai.llm.providers
Class AnthropicClient
java.lang.Object
com.tnsai.llm.providers.AbstractLLMClient
com.tnsai.llm.providers.AnthropicClient
- All Implemented Interfaces:
com.tnsai.llm.LLMClient
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating AnthropicClient instances. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringBeta header for advanced tool use features (tool search, programmatic calling)static final StringBeta header for computer usestatic final StringBeta header for prompt cachingstatic final StringBeta header for web search capabilitystatic final StringTool type for code execution (programmatic tool calling)static final StringTool type for regex-based tool searchstatic final StringTool type for web search -
Constructor Summary
ConstructorsConstructorDescriptionAnthropicClient(String model) Creates an AnthropicClient with the specified model.AnthropicClient(String model, float temperature) Creates an AnthropicClient with the specified model and temperature.AnthropicClient(String model, float temperature, Float topP, Integer maxTokens) Creates an AnthropicClient with the specified model, temperature, topP, and max tokens.AnthropicClient(String model, float temperature, Integer maxTokens) Creates an AnthropicClient with the specified model, temperature, and max tokens.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.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.AnthropicClient(String baseUrl, String model, float temperature, Integer maxTokens, String apiKey) Creates an AnthropicClient with full configuration. -
Method Summary
Modifier and TypeMethodDescriptionstatic AnthropicClient.Builderbuilder()Creates a new builder for AnthropicClient.com.tnsai.llm.ChatResponsechat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Creates a code execution tool definition for programmatic tool calling.Creates a deferred tool definition for tool search.Creates a tool definition that can be called by code execution.Creates a tool search tool definition for dynamic tool discovery.Creates a web search tool definition.createWebSearchTool(List<String> allowedDomains, List<String> blockedDomains) Creates a web search tool definition with domain filtering.getBetas()Returns the list of beta features enabled for this client.booleanChecks if this client has advanced tool use enabled.booleanhasBetas()Returns true if any beta features are enabled.booleanChecks if this client has prompt caching enabled.booleanChecks if this client has web search enabled.streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Methods inherited from class com.tnsai.llm.providers.AbstractLLMClient
getBaseUrl, getCallTimeoutMs, getMaxTokens, getModel, getTemperature, getTopPMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, chat, getCapabilities, getProviderName, listModels, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec, streamChatWithSpec
-
Field Details
-
BETA_ADVANCED_TOOL_USE
Beta header for advanced tool use features (tool search, programmatic calling)- See Also:
-
BETA_WEB_SEARCH
Beta header for web search capability- See Also:
-
BETA_PROMPT_CACHING
Beta header for prompt caching- See Also:
-
BETA_COMPUTER_USE
Beta header for computer use- See Also:
-
TOOL_TYPE_SEARCH_REGEX
Tool type for regex-based tool search- See Also:
-
TOOL_TYPE_CODE_EXECUTION
Tool type for code execution (programmatic tool calling)- See Also:
-
TOOL_TYPE_WEB_SEARCH
Tool type for web search- See Also:
-
-
Constructor Details
-
AnthropicClient
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
Creates an AnthropicClient with the specified model and temperature.- Parameters:
model- The model to usetemperature- The temperature for response generation (0.0-1.0)
-
AnthropicClient
Creates an AnthropicClient with the specified model, temperature, and max tokens.- Parameters:
model- The model to usetemperature- The temperature for response generation (0.0-1.0)maxTokens- The maximum number of tokens to generate (required by Claude)
-
AnthropicClient
Creates an AnthropicClient with the specified model, temperature, topP, and max tokens.- Parameters:
model- The model to usetemperature- 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 usetemperature- The temperature for response generation (0.0-1.0)maxTokens- The maximum number of tokens to generateapiKey- 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 usetemperature- 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 generateapiKey- 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, injectcache_control: ephemeralmarkers on the system prompt and the lastcacheLastNTurnsuser messages in history. Auto-addsBETA_PROMPT_CACHINGto the betas list — the markers are silent no-ops without the beta header. Defaultfalseso 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. Default3.- Since:
- 0.5.0 (issue #27)
-
-
Method Details
-
builder
Creates a new builder for AnthropicClient.- Returns:
- a new Builder instance
-
chat
-
streamChat
-
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
Creates a tool search tool definition for dynamic tool discovery. -
createCodeExecutionTool
Creates a code execution tool definition for programmatic tool calling. -
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.
-