Class AnthropicChatModel

java.lang.Object
io.agentscope.core.model.ChatModelBase
io.agentscope.extensions.model.anthropic.AnthropicChatModel
All Implemented Interfaces:
io.agentscope.core.model.Model

public class AnthropicChatModel extends io.agentscope.core.model.ChatModelBase
Anthropic Chat Model implementation using the official Anthropic Java SDK.

This implementation provides complete integration with Anthropic's Messages API, including tool calling, streaming support, and extended thinking features.

Important notes:

  • System messages are handled via the system parameter, not as messages
  • Tool results must be in separate user messages
  • Supports Claude models (claude-3-*, claude-sonnet-*, etc.)
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Builder for AnthropicChatModel.
  • Constructor Summary

    Constructors
    Constructor
    Description
    AnthropicChatModel(String baseUrl, String apiKey, String modelName, boolean streamEnabled, io.agentscope.core.model.GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, io.agentscope.core.model.transport.ProxyConfig proxyConfig, String cacheTtl)
     
    AnthropicChatModel(String baseUrl, String apiKey, String authToken, String modelName, boolean streamEnabled, io.agentscope.core.model.GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, io.agentscope.core.model.transport.ProxyConfig proxyConfig, String cacheTtl)
    Creates an Anthropic chat model with optional bearer token authentication.
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a builder for constructing AnthropicChatModel instances.
    protected reactor.core.publisher.Flux<io.agentscope.core.model.ChatResponse>
    doStream(List<io.agentscope.core.message.Msg> messages, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.GenerateOptions options)
    Stream chat completion responses from Anthropic's API.
    Gets the model name for logging and identification.

    Methods inherited from class io.agentscope.core.model.ChatModelBase

    getContextWindowSize, setContextWindowSize, setNativeStructuredOutput, setNativeStructuredOutputWithTools, stream, supportsNativeStructuredOutput, supportsNativeStructuredOutputWithTools

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AnthropicChatModel

      public AnthropicChatModel(String baseUrl, String apiKey, String modelName, boolean streamEnabled, io.agentscope.core.model.GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, io.agentscope.core.model.transport.ProxyConfig proxyConfig, String cacheTtl)
    • AnthropicChatModel

      public AnthropicChatModel(String baseUrl, String apiKey, String authToken, String modelName, boolean streamEnabled, io.agentscope.core.model.GenerateOptions defaultOptions, AnthropicBaseFormatter formatter, io.agentscope.core.model.transport.ProxyConfig proxyConfig, String cacheTtl)
      Creates an Anthropic chat model with optional bearer token authentication.

      apiKey and authToken are mutually exclusive.

      Parameters:
      baseUrl - the base URL for the Anthropic API (null for default)
      apiKey - the API key for authentication (null to omit)
      authToken - the bearer token without the Bearer prefix (null to omit)
      modelName - the model name to use
      streamEnabled - whether streaming should be enabled
      defaultOptions - default generation options
      formatter - the message formatter to use (null for the default formatter)
      proxyConfig - the proxy configuration (null for no proxy)
      cacheTtl - the TTL for prompt-caching markers (null for default 5m)
      Throws:
      IllegalArgumentException - if both API key and bearer token are configured
  • Method Details

    • doStream

      protected reactor.core.publisher.Flux<io.agentscope.core.model.ChatResponse> doStream(List<io.agentscope.core.message.Msg> messages, List<io.agentscope.core.model.ToolSchema> tools, io.agentscope.core.model.GenerateOptions options)
      Stream chat completion responses from Anthropic's API.

      This method internally handles message formatting using the configured formatter. It supports both streaming and non-streaming modes based on the streamEnabled setting.

      Supports timeout and retry configuration through GenerateOptions.

      Specified by:
      doStream in class io.agentscope.core.model.ChatModelBase
      Parameters:
      messages - AgentScope messages to send to the model
      tools - Optional list of tool schemas (null or empty if no tools)
      options - Optional generation options (null to use defaults)
      Returns:
      Flux stream of chat responses
    • getModelName

      public String getModelName()
      Gets the model name for logging and identification.
      Returns:
      the model name
    • builder

      public static AnthropicChatModel.Builder builder()
      Creates a builder for constructing AnthropicChatModel instances.
      Returns:
      a new builder