Class AnthropicProvider

java.lang.Object
org.machanism.machai.ai.provider.AbstractAIProvider
org.machanism.machai.ai.provider.anthropic.AnthropicProvider
All Implemented Interfaces:
Genai

public class AnthropicProvider extends AbstractAIProvider
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 Details

    • ANTHROPIC_API_KEY

      public static final String ANTHROPIC_API_KEY
      Configuration property name that contains the Anthropic API key or authorization token.
      See Also:
    • ANTHROPIC_BASE_URL

      public static final String ANTHROPIC_BASE_URL
      Configuration property name that overrides the Anthropic API base URL.
      See Also:
  • Constructor Details

    • AnthropicProvider

      public AnthropicProvider()
  • Method Details

    • init

      public void init(String model, Configurator config)
      Initializes this provider with the supplied model and configuration.
      Specified by:
      init in interface Genai
      Overrides:
      init in class AbstractAIProvider
      Parameters:
      model - the Anthropic model identifier to use for subsequent requests
      config - provider configuration containing credentials, endpoint, timeout, and limits
    • addMcpServer

      protected void addMcpServer(String name, String url, String authorization, String description)
      Registers an MCP server definition to include in future Anthropic requests.
      Overrides:
      addMcpServer in class AbstractAIProvider
      Parameters:
      name - server name exposed to the model
      url - server endpoint URL
      authorization - optional authorization token, or null when not required
      description - optional human-readable server description; currently not forwarded by the SDK model
    • addWebSearch

      protected void addWebSearch(String type, String city, String country, String region)
      Registers a web search tool and optional user-location hints for future requests.
      Overrides:
      addWebSearch in class AbstractAIProvider
      Parameters:
      type - web search tool version; supported values are "20260209", "20250305", and AbstractAIProvider.DEFAULT_WEBSEARCH_TYPE_NAME
      city - optional city hint for search localization
      country - optional country hint for search localization
      region - optional region hint for search localization
      Throws:
      IllegalArgumentException - if type is not a supported Anthropic web search tool version
    • prompt

      public void prompt(String text)
      Adds a non-blank user prompt to the current conversation.
      Specified by:
      prompt in interface Genai
      Overrides:
      prompt in class AbstractAIProvider
      Parameters:
      text - prompt text to submit as a user message; blank values are ignored
    • perform

      public String 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 null when 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:
      clear in interface Genai
      Overrides:
      clear in class AbstractAIProvider
    • 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:
      addTool in class AbstractAIProvider
      Parameters:
      name - tool function name exposed to the model
      description - human-readable tool description used by the model to decide when to call it
      function - callback that executes the tool locally
      paramsDesc - 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

      protected void logInputsSpec(Writer streamWriter) throws IOException
      Writes provider-specific input items to the supplied log writer.
      Overrides:
      logInputsSpec in class AbstractAIProvider
      Parameters:
      streamWriter - destination writer that receives serialized prompt content
      Throws:
      IOException - if writing to streamWriter fails