Class AnthropicProvider
java.lang.Object
org.machanism.machai.ai.provider.AbstractAIProvider
org.machanism.machai.ai.provider.anthropic.AnthropicProvider
- All Implemented Interfaces:
Genai
Anthropic-backed implementation of Machai's
Genai abstraction.
This provider adapts the Anthropic Java SDK to the Machai provider interface. It manages prompt collection, request construction for the Anthropic Beta Messages API, custom tool execution, optional web search integration, optional MCP server forwarding, and usage tracking.
Configuration
chatModel(required): model identifier for the Anthropic API, for example"claude-3-opus-20240229".ANTHROPIC_API_KEY(required): API key or authorization token for the Anthropic API.ANTHROPIC_BASE_URL(optional): base URL for Anthropic-compatible endpoints. If unset, the SDK default is used.GENAI_TIMEOUT(optional): request timeout in seconds. If missing, zero, or negative, the SDK default is used.MAX_OUTPUT_TOKENS(optional): maximum number of output tokens. Defaults to 18000L.MAX_TOOL_CALLS(optional): maximum number of tool calls per response. Zero leaves the limit unset.cacheThreshold(optional): character length above which tool results are marked with ephemeral prompt cache control.
- Since:
- 1.1.13
- Author:
- Viktor Tovstyi
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringConfiguration property name that contains the Anthropic API key or authorization token.static final StringConfiguration property name that overrides the Anthropic API base URL.Fields inherited from class org.machanism.machai.ai.provider.AbstractAIProvider
chatModel, config, DEFAULT_WEBSEARCH_TYPE_NAME, instructions, lastUsage, LINE_SEPARATOR, LOG_INPUTS_PROP_NAME, LOG_LINE_LENG, LOG_SECTION_SEPARATOR, MAX_OUTPUT_TOKENS, maxOutputTokens, maxToolCalls, MCP_PROP_NAME_PREFIX, PARAGRAPH_SEPARATOR, PASSWORD_PROP_NAME, PROJECT_DIR_PARAM_NAME, projectDir, SERVERID_PROP_NAME, timeoutSec, USERNAME_PROP_NAME -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddMcpServer(String name, String url, String authorization, String description) Registers an MCP server definition to include in future Anthropic requests.voidaddTool(String name, String description, ToolFunction function, ParamDescriptor... paramsDesc) Registers a local function tool that Anthropic may request during response generation.protected voidaddWebSearch(String type, String city, String country, String region) Registers a web search tool and optional user-location hints for future requests.voidclear()Clears the accumulated conversation inputs so the provider can be reused for a new request.protected com.anthropic.client.AnthropicClientCreates and configures an Anthropic client using the current provider configuration.voidinit(String model, Configurator config) Initializes this provider with the supplied model and configuration.protected voidlogInputsSpec(Writer streamWriter) Writes provider-specific input items to the supplied log writer.perform()Sends the accumulated conversation to Anthropic and returns the final text response.voidAdds a non-blank user prompt to the current conversation.Methods inherited from class org.machanism.machai.ai.provider.AbstractAIProvider
addMcpServers, addPrompt, addPrompts, addTools, addWebSearch, getParamValue, getProjectDir, getTimeout, inputsLog, instructions, logInputs, normalize, safelyInvokeTool, setProjectDir, setTimeout, setWorkingDir
-
Field Details
-
ANTHROPIC_API_KEY
Configuration property name that contains the Anthropic API key or authorization token.- See Also:
-
ANTHROPIC_BASE_URL
Configuration property name that overrides the Anthropic API base URL.- See Also:
-
-
Constructor Details
-
AnthropicProvider
public AnthropicProvider()
-
-
Method Details
-
init
Initializes this provider with the supplied model and configuration.- Specified by:
initin interfaceGenai- Overrides:
initin classAbstractAIProvider- Parameters:
model- the Anthropic model identifier to use for subsequent requestsconfig- provider configuration containing credentials, endpoint, timeout, and limits
-
addMcpServer
Registers an MCP server definition to include in future Anthropic requests.- Overrides:
addMcpServerin classAbstractAIProvider- Parameters:
name- server name exposed to the modelurl- server endpoint URLauthorization- optional authorization token, ornullwhen not requireddescription- optional human-readable server description; currently not forwarded by the SDK model
-
addWebSearch
Registers a web search tool and optional user-location hints for future requests.- Overrides:
addWebSearchin classAbstractAIProvider- Parameters:
type- web search tool version; supported values are"20260209","20250305", andAbstractAIProvider.DEFAULT_WEBSEARCH_TYPE_NAMEcity- optional city hint for search localizationcountry- optional country hint for search localizationregion- optional region hint for search localization- Throws:
IllegalArgumentException- iftypeis not a supported Anthropic web search tool version
-
prompt
Adds a non-blank user prompt to the current conversation.- Specified by:
promptin interfaceGenai- Overrides:
promptin classAbstractAIProvider- Parameters:
text- prompt text to submit as a user message; blank values are ignored
-
perform
Sends the accumulated conversation to Anthropic and returns the final text response.If the model requests tool use, this method resolves the tool-call loop before returning the final textual answer.
- Returns:
- final model response text, or
nullwhen no inputs are available or no text is produced
-
clear
public void clear()Clears the accumulated conversation inputs so the provider can be reused for a new request.- Specified by:
clearin interfaceGenai- Overrides:
clearin classAbstractAIProvider
-
addTool
public void addTool(String name, String description, ToolFunction function, ParamDescriptor... paramsDesc) Registers a local function tool that Anthropic may request during response generation.- Specified by:
addToolin classAbstractAIProvider- Parameters:
name- tool function name exposed to the modeldescription- human-readable tool description used by the model to decide when to call itfunction- callback that executes the tool locallyparamsDesc- descriptors for tool input parameters; the project directory parameter is handled internally and is not included in the Anthropic input schema
-
getClient
protected com.anthropic.client.AnthropicClient getClient()Creates and configures an Anthropic client using the current provider configuration.- Returns:
- Anthropic client configured with authentication, optional base URL, timeout, and retry settings
-
logInputsSpec
Writes provider-specific input items to the supplied log writer.- Overrides:
logInputsSpecin classAbstractAIProvider- Parameters:
streamWriter- destination writer that receives serialized prompt content- Throws:
IOException- if writing tostreamWriterfails
-