Class AnthropicChatModel
- All Implemented Interfaces:
ChatModel,StreamingChatModel,Model<Prompt,,ChatResponse> StreamingModel<Prompt,ChatResponse>
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.
Observability. Two layers of Micrometer observations are emitted: a
gen_ai.client.operation span per chat-model call (with token usage, model
metadata, and request parameters), and an okhttp.requests span per outbound
HTTP attempt (with HTTP method, URI, status code, and traceparent propagation).
Optional OkHttp connection-pool gauges are bound to the
MeterRegistry when supplied. For both synchronous
and streaming calls the HTTP span nests under the chat-model span.
- Since:
- 1.0.0
- Author:
- Christian Tzolov, luocongqiu, Mariusz Bernacki, Thomas Vitale, Claudio Silva Junior, Alexandros Pappas, Jonghoon Park, Soby Chacko, Austin Dase, Sebastien Deleuze, Ilayaperumal Gopinathan, Jewoo Shin, Seeun Kim, guan xu
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for creatingAnthropicChatModelinstances. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringMetadata key set totrueon streamingAssistantMessagechunks that carry incremental thinking (reasoning) text from a thinking-enabled model.static final StringMetadata key holding the incremental thinking text on streamingAssistantMessagechunks whereTHINKING_METADATA_KEYistrue. -
Method Summary
Modifier and TypeMethodDescriptionstatic AnthropicChatModel.Builderbuilder()Creates a new builder forAnthropicChatModel.Executes a method call to the AI model.com.anthropic.client.AnthropicClientReturns the underlying synchronous Anthropic SDK client.com.anthropic.client.AnthropicClientAsyncReturns the underlying asynchronous Anthropic SDK client.Gets the chat options for this model.internalCall(Prompt prompt, @Nullable ChatResponse previousChatResponse) Internal method to handle synchronous chat completion calls with tool execution support.reactor.core.publisher.Flux<ChatResponse>internalStream(Prompt prompt, @Nullable ChatResponse previousChatResponse) Internal method to handle streaming chat completion calls with tool execution support.voidsetObservationConvention(ChatModelObservationConvention observationConvention) Use the provided convention for reporting observation data.reactor.core.publisher.Flux<ChatResponse>Streams the chat completion as aFluxofChatResponseevents.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.ai.chat.model.ChatModel
call, call, getDefaultOptionsMethods inherited from interface org.springframework.ai.chat.model.StreamingChatModel
stream, stream
-
Field Details
-
THINKING_METADATA_KEY
Metadata key set totrueon streamingAssistantMessagechunks that carry incremental thinking (reasoning) text from a thinking-enabled model.Use this key to identify thinking deltas in a streaming response:
chatModel.stream(prompt).subscribe(response -> { AssistantMessage message = response.getResult().getOutput(); if (Boolean.TRUE.equals(message.getMetadata().get(AnthropicChatModel.THINKING_METADATA_KEY))) { String chunk = (String) message.getMetadata().get(AnthropicChatModel.THINKING_TEXT_METADATA_KEY); } });- Since:
- 2.0.2
- See Also:
-
THINKING_TEXT_METADATA_KEY
Metadata key holding the incremental thinking text on streamingAssistantMessagechunks whereTHINKING_METADATA_KEYistrue. The chunk content (getText()) isnullso that thinking text is not aggregated into the final answer.- Since:
- 2.0.2
- See Also:
-
-
Method Details
-
builder
Creates a new builder forAnthropicChatModel.- Returns:
- a new builder instance
-
getOptions
Gets the chat options for this model.- Specified by:
getOptionsin interfaceChatModel- Returns:
- the chat options
- Since:
- 2.0.0
-
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
Description copied from interface:ModelExecutes a method call to the AI model. -
stream
Streams the chat completion as aFluxofChatResponseevents.- Specified by:
streamin interfaceChatModel- Specified by:
streamin interfaceStreamingChatModel- Specified by:
streamin interfaceStreamingModel<Prompt,ChatResponse> - Parameters:
prompt- the prompt- Returns:
- a
Fluxof streamedChatResponseevents
-
internalStream
public reactor.core.publisher.Flux<ChatResponse> internalStream(Prompt prompt, @Nullable 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.Rate-limit headers are read from the streaming response via
withRawResponse().createStreaming(...)and attached to the aggregatedChatResponse. Because that SDK call exposes the stream as a blockingStreamResponse, the events are pulled onSchedulers.boundedElastic(); a streaming call therefore holds a worker thread for the duration of the stream.- 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
FluxofChatResponseevents, which can include text chunks and the final response with tool call information or the model's final answer.
-
internalCall
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
ChatResponseafter all tool calls (if any) are resolved.
-
setObservationConvention
Use the provided convention for reporting observation data.- Parameters:
observationConvention- the provided convention
-