Class AnthropicChatModel

java.lang.Object
org.springframework.ai.anthropic.AnthropicChatModel
All Implemented Interfaces:
org.springframework.ai.chat.model.ChatModel, org.springframework.ai.chat.model.StreamingChatModel, org.springframework.ai.model.Model<org.springframework.ai.chat.prompt.Prompt, org.springframework.ai.chat.model.ChatResponse>, org.springframework.ai.model.StreamingModel<org.springframework.ai.chat.prompt.Prompt, org.springframework.ai.chat.model.ChatResponse>

public final class AnthropicChatModel extends Object implements org.springframework.ai.chat.model.ChatModel, org.springframework.ai.chat.model.StreamingChatModel
ChatModel and StreamingChatModel implementation using the official Anthropic Java SDK.

Supports synchronous and streaming completions, tool calling, and Micrometer-based observability. API credentials are auto-detected from ANTHROPIC_API_KEY if not configured.

Since:
1.0.0
Author:
Christian Tzolov, luocongqiu, Mariusz Bernacki, Thomas Vitale, Claudio Silva Junior, Alexandros Pappas, Jonghoon Park, Soby Chacko, Austin Dase
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Builder for creating AnthropicChatModel instances.
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a new builder for AnthropicChatModel.
    org.springframework.ai.chat.model.ChatResponse
    call(org.springframework.ai.chat.prompt.Prompt prompt)
     
    com.anthropic.client.AnthropicClient
    Returns the underlying synchronous Anthropic SDK client.
    com.anthropic.client.AnthropicClientAsync
    Returns the underlying asynchronous Anthropic SDK client.
    org.springframework.ai.chat.prompt.ChatOptions
     
    Gets the chat options for this model.
    org.springframework.ai.chat.model.ChatResponse
    internalCall(org.springframework.ai.chat.prompt.Prompt prompt, @Nullable org.springframework.ai.chat.model.ChatResponse previousChatResponse)
    Internal method to handle synchronous chat completion calls with tool execution support.
    reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse>
    internalStream(org.springframework.ai.chat.prompt.Prompt prompt, @Nullable org.springframework.ai.chat.model.ChatResponse previousChatResponse)
    Internal method to handle streaming chat completion calls with tool execution support.
    void
    setObservationConvention(org.springframework.ai.chat.observation.ChatModelObservationConvention observationConvention)
    Use the provided convention for reporting observation data.
    reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse>
    stream(org.springframework.ai.chat.prompt.Prompt prompt)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.ai.chat.model.ChatModel

    call, call

    Methods inherited from interface org.springframework.ai.chat.model.StreamingChatModel

    stream, stream
  • Method Details

    • builder

      public static AnthropicChatModel.Builder builder()
      Creates a new builder for AnthropicChatModel.
      Returns:
      a new builder instance
    • getOptions

      public AnthropicChatOptions getOptions()
      Gets the chat options for this model.
      Returns:
      the chat options
    • getAnthropicClient

      public com.anthropic.client.AnthropicClient getAnthropicClient()
      Returns the underlying synchronous Anthropic SDK client. Useful for accessing SDK features directly, such as the Files API (client.beta().files()).
      Returns:
      the sync client
    • getAnthropicClientAsync

      public com.anthropic.client.AnthropicClientAsync getAnthropicClientAsync()
      Returns the underlying asynchronous Anthropic SDK client. Useful for non-blocking access to SDK features directly, such as the Files API.
      Returns:
      the async client
    • call

      public org.springframework.ai.chat.model.ChatResponse call(org.springframework.ai.chat.prompt.Prompt prompt)
      Specified by:
      call in interface org.springframework.ai.chat.model.ChatModel
      Specified by:
      call in interface org.springframework.ai.model.Model<org.springframework.ai.chat.prompt.Prompt, org.springframework.ai.chat.model.ChatResponse>
    • stream

      public reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> stream(org.springframework.ai.chat.prompt.Prompt prompt)
      Specified by:
      stream in interface org.springframework.ai.chat.model.ChatModel
      Specified by:
      stream in interface org.springframework.ai.chat.model.StreamingChatModel
      Specified by:
      stream in interface org.springframework.ai.model.StreamingModel<org.springframework.ai.chat.prompt.Prompt, org.springframework.ai.chat.model.ChatResponse>
    • internalStream

      public reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> internalStream(org.springframework.ai.chat.prompt.Prompt prompt, @Nullable org.springframework.ai.chat.model.ChatResponse previousChatResponse)
      Internal method to handle streaming chat completion calls with tool execution support. This method is called recursively to support multi-turn tool calling.
      Parameters:
      prompt - The prompt for the chat completion. In a recursive tool-call scenario, this prompt will contain the full conversation history including the tool results.
      previousChatResponse - The chat response from the preceding API call. This is used to accumulate token usage correctly across multiple API calls in a single user turn.
      Returns:
      A Flux of ChatResponse events, which can include text chunks and the final response with tool call information or the model's final answer.
    • internalCall

      public org.springframework.ai.chat.model.ChatResponse internalCall(org.springframework.ai.chat.prompt.Prompt prompt, @Nullable org.springframework.ai.chat.model.ChatResponse previousChatResponse)
      Internal method to handle synchronous chat completion calls with tool execution support. This method is called recursively to support multi-turn tool calling.
      Parameters:
      prompt - The prompt for the chat completion. In a recursive tool-call scenario, this prompt will contain the full conversation history including the tool results.
      previousChatResponse - The chat response from the preceding API call. This is used to accumulate token usage correctly across multiple API calls in a single user turn.
      Returns:
      The final ChatResponse after all tool calls (if any) are resolved.
    • getDefaultOptions

      public org.springframework.ai.chat.prompt.ChatOptions getDefaultOptions()
      Specified by:
      getDefaultOptions in interface org.springframework.ai.chat.model.ChatModel
    • setObservationConvention

      public void setObservationConvention(org.springframework.ai.chat.observation.ChatModelObservationConvention observationConvention)
      Use the provided convention for reporting observation data.
      Parameters:
      observationConvention - the provided convention