Class AnthropicTokenCountEstimator.Builder
java.lang.Object
dev.langchain4j.model.anthropic.AnthropicTokenCountEstimator.Builder
- Enclosing class:
AnthropicTokenCountEstimator
- Since:
- 1.4.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConfigures the estimator to automatically insert a minimal dummy user message ("ping") when the provided messages contain only system messages or are empty.addDummyUserMessageIfNoUserMessages(String dummyUserMessage) Configures the estimator to automatically insert a dummy user message with custom text when the provided messages contain only system messages or are empty.Sets 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()httpClientBuilder(HttpClientBuilder httpClientBuilder) Sets a customHttpClientBuilderfor the underlying HTTP client.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.modelName(AnthropicChatModelName modelName) Sets the model used for token count estimation using a type-safe enum constant.Sets the model used for token count estimation, specified as a string model ID.Sets the HTTP request timeout for calls to the Anthropic API.Sets the value of theanthropic-versionrequest header.
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
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.Alternatively, set the
ANTHROPIC_API_KEYenvironment variable.- 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
-
timeout
Sets the HTTP request timeout for calls to the Anthropic API.- Parameters:
timeout- the request timeout- 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
-
modelName
Sets the model used for token count estimation, specified as a string model ID.This field is required. See
AnthropicChatModelNamefor available model constants.- Parameters:
modelName- the model ID, e.g."claude-opus-4-5"- Returns:
this
-
modelName
Sets the model used for token count estimation using a type-safe enum constant.This field is required.
- Parameters:
modelName- the model name enum value- Returns:
this
-
addDummyUserMessageIfNoUserMessages
Configures the estimator to automatically insert a minimal dummy user message ("ping") when the provided messages contain only system messages or are empty.The Anthropic token-counting API requires at least one non-system message. Without this option, passing only system messages throws an
IllegalArgumentException.- Returns:
this- See Also:
-
addDummyUserMessageIfNoUserMessages
public AnthropicTokenCountEstimator.Builder addDummyUserMessageIfNoUserMessages(String dummyUserMessage) Configures the estimator to automatically insert a dummy user message with custom text when the provided messages contain only system messages or are empty.The Anthropic token-counting API requires at least one non-system message. Without this option, passing only system messages throws an
IllegalArgumentException.- Parameters:
dummyUserMessage- the text of the dummy user message to insert- Returns:
this- See Also:
-
build
-