Class AnthropicChatModel.AnthropicChatModelBuilder
- Enclosing class:
AnthropicChatModel
-
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()cacheAutomatically(Boolean cacheAutomatically) Enables automatic prompt caching of the conversation.cacheSystemMessages(Boolean cacheSystemMessages) Enables prompt caching forSystemMessages.cacheTools(Boolean cacheTools) Enables prompt caching forToolSpecifications.Sets how long the cached prompt is kept:AnthropicChatRequestParameters.CACHE_TTL_5M(5 minutes, the default) orAnthropicChatRequestParameters.CACHE_TTL_1H(1 hour).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.defaultRequestParameters(ChatRequestParameters parameters) Sets defaultChatRequestParametersthat are merged into every request.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 received from the Anthropic API.maxRetries(Integer maxRetries) Sets the number of times to retry a request on transient errors (e.g. rate limits, server errors).Sets the maximum number of tokens to generate in the response.midConversationSystemMessages(Boolean midConversationSystemMessages) Controls whether aSystemMessagethat appears after the conversation has started (a mid-conversation system message) is sent inline as asystementry in themessagesarray, instead of being merged into the top-levelsystemprompt.modelName(AnthropicChatModelName modelName) Sets the model to use for chat completions using a type-safe enum constant.Sets the model to use for chat completions, specified as a string model ID.responseFormat(ResponseFormat responseFormat) Sets the response format, enabling structured output such as JSON mode.returnCacheDiagnostics(Boolean returnCacheDiagnostics) Enables Anthropic's (beta) cache diagnostics for every request.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().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.skills(AnthropicSkill... skills) skills(List<AnthropicSkill> skills) Enables Anthropic Agent Skills so Claude can generate real downloadable documents (e.g.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 whether the API returns readable thinking text next to the thinking signature.thinkingType(String thinkingType) Enables thinking.Sets the HTTP request timeout for calls to the Anthropic API.toolChoice(ToolChoice toolChoice) Controls how the model uses tools.toolChoiceName(String toolChoiceName) Sets the name of the tool that the model is forced to call.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
-
AnthropicChatModelBuilder
public AnthropicChatModelBuilder()
-
-
Method Details
-
httpClientBuilder
public AnthropicChatModel.AnthropicChatModelBuilder 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 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
Sets the model to use for chat completions using a type-safe enum constant.- Parameters:
modelName- the model name enum value- Returns:
this
-
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
Sets sequences that, when generated, will cause the model to stop generating further tokens.- Parameters:
stopSequences- the list of stop sequences- Returns:
this
-
responseFormat
Sets the response format, enabling structured output such as JSON mode.- Parameters:
responseFormat- the desired response format- Returns:
this
-
toolSpecifications
public AnthropicChatModel.AnthropicChatModelBuilder 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 AnthropicChatModel.AnthropicChatModelBuilder toolSpecifications(ToolSpecification... toolSpecifications) Sets the tools (functions) available to the model for function calling.- Parameters:
toolSpecifications- the tool specifications- Returns:
this
-
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
Sets the name of the tool that the model is forced to call.It can be set on its own; it does not require
toolChoice(ToolChoice). WhentoolChoice(ToolChoice)is set as well, the named tool takes precedence over it. It has no effect when the request contains no tools.- Parameters:
toolChoiceName- the name of the tool to force- Returns:
this
-
disableParallelToolUse
public AnthropicChatModel.AnthropicChatModelBuilder 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 AnthropicChatModel.AnthropicChatModelBuilder 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(); -
returnServerToolResults
public AnthropicChatModel.AnthropicChatModelBuilder 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:
-
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(); -
skills
Enables Anthropic Agent Skills so Claude can generate real downloadable documents (e.g..xlsx,.pptx,.docx,.pdf). For example:AnthropicChatModel model = AnthropicChatModel.builder() .apiKey(System.getenv("ANTHROPIC_API_KEY")) .modelName("claude-opus-4-8") .maxTokens(4096) .skills(AnthropicSkill.XLSX, AnthropicSkill.PPTX) .returnServerToolResults(true) .build();Enabling skills automatically adds thecontainer.skillsblock and thecode_executionserver tool (unless already configured viaserverTools(List)), so that does not need to be wired up manually.You must, however, opt into the required beta features yourself via
beta(String), for example.beta("code-execution-2025-08-25,skills-2025-10-02,files-api-2025-04-14"). These are beta headers and their values change over time; see the Agent Skills docs for the current set.Combine with
returnServerToolResults(Boolean)to surface the generated file ids under the"server_tool_results"key ofAiMessage.attributes().Skills are supported on Claude Sonnet 4 / 4.5, Opus 4 and later. At most 8 skills may be enabled per request.
-
skills
- See Also:
-
toolMetadataKeysToSend
public AnthropicChatModel.AnthropicChatModelBuilder toolMetadataKeysToSend(Set<String> toolMetadataKeysToSend) Specifies metadata keys from theToolSpecification.metadata()to be included in the request. -
toolMetadataKeysToSend
public AnthropicChatModel.AnthropicChatModelBuilder toolMetadataKeysToSend(String... toolMetadataKeysToSend) Specifies metadata keys from theToolSpecification.metadata()to be included in the request. -
cacheSystemMessages
public AnthropicChatModel.AnthropicChatModelBuilder cacheSystemMessages(Boolean cacheSystemMessages) Enables prompt caching forSystemMessages.When
true, the last system message is marked withcache_control, so that Anthropic caches the tools and system messages across requests, reducing cost and latency for repeated prompts. It works for any kind of usage, including independent calls without chat memory. To also cache a conversation whose history grows from one request to the next, enablecacheAutomaticallyas well. See the prompt caching docs.- Parameters:
cacheSystemMessages- whether to cache system messages- Returns:
this
-
cacheTools
Enables prompt caching forToolSpecifications.When
true, the last tool definition is marked withcache_control, so that Anthropic caches the tool definitions across requests. To also cache a conversation whose history grows from one request to the next, enablecacheAutomaticallyas well. See the prompt caching docs.- Parameters:
cacheTools- whether to cache tool definitions- Returns:
this
-
cacheAutomatically
Enables automatic prompt caching of the conversation.When
true, Anthropic places a cache breakpoint on the last cacheable block of each request and moves it forward as the conversation grows, so that every request reads the whole previous conversation (tools, system messages and earlier messages) from the cache instead of processing it again. No message has to be marked for caching by hand, so it also works for AI Services and agents.It only pays off when each request starts with everything the previous request sent, for example when an AI Service or an agent calls tools in a loop, or in a chat whose memory still holds all previous messages. When the beginning of the conversation changes on every request (for example, when a chat memory such as a full MessageWindowChatMemory evicts old messages on every turn, or for independent calls without chat memory), every request pays the cache write price and nothing is read back, which costs more than not caching at all.
When enabling it, enable
cacheSystemMessagesandcacheToolsas well, to keep the system messages and tools cached even when the beginning of the conversation changes. Each of these options, as well as each message marked with thecache_controlattribute, uses one of the 4 cache breakpoints Anthropic allows per request; a request with more is rejected.This option is sent as a top-level
cache_controlfield of the request. Anthropic-compatible gateways and proxies that do not support this field may reject the request or ignore the field.Writing to the cache costs more than regular input tokens, and reading from it costs much less, so caching pays off when the same prefix is sent more than once within the cache TTL (see
cacheTtl). Prompts shorter than the model-specific minimum are not cached. Disabled by default. See the prompt caching docs.- Parameters:
cacheAutomatically- whether to enable automatic caching of the conversation- Returns:
this
-
cacheTtl
Sets how long the cached prompt is kept:AnthropicChatRequestParameters.CACHE_TTL_5M(5 minutes, the default) orAnthropicChatRequestParameters.CACHE_TTL_1H(1 hour).Applies to every cache breakpoint: system messages, tools, automatic conversation caching and messages marked with the
cache_controlattribute. It has no effect unless at least one of them is cached. The value is sent to Anthropic as is; a blank value is rejected. Each cache hit refreshes the TTL, so"5m"is enough when requests sharing the same prefix are less than 5 minutes apart. Use"1h"when they are further apart (for example, a user replying after 20 minutes, or batch processing). Writing to the 1-hour cache costs more than writing to the 5-minute one. See the prompt caching docs.- Parameters:
cacheTtl- the cache TTL,AnthropicChatRequestParameters.CACHE_TTL_5MorAnthropicChatRequestParameters.CACHE_TTL_1H- Returns:
this
-
thinkingType
Enables thinking. -
thinkingBudgetTokens
public AnthropicChatModel.AnthropicChatModelBuilder thinkingBudgetTokens(Integer thinkingBudgetTokens) Configures thinking. -
thinkingDisplay
Controls whether the API returns readable thinking text next to the thinking signature.Valid values:
"summarized": thinking blocks contain a readable summary of the reasoning."omitted": thinking blocks contain an empty thinking text, only the encrypted signature is returned.
"omitted", older ones to"summarized". Set it to"summarized"whenever the thinking text itself is needed, for example in order to show it to the end user.The model thinks and is billed the same way in both cases; only the visibility of the thinking text changes.
- See Also:
-
returnThinking
Controls whether to return thinking/reasoning text (if available) insideAiMessage.thinking(). 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().Please note that
AiMessage.thinking()stays empty when the API returns no thinking text, which is the default for recent Claude models. SeethinkingDisplay(String).- See Also:
-
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:
-
midConversationSystemMessages
public AnthropicChatModel.AnthropicChatModelBuilder midConversationSystemMessages(Boolean midConversationSystemMessages) Controls whether aSystemMessagethat appears after the conversation has started (a mid-conversation system message) is sent inline as asystementry in themessagesarray, instead of being merged into the top-levelsystemprompt.Disabled by default, in which case every
SystemMessageis sent via the top-levelsystemprompt regardless of position (the existing behavior). When enabled, leadingSystemMessages still populate the top-levelsystemprompt, while later ones are sent inline so they take effect from that point in the conversation onward.Supported only by Claude Opus 4.8. Anthropic also constrains placement: a mid-conversation system message must immediately follow a
userturn (including auserturn carrying tool results) and must not sit between atool_useblock and itstool_result; an unsupported model or an invalid placement returns a 400. This library does not reorder messages; it sends them at the position the caller provided.- Parameters:
midConversationSystemMessages- whether to send mid-conversation system messages inline- Returns:
this- See Also:
-
timeout
Sets the HTTP request timeout for calls to the Anthropic API.- Parameters:
timeout- the request timeout- Returns:
this
-
maxRetries
Sets the number of times to retry a request on transient errors (e.g. rate limits, server errors).Defaults to
2.- Parameters:
maxRetries- the maximum number of retry attempts- Returns:
this
-
logRequests
Enables debug logging of HTTP request bodies sent to the Anthropic API.- Parameters:
logRequests- whether to log requests- Returns:
this
-
logResponses
Enables debug logging of HTTP response bodies received from the Anthropic API.- Parameters:
logResponses- whether to log responses- Returns:
this
-
logger
- Parameters:
logger- an alternateLoggerto be used instead of the default one provided by Langchain4J for logging requests and responses.- Returns:
this.
-
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
Sets theChatModelListeners to be notified on each request and response.- Parameters:
listeners- the chat model listeners- Returns:
this
-
defaultRequestParameters
public AnthropicChatModel.AnthropicChatModelBuilder defaultRequestParameters(ChatRequestParameters parameters) Sets defaultChatRequestParametersthat are merged into every request. Individual request parameters take precedence over these defaults.- Parameters:
parameters- the default request parameters- Returns:
this
-
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
-
returnCacheDiagnostics
public AnthropicChatModel.AnthropicChatModelBuilder returnCacheDiagnostics(Boolean returnCacheDiagnostics) Enables Anthropic's (beta) cache diagnostics for every request.Requires the
cache-diagnosis-2026-04-07beta header to also be set viabeta(String). There is intentionally no model-level default for theidto compare against — it must be set per-request viaAnthropicChatRequestParameters.Builder.previousMessageId(String); seeAnthropicChatRequestParameters.previousMessageId()for why.- See Also:
-
customParameters
public AnthropicChatModel.AnthropicChatModelBuilder 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
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
-
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 AnthropicChatModel.AnthropicChatModelBuilder 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
-
supportedCapabilities
public AnthropicChatModel.AnthropicChatModelBuilder 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 AnthropicChatModel.AnthropicChatModelBuilder supportedCapabilities(Set<Capability> supportedCapabilities) Declares the capabilities supported by the model.- Parameters:
supportedCapabilities- the set of capabilities to declare- Returns:
this
-
build
-