Class AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder
- Enclosing class:
AnthropicStreamingChatModel
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionSets the Anthropic API key used to authenticate requests.Sets the base URL of the Anthropic API.Sets the value of theanthropic-betarequest header to opt into beta features.build()cacheSystemMessages(Boolean cacheSystemMessages) Enables prompt caching forSystemMessages.cacheTools(Boolean cacheTools) Enables prompt caching forToolSpecifications.customHeaders(Supplier<Map<String, String>> customHeadersSupplier) Sets a supplier of extra HTTP headers to include in every request to the Anthropic API.customHeaders(Map<String, String> customHeaders) Sets extra HTTP headers to include in every request to the Anthropic API.customParameters(Map<String, Object> customParameters) Sets arbitrary extra parameters to include in the Anthropic API request body.disableParallelToolUse(Boolean disableParallelToolUse) When set totrue, prevents the model from calling multiple tools in a single response turn.httpClientBuilder(HttpClientBuilder httpClientBuilder) Sets a customHttpClientBuilderfor the underlying HTTP client.listeners(ChatModelListener... listeners) Sets theChatModelListeners to be notified on each request and response.listeners(List<ChatModelListener> listeners) Sets the list ofChatModelListeners to be notified on each request and response.logger(org.slf4j.Logger logger) logRequests(Boolean logRequests) Enables debug logging of HTTP request bodies sent to the Anthropic API.logResponses(Boolean logResponses) Enables debug logging of HTTP response bodies (server-sent events) received from the Anthropic API.Sets the maximum number of tokens to generate in the response.modelName(AnthropicChatModelName modelName) Sets the model to use for streaming chat completions using a type-safe enum constant.Sets the model to use for streaming chat completions, specified as a string model ID.responseFormat(ResponseFormat responseFormat) Sets the response format, enabling structured output such as JSON mode.returnServerToolResults(Boolean returnServerToolResults) Controls whether to return server tool results (e.g., web_search, code_execution) insideAiMessage.attributes()under the key "server_tool_results".returnThinking(Boolean returnThinking) Controls whether to return thinking/reasoning text (if available) insideAiMessage.thinking()and whether to invoke theStreamingChatResponseHandler.onPartialThinking(PartialThinking)callback.sendThinking(Boolean sendThinking) Controls whether to send thinking/reasoning text to the LLM in follow-up requests.serverTools(AnthropicServerTool... serverTools) Specifies server tools to be included in each request to the Anthropic API.serverTools(List<AnthropicServerTool> serverTools) Specifies server tools to be included in each request to the Anthropic API.stopSequences(List<String> stopSequences) Sets sequences that, when generated, will cause the model to stop generating further tokens.strictTools(Boolean strictTools) Enables strict JSON schema validation for tool input parameters.supportedCapabilities(Capability... supportedCapabilities) Declares the capabilities supported by the model (e.g.supportedCapabilities(Set<Capability> supportedCapabilities) Declares the capabilities supported by the model.temperature(Double temperature) Sets the sampling temperature in the range[0.0, 1.0].thinkingBudgetTokens(Integer thinkingBudgetTokens) Configures thinking.thinkingDisplay(String thinkingDisplay) Controls how thinking content is returned in the response stream.thinkingType(String thinkingType) Enables thinking.Sets the HTTP request timeout for calls to the Anthropic streaming API.toolChoice(ToolChoice toolChoice) Controls how the model uses tools.toolChoiceName(String toolChoiceName) Sets the name of the specific tool the model must use whenToolChoiceis set toToolChoice.REQUIRED.toolMetadataKeysToSend(String... toolMetadataKeysToSend) Specifies metadata keys from theToolSpecification.metadata()to be included in the request.toolMetadataKeysToSend(Set<String> toolMetadataKeysToSend) Specifies metadata keys from theToolSpecification.metadata()to be included in the request.toolSpecifications(ToolSpecification... toolSpecifications) Sets the tools (functions) available to the model for function calling.toolSpecifications(List<ToolSpecification> toolSpecifications) Sets the list of tools (functions) available to the model for function calling.Sets the top-K sampling value.Sets the nucleus sampling probability (top-p) in the range(0.0, 1.0].Sets the user ID for the requests.Sets the value of theanthropic-versionrequest header.
-
Constructor Details
-
AnthropicStreamingChatModelBuilder
public AnthropicStreamingChatModelBuilder()
-
-
Method Details
-
httpClientBuilder
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder httpClientBuilder(HttpClientBuilder httpClientBuilder) Sets a customHttpClientBuilderfor the underlying HTTP client. Use this to configure timeouts, proxies, or other HTTP-level settings.- Parameters:
httpClientBuilder- the HTTP client builder- Returns:
this
-
baseUrl
Sets the base URL of the Anthropic API.Defaults to
https://api.anthropic.com/v1/.- Parameters:
baseUrl- the base URL- Returns:
this
-
apiKey
Sets the Anthropic API key used to authenticate requests.- Parameters:
apiKey- the API key- Returns:
this
-
version
Sets the value of theanthropic-versionrequest header.Defaults to
2023-06-01. See the Anthropic API versioning docs.- Parameters:
version- the API version string- Returns:
this
-
beta
Sets the value of theanthropic-betarequest header to opt into beta features.See the Anthropic beta headers docs.
- Parameters:
beta- the beta feature identifier- Returns:
this
-
modelName
Sets the model to use for streaming chat completions, specified as a string model ID.See
AnthropicChatModelNamefor available model constants.- Parameters:
modelName- the model ID, e.g."claude-opus-4-5"- Returns:
this
-
modelName
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder modelName(AnthropicChatModelName modelName) Sets the model to use for streaming chat completions using a type-safe enum constant.- Parameters:
modelName- the model name enum value- Returns:
this
-
temperature
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder temperature(Double temperature) Sets the sampling temperature in the range[0.0, 1.0]. Higher values produce more random output; lower values produce more deterministic output.Cannot be used together with
topP(Double).- Parameters:
temperature- the sampling temperature- Returns:
this
-
topP
Sets the nucleus sampling probability (top-p) in the range(0.0, 1.0]. Only the tokens whose cumulative probability exceeds this threshold are considered.Cannot be used together with
temperature(Double).- Parameters:
topP- the nucleus sampling threshold- Returns:
this
-
topK
Sets the top-K sampling value. Only thetopKmost-likely next tokens are considered at each step.Recommended for advanced use only;
temperature(Double)is usually sufficient.- Parameters:
topK- the number of top tokens to sample from- Returns:
this
-
maxTokens
Sets the maximum number of tokens to generate in the response.Defaults to
1024if not set.- Parameters:
maxTokens- the maximum number of output tokens- Returns:
this
-
stopSequences
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder stopSequences(List<String> stopSequences) Sets sequences that, when generated, will cause the model to stop generating further tokens.- Parameters:
stopSequences- the list of stop sequences- Returns:
this
-
responseFormat
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder responseFormat(ResponseFormat responseFormat) Sets the response format, enabling structured output such as JSON mode.- Parameters:
responseFormat- the desired response format- Returns:
this
-
toolSpecifications
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder toolSpecifications(List<ToolSpecification> toolSpecifications) Sets the list of tools (functions) available to the model for function calling.- Parameters:
toolSpecifications- the tool specifications- Returns:
this
-
toolSpecifications
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder toolSpecifications(ToolSpecification... toolSpecifications) Sets the tools (functions) available to the model for function calling.- Parameters:
toolSpecifications- the tool specifications- Returns:
this
-
cacheSystemMessages
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder cacheSystemMessages(Boolean cacheSystemMessages) Enables prompt caching forSystemMessages.When
true, system messages are sent with thecache_controlheader to allow Anthropic to cache them across requests, reducing cost and latency for repeated prompts. See the prompt caching docs.- Parameters:
cacheSystemMessages- whether to cache system messages- Returns:
this
-
cacheTools
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder cacheTools(Boolean cacheTools) Enables prompt caching forToolSpecifications.When
true, tool definitions are sent with thecache_controlheader to allow Anthropic to cache them across requests. See the prompt caching docs.- Parameters:
cacheTools- whether to cache tool definitions- Returns:
this
-
thinkingType
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder thinkingType(String thinkingType) Enables thinking. -
thinkingBudgetTokens
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder thinkingBudgetTokens(Integer thinkingBudgetTokens) Configures thinking. -
thinkingDisplay
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder thinkingDisplay(String thinkingDisplay) Controls how thinking content is returned in the response stream.Valid values:
"summarized"and"omitted". On Claude Opus 4.7 the server default is"omitted"; on earlier Opus/Sonnet models the default is"summarized". Set to"summarized"explicitly on Opus 4.7+ to restore visible thinking text for UIs that stream it.- See Also:
-
returnThinking
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder returnThinking(Boolean returnThinking) Controls whether to return thinking/reasoning text (if available) insideAiMessage.thinking()and whether to invoke theStreamingChatResponseHandler.onPartialThinking(PartialThinking)callback. Please note that this does not enable thinking/reasoning for the LLM; it only controls whether to parse thethinkingfield from the API response and return it inside theAiMessage.Disabled by default. If enabled, the thinking text will be stored within the
AiMessageand may be persisted. If enabled, thinking signatures will also be stored and returned inside theAiMessage.attributes().- See Also:
-
sendThinking
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder sendThinking(Boolean sendThinking) Controls whether to send thinking/reasoning text to the LLM in follow-up requests.Enabled by default. If enabled, the contents of
AiMessage.thinking()will be sent in the API request. If enabled, thinking signatures (inside theAiMessage.attributes()) will also be sent.- See Also:
-
timeout
Sets the HTTP request timeout for calls to the Anthropic streaming API.- Parameters:
timeout- the request timeout- Returns:
this
-
logRequests
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder logRequests(Boolean logRequests) Enables debug logging of HTTP request bodies sent to the Anthropic API.- Parameters:
logRequests- whether to log requests- Returns:
this
-
logResponses
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder logResponses(Boolean logResponses) Enables debug logging of HTTP response bodies (server-sent events) received from the Anthropic API.- Parameters:
logResponses- whether to log responses- Returns:
this
-
logger
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder logger(org.slf4j.Logger logger) - Parameters:
logger- an alternateLoggerto be used instead of the default one provided by Langchain4J for logging requests and responses.- Returns:
this.
-
listeners
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder listeners(List<ChatModelListener> listeners) Sets the list ofChatModelListeners to be notified on each request and response. Useful for logging, metrics, and observability integrations.- Parameters:
listeners- the chat model listeners- Returns:
this
-
listeners
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder listeners(ChatModelListener... listeners) Sets theChatModelListeners to be notified on each request and response.- Parameters:
listeners- the chat model listeners- Returns:
this
-
toolChoice
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder toolChoice(ToolChoice toolChoice) Controls how the model uses tools.Use
ToolChoice.AUTOto let the model decide,ToolChoice.REQUIREDto force tool use, orToolChoice.NONEto disable tool use.- Parameters:
toolChoice- the tool choice strategy- Returns:
this
-
toolChoiceName
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder toolChoiceName(String toolChoiceName) Sets the name of the specific tool the model must use whenToolChoiceis set toToolChoice.REQUIRED.- Parameters:
toolChoiceName- the name of the tool to force- Returns:
this
-
disableParallelToolUse
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder disableParallelToolUse(Boolean disableParallelToolUse) When set totrue, prevents the model from calling multiple tools in a single response turn.- Parameters:
disableParallelToolUse- whether to disable parallel tool use- Returns:
this
-
serverTools
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder serverTools(List<AnthropicServerTool> serverTools) Specifies server tools to be included in each request to the Anthropic API. For example:AnthropicServerTool webSearchTool = AnthropicServerTool.builder() .type("web_search_20250305") .name("web_search") .addAttribute("max_uses", 5) .addAttribute("allowed_domains", List.of("accuweather.com")) .build(); -
serverTools
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder serverTools(AnthropicServerTool... serverTools) Specifies server tools to be included in each request to the Anthropic API. For example:AnthropicServerTool webSearchTool = AnthropicServerTool.builder() .type("web_search_20250305") .name("web_search") .addAttribute("max_uses", 5) .addAttribute("allowed_domains", List.of("accuweather.com")) .build(); -
returnServerToolResults
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder returnServerToolResults(Boolean returnServerToolResults) Controls whether to return server tool results (e.g., web_search, code_execution) insideAiMessage.attributes()under the key "server_tool_results".Disabled by default to avoid polluting ChatMemory with potentially large data. If enabled, server tool results will be stored as a
List<AnthropicServerToolResult>within the AiMessage attributes.- See Also:
-
toolMetadataKeysToSend
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder toolMetadataKeysToSend(Set<String> toolMetadataKeysToSend) Specifies metadata keys from theToolSpecification.metadata()to be included in the request. -
toolMetadataKeysToSend
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder toolMetadataKeysToSend(String... toolMetadataKeysToSend) Specifies metadata keys from theToolSpecification.metadata()to be included in the request. -
userId
Sets the user ID for the requests. This should be a uuid, hash value, or other opaque identifier. Anthropic may use this id to help detect abuse. Do not include any identifying information such as name, email address, or phone number.- Parameters:
userId- the user identifier- Returns:
- this builder
-
customParameters
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder customParameters(Map<String, Object> customParameters) Sets arbitrary extra parameters to include in the Anthropic API request body. Use this for experimental or provider-specific fields not yet covered by dedicated builder methods.- Parameters:
customParameters- a map of parameter names to values- Returns:
this
-
strictTools
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder strictTools(Boolean strictTools) Enables strict JSON schema validation for tool input parameters. Whentrue, the model enforces the exact schema defined inToolSpecification.- Parameters:
strictTools- whether to enable strict tool schema validation- Returns:
this
-
supportedCapabilities
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder supportedCapabilities(Capability... supportedCapabilities) Declares the capabilities supported by the model (e.g.Capability.RESPONSE_FORMAT_JSON_SCHEMA). This influences how LangChain4j generates requests for this model.- Parameters:
supportedCapabilities- the capabilities to declare- Returns:
this
-
supportedCapabilities
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder supportedCapabilities(Set<Capability> supportedCapabilities) Declares the capabilities supported by the model.- Parameters:
supportedCapabilities- the set of capabilities to declare- Returns:
this
-
customHeaders
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder customHeaders(Map<String, String> customHeaders) Sets extra HTTP headers to include in every request to the Anthropic API.- Parameters:
customHeaders- a map of header names to values- Returns:
this
-
customHeaders
public AnthropicStreamingChatModel.AnthropicStreamingChatModelBuilder customHeaders(Supplier<Map<String, String>> customHeadersSupplier) Sets a supplier of extra HTTP headers to include in every request to the Anthropic API. The supplier is called once per request, allowing dynamic header values.- Parameters:
customHeadersSupplier- a supplier that returns a map of header names to values- Returns:
this
-
build
-