diff --git a/docs-website/reference_versioned_docs/version-3.2-unstable/integrations-api/anthropic.md b/docs-website/reference_versioned_docs/version-3.2-unstable/integrations-api/anthropic.md new file mode 100644 index 0000000000..1ddb4641cf --- /dev/null +++ b/docs-website/reference_versioned_docs/version-3.2-unstable/integrations-api/anthropic.md @@ -0,0 +1,740 @@ +--- +title: "Anthropic" +id: integrations-anthropic +description: "Anthropic integration for Haystack" +slug: "/integrations-anthropic" +--- + + +## haystack_integrations.components.generators.anthropic.chat.chat_generator + +### AnthropicChatGenerator + +Completes chats using Anthropic's large language models (LLMs). + +It uses [ChatMessage](https://docs.haystack.deepset.ai/docs/data-classes#chatmessage) +format in input and output. Supports multimodal inputs including text and images. + +You can customize how the text is generated by passing parameters to the +Anthropic API. Use the `**generation_kwargs` argument when you initialize +the component or when you run it. Any parameter that works with +`anthropic.Message.create` will work here too. + +For details on Anthropic API parameters, see +[Anthropic documentation](https://docs.anthropic.com/en/api/messages). + +Usage example: + +```python +from haystack_integrations.components.generators.anthropic import ( + AnthropicChatGenerator, +) +from haystack.dataclasses import ChatMessage + +generator = AnthropicChatGenerator( + generation_kwargs={ + "max_tokens": 1000, + "temperature": 0.7, + }, +) + +messages = [ + ChatMessage.from_system( + "You are a helpful, respectful and honest assistant" + ), + ChatMessage.from_user("What's Natural Language Processing?"), +] +print(generator.run(messages=messages)) +``` + +Usage example with images: + +```python +from haystack.dataclasses import ChatMessage, ImageContent + +image_content = ImageContent.from_file_path("path/to/image.jpg") +messages = [ + ChatMessage.from_user( + content_parts=["What's in this image?", image_content] + ) +] +generator = AnthropicChatGenerator() +result = generator.run(messages) +``` + +#### SUPPORTED_MODELS + +```python +SUPPORTED_MODELS: list[str] = [ + "claude-fable-5-1", + "claude-fable-5", + "claude-opus-5", + "claude-opus-4-8", + "claude-opus-4-7", + "claude-opus-4-6", + "claude-opus-4-5-20251101", + "claude-sonnet-5", + "claude-sonnet-4-6", + "claude-sonnet-4-5-20250929", + "claude-haiku-4-5-20251001", +] + +``` + +A non-exhaustive list of chat models supported by this component. See +https://platform.claude.com/docs/en/about-claude/models/overview for the full list. + +#### __init__ + +```python +__init__( + api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"), + model: str = "claude-sonnet-4-5", + streaming_callback: StreamingCallbackT | None = None, + generation_kwargs: dict[str, Any] | None = None, + ignore_tools_thinking_messages: bool = True, + tools: ToolsType | None = None, + anthropic_server_tools: list[dict[str, Any]] | None = None, + *, + timeout: float | None = None, + max_retries: int | None = None +) -> None +``` + +Creates an instance of AnthropicChatGenerator. + +**Parameters:** + +- **api_key** (Secret) – The Anthropic API key +- **model** (str) – The name of the model to use. +- **streaming_callback** (StreamingCallbackT | None) – A callback function that is called when a new token is received from the stream. + The callback function accepts StreamingChunk as an argument. +- **generation_kwargs** (dict\[str, Any\] | None) – Other parameters to use for the model. These parameters are all sent directly to + the Anthropic endpoint. See Anthropic [documentation](https://docs.anthropic.com/claude/reference/messages_post) + for more details. + +Supported generation_kwargs parameters are: + +- `system`: The system message to be passed to the model. +- `max_tokens`: The maximum number of tokens to generate. Defaults to 8192. A response that hits + this limit is cut off; if the model was writing a tool call at the time, that call is dropped + and the reply carries a `length` finish reason. +- `metadata`: A dictionary of metadata to be passed to the model. +- `service_tier`: Whether the request may use priority capacity (`auto`) or standard capacity only + (`standard_only`). See [service tiers](https://platform.claude.com/docs/en/api/service-tiers). +- `stop_sequences`: A list of strings that the model should stop generating at. +- `temperature`: The temperature to use for sampling. +- `top_p`: The top_p value to use for nucleus sampling. +- `top_k`: The top_k value to use for top-k sampling. +- `extra_headers`: A dictionary of extra headers to be passed to the model (i.e. for beta features). +- `thinking`: A dictionary of thinking parameters to be passed to the model. + The `budget_tokens` passed for thinking should be less than `max_tokens`. + For more details and supported models, see: [Anthropic Extended Thinking](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking) +- `output_config`: A dictionary of output configuration options to be passed to the model. +- **ignore_tools_thinking_messages** (bool) – Anthropic's approach to tools (function calling) resolution involves a + "chain of thought" messages before returning the actual function names and parameters in a message. If + `ignore_tools_thinking_messages` is `True`, the generator will drop so-called thinking messages when tool + use is detected. See the Anthropic [tools](https://docs.anthropic.com/en/docs/tool-use#chain-of-thought-tool-use) + for more details. +- **tools** (ToolsType | None) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use. + Each tool should have a unique name. +- **anthropic_server_tools** (list\[dict\[str, Any\]\] | None) – A list of Anthropic server-side tools passed directly to the API. + Use this for native Anthropic tools such as web search (`{"type": "web_search_20250305"}`), + code execution tool, or other provider-managed tools. Refer to the + [Anthropic documentation](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/web-search-tool) + for the exact dict format each native tool expects. +- **timeout** (float | None) – Timeout for Anthropic client calls. If not set, it defaults to the default set by the Anthropic client. +- **max_retries** (int | None) – Maximum number of retries to attempt for failed requests. If not set, it defaults to the default set by + the Anthropic client. + +#### warm_up + +```python +warm_up() -> None +``` + +Create the synchronous Anthropic client. + +#### warm_up_async + +```python +warm_up_async() -> None +``` + +Create the asynchronous Anthropic client. + +#### close + +```python +close() -> None +``` + +Close the synchronous Anthropic client. + +#### close_async + +```python +close_async() -> None +``` + +Close the asynchronous Anthropic client. + +#### to_dict + +```python +to_dict() -> dict[str, Any] +``` + +Serialize this component to a dictionary. + +**Returns:** + +- dict\[str, Any\] – The serialized component as a dictionary. + +#### from_dict + +```python +from_dict(data: dict[str, Any]) -> AnthropicChatGenerator +``` + +Deserialize this component from a dictionary. + +**Parameters:** + +- **data** (dict\[str, Any\]) – The dictionary representation of this component. + +**Returns:** + +- AnthropicChatGenerator – The deserialized component instance. + +#### run + +```python +run( + messages: list[ChatMessage] | str, + streaming_callback: StreamingCallbackT | None = None, + generation_kwargs: dict[str, Any] | None = None, + tools: ToolsType | None = None, +) -> dict[str, list[ChatMessage]] +``` + +Invokes the Anthropic API with the given messages and generation kwargs. + +**Parameters:** + +- **messages** (list\[ChatMessage\] | str) – A list of ChatMessage instances representing the input messages. + If a string is provided, it is converted to a list containing a ChatMessage with user role. +- **streaming_callback** (StreamingCallbackT | None) – A callback function that is called when a new token is received from the stream. +- **generation_kwargs** (dict\[str, Any\] | None) – Optional arguments to pass to the Anthropic generation endpoint. These are merged + per key with the `generation_kwargs` passed at initialization: keys provided here take precedence, keys set + only at initialization are kept. +- **tools** (ToolsType | None) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use. + Each tool should have a unique name. If set, it will override the `tools` parameter set during component + initialization. + +**Returns:** + +- dict\[str, list\[ChatMessage\]\] – A dictionary with the following keys: +- `replies`: The responses from the model + +#### run_async + +```python +run_async( + messages: list[ChatMessage] | str, + streaming_callback: StreamingCallbackT | None = None, + generation_kwargs: dict[str, Any] | None = None, + tools: ToolsType | None = None, +) -> dict[str, list[ChatMessage]] +``` + +Async version of the run method. Invokes the Anthropic API with the given messages and generation kwargs. + +**Parameters:** + +- **messages** (list\[ChatMessage\] | str) – A list of ChatMessage instances representing the input messages. + If a string is provided, it is converted to a list containing a ChatMessage with user role. +- **streaming_callback** (StreamingCallbackT | None) – A callback function that is called when a new token is received from the stream. +- **generation_kwargs** (dict\[str, Any\] | None) – Optional arguments to pass to the Anthropic generation endpoint. These are merged + per key with the `generation_kwargs` passed at initialization: keys provided here take precedence, keys set + only at initialization are kept. +- **tools** (ToolsType | None) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use. + Each tool should have a unique name. If set, it will override the `tools` parameter set during component + initialization. + +**Returns:** + +- dict\[str, list\[ChatMessage\]\] – A dictionary with the following keys: +- `replies`: The responses from the model + +## haystack_integrations.components.generators.anthropic.chat.foundry_chat_generator + +### AnthropicFoundryChatGenerator + +Bases: AnthropicChatGenerator + +Enables text generation using Anthropic's Claude models via Azure Foundry. + +A variety of Claude models (Opus, Sonnet, Haiku, and others) are available through Azure Foundry. + +To use AnthropicFoundryChatGenerator, you must have an Azure subscription with Foundry enabled +and the desired Anthropic model deployed in your Foundry resource. + +For more details, refer to the [Anthropic Foundry documentation](https://github.com/anthropics/anthropic-sdk-python/blob/main/src/anthropic/lib/foundry.md). + +Any valid text generation parameters for the Anthropic messaging API can be passed to +the AnthropicFoundry API. Users can provide these parameters directly to the component via +the `generation_kwargs` parameter in `__init__` or the `run` method. + +For more details on the parameters supported by the Anthropic API, refer to the +Anthropic Message API [documentation](https://docs.anthropic.com/en/api/messages). + +```python +from haystack_integrations.components.generators.anthropic import AnthropicFoundryChatGenerator +from haystack.dataclasses import ChatMessage +from haystack.utils import Secret + +messages = [ChatMessage.from_user("What's Natural Language Processing?")] + +client = AnthropicFoundryChatGenerator( + model="claude-sonnet-4-5", + api_key=Secret.from_env_var("ANTHROPIC_FOUNDRY_API_KEY"), + resource="my-resource", +) + +response = client.run(messages) +print(response) +>> {'replies': [ChatMessage(_role=, _content=[TextContent(text= +>> "Natural Language Processing (NLP) is a field of artificial intelligence that +>> focuses on enabling computers to understand, interpret, and generate human language. It involves developing +>> techniques and algorithms to analyze and process text or speech data, allowing machines to comprehend and +>> communicate in natural languages like English, Spanish, or Chinese.")], +>> _name=None, _meta={'model': 'claude-sonnet-4-5', 'index': 0, 'finish_reason': 'end_turn', +>> 'usage': {'input_tokens': 15, 'output_tokens': 64}})]} +``` + +For more details on supported models and their capabilities, refer to the Anthropic +[documentation](https://docs.anthropic.com/claude/docs/intro-to-claude). + +#### SUPPORTED_MODELS + +```python +SUPPORTED_MODELS: list[str] = [ + "claude-fable-5-1", + "claude-fable-5", + "claude-opus-5", + "claude-opus-4-8", + "claude-opus-4-7", + "claude-opus-4-6", + "claude-opus-4-5", + "claude-sonnet-5", + "claude-sonnet-4-6", + "claude-sonnet-4-5", + "claude-haiku-4-5", +] + +``` + +A non-exhaustive list of chat models supported by this component. +The actual availability depends on your Azure Foundry resource configuration. + +#### __init__ + +```python +__init__( + *, + api_key: Secret | None = Secret.from_env_var( + "ANTHROPIC_FOUNDRY_API_KEY", strict=True + ), + resource: str | None = None, + endpoint: str | None = None, + model: str = "claude-sonnet-4-5", + streaming_callback: Callable[[StreamingChunk], None] | None = None, + generation_kwargs: dict[str, Any] | None = None, + ignore_tools_thinking_messages: bool = True, + tools: ToolsType | None = None, + anthropic_server_tools: list[dict[str, Any]] | None = None, + timeout: float | None = None, + max_retries: int | None = None, + azure_ad_token_provider: Callable[[], str] | None = None +) -> None +``` + +Creates an instance of AnthropicFoundryChatGenerator. + +**Parameters:** + +- **api_key** (Secret | None) – The API key to use for authentication. + Defaults to the `ANTHROPIC_FOUNDRY_API_KEY` environment variable. + Can be `None` when using `azure_ad_token_provider` instead. +- **resource** (str | None) – The Foundry resource name. Can also be set via the `ANTHROPIC_FOUNDRY_RESOURCE` + environment variable. Either `resource` or `endpoint` must be provided. +- **endpoint** (str | None) – The full Foundry endpoint URL (e.g., + "https://your-resource.openai.azure.com/anthropic"). + Either `resource` or `endpoint` must be provided. +- **model** (str) – The name of the model to use (deployment name in Foundry). +- **streaming_callback** (Callable\\[[StreamingChunk\], None\] | None) – A callback function that is called when a new token is received from the stream. + The callback function accepts StreamingChunk as an argument. +- **generation_kwargs** (dict\[str, Any\] | None) – Other parameters to use for the model. These parameters are all sent directly to + the AnthropicFoundry endpoint. See Anthropic [documentation](https://docs.anthropic.com/claude/reference/messages_post) + for more details. + Supported generation_kwargs parameters are: +- `system`: The system message to be passed to the model. +- `max_tokens`: The maximum number of tokens to generate. Defaults to 8192. A response that hits + this limit is cut off; if the model was writing a tool call at the time, that call is dropped + and the reply carries a `length` finish reason. +- `metadata`: A dictionary of metadata to be passed to the model. +- `service_tier`: Whether the request may use priority capacity (`auto`) or standard capacity only + (`standard_only`). See [service tiers](https://platform.claude.com/docs/en/api/service-tiers). +- `stop_sequences`: A list of strings that the model should stop generating at. +- `temperature`: The temperature to use for sampling. +- `top_p`: The top_p value to use for nucleus sampling. +- `top_k`: The top_k value to use for top-k sampling. +- `extra_headers`: A dictionary of extra headers to be passed to the model (i.e. for beta features). +- **ignore_tools_thinking_messages** (bool) – Anthropic's approach to tools (function calling) resolution involves a + "chain of thought" messages before returning the actual function names and parameters in a message. If + `ignore_tools_thinking_messages` is `True`, the generator will drop so-called thinking messages when tool + use is detected. See the Anthropic [tools](https://docs.anthropic.com/en/docs/tool-use#chain-of-thought-tool-use) + for more details. +- **tools** (ToolsType | None) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use. + Each tool should have a unique name. +- **anthropic_server_tools** (list\[dict\[str, Any\]\] | None) – A list of Anthropic server-side tools passed directly to the API. + Use this for native Anthropic tools such as web search (`{"type": "web_search_20250305"}`), + code execution tool, or other provider-managed tools. Refer to the + [Anthropic documentation](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/web-search-tool) + for the exact dict format each native tool expects. +- **timeout** (float | None) – Timeout for Anthropic client calls. If not set, it defaults to the default set by the Anthropic client. +- **max_retries** (int | None) – Maximum number of retries to attempt for failed requests. If not set, it defaults to the default set by + the Anthropic client. +- **azure_ad_token_provider** (Callable\[[], str\] | None) – A function that returns an Azure AD token for authentication. + Can be used instead of `api_key` for enhanced security. + See [Azure Identity documentation](https://learn.microsoft.com/en-us/azure/developer/python/sdk/authentication/overview) + for more details. + +#### warm_up + +```python +warm_up() -> None +``` + +Create the synchronous Anthropic Foundry client. + +#### warm_up_async + +```python +warm_up_async() -> None +``` + +Create the asynchronous Anthropic Foundry client. + +#### to_dict + +```python +to_dict() -> dict[str, Any] +``` + +Serialize this component to a dictionary. + +**Returns:** + +- dict\[str, Any\] – The serialized component as a dictionary. + +#### from_dict + +```python +from_dict(data: dict[str, Any]) -> AnthropicFoundryChatGenerator +``` + +Deserialize this component from a dictionary. + +**Parameters:** + +- **data** (dict\[str, Any\]) – The dictionary representation of this component. + +**Returns:** + +- AnthropicFoundryChatGenerator – The deserialized component instance. + +## haystack_integrations.components.generators.anthropic.chat.vertex_chat_generator + +### AnthropicVertexChatGenerator + +Bases: AnthropicChatGenerator + +Enables text generation using Anthropic's Claude models via the Anthropic Vertex AI API. + +A variety of Claude models (Opus, Sonnet, Haiku, and others) are available through the Vertex AI API endpoint. + +To use AnthropicVertexChatGenerator, you must have a GCP project with Vertex AI enabled. +Additionally, ensure that the desired Anthropic model is activated in the Vertex AI Model Garden. +Before making requests, you may need to authenticate with GCP using `gcloud auth login`. +For more details, refer to the [guide] (https://docs.anthropic.com/en/api/claude-on-vertex-ai). + +Any valid text generation parameters for the Anthropic messaging API can be passed to +the AnthropicVertex API. Users can provide these parameters directly to the component via +the `generation_kwargs` parameter in `__init__` or the `run` method. + +For more details on the parameters supported by the Anthropic API, refer to the +Anthropic Message API [documentation](https://docs.anthropic.com/en/api/messages). + +```python +from haystack_integrations.components.generators.anthropic import AnthropicVertexChatGenerator +from haystack.dataclasses import ChatMessage + +messages = [ChatMessage.from_user("What's Natural Language Processing?")] +client = AnthropicVertexChatGenerator( + model="claude-sonnet-4-5@20250929", + project_id="your-project-id", region="your-region" + ) +response = client.run(messages) +print(response) + +>> {'replies': [ChatMessage(_role=, _content=[TextContent(text= +>> "Natural Language Processing (NLP) is a field of artificial intelligence that +>> focuses on enabling computers to understand, interpret, and generate human language. It involves developing +>> techniques and algorithms to analyze and process text or speech data, allowing machines to comprehend and +>> communicate in natural languages like English, Spanish, or Chinese.")], +>> _name=None, _meta={'model': 'claude-sonnet-4-5@20250929', 'index': 0, 'finish_reason': 'end_turn', +>> 'usage': {'input_tokens': 15, 'output_tokens': 64}})]} +``` + +For more details on supported models and their capabilities, refer to the Anthropic +[documentation](https://docs.anthropic.com/claude/docs/intro-to-claude). + +For a list of available model IDs when using Claude on Vertex AI, see +[Claude on Vertex AI - model availability](https://platform.claude.com/docs/en/build-with-claude/claude-on-vertex-ai#model-availability). + +#### SUPPORTED_MODELS + +```python +SUPPORTED_MODELS: list[str] = [ + "claude-fable-5-1", + "claude-fable-5", + "claude-opus-5", + "claude-opus-4-8", + "claude-opus-4-7", + "claude-opus-4-6", + "claude-opus-4-5@20251101", + "claude-sonnet-5", + "claude-sonnet-4-6", + "claude-sonnet-4-5@20250929", + "claude-haiku-4-5@20251001", +] + +``` + +A non-exhaustive list of chat models supported by this component. See +https://platform.claude.com/docs/en/build-with-claude/claude-on-vertex-ai#model-availability for the full list. + +#### __init__ + +```python +__init__( + region: str, + project_id: str, + model: str = "claude-sonnet-4-5@20250929", + streaming_callback: Callable[[StreamingChunk], None] | None = None, + generation_kwargs: dict[str, Any] | None = None, + ignore_tools_thinking_messages: bool = True, + tools: ToolsType | None = None, + anthropic_server_tools: list[dict[str, Any]] | None = None, + *, + timeout: float | None = None, + max_retries: int | None = None +) -> None +``` + +Creates an instance of AnthropicVertexChatGenerator. + +**Parameters:** + +- **region** (str) – The region where the Anthropic model is deployed. Defaults to "us-central1". +- **project_id** (str) – The GCP project ID where the Anthropic model is deployed. +- **model** (str) – The name of the model to use. +- **streaming_callback** (Callable\\[[StreamingChunk\], None\] | None) – A callback function that is called when a new token is received from the stream. + The callback function accepts StreamingChunk as an argument. +- **generation_kwargs** (dict\[str, Any\] | None) – Other parameters to use for the model. These parameters are all sent directly to + the AnthropicVertex endpoint. See Anthropic [documentation](https://docs.anthropic.com/claude/reference/messages_post) + for more details. + +Supported generation_kwargs parameters are: + +- `system`: The system message to be passed to the model. +- `max_tokens`: The maximum number of tokens to generate. Defaults to 8192. A response that hits + this limit is cut off; if the model was writing a tool call at the time, that call is dropped + and the reply carries a `length` finish reason. +- `metadata`: A dictionary of metadata to be passed to the model. +- `service_tier`: Whether the request may use priority capacity (`auto`) or standard capacity only + (`standard_only`). See [service tiers](https://platform.claude.com/docs/en/api/service-tiers). +- `stop_sequences`: A list of strings that the model should stop generating at. +- `temperature`: The temperature to use for sampling. +- `top_p`: The top_p value to use for nucleus sampling. +- `top_k`: The top_k value to use for top-k sampling. +- `extra_headers`: A dictionary of extra headers to be passed to the model (i.e. for beta features). +- **ignore_tools_thinking_messages** (bool) – Anthropic's approach to tools (function calling) resolution involves a + "chain of thought" messages before returning the actual function names and parameters in a message. If + `ignore_tools_thinking_messages` is `True`, the generator will drop so-called thinking messages when tool + use is detected. See the Anthropic [tools](https://docs.anthropic.com/en/docs/tool-use#chain-of-thought-tool-use) + for more details. +- **tools** (ToolsType | None) – A list of Tool and/or Toolset objects, or a single Toolset, that the model can use. + Each tool should have a unique name. +- **anthropic_server_tools** (list\[dict\[str, Any\]\] | None) – A list of Anthropic server-side tools passed directly to the API. + On Vertex AI only the basic web search tool (`{"type": "web_search_20250305"}`) is available: + web search with dynamic filtering, web fetch and code execution are not supported. Refer to the + [Anthropic documentation](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/web-search-tool) + for the exact dict format each native tool expects. +- **timeout** (float | None) – Timeout for Anthropic client calls. If not set, it defaults to the default set by the Anthropic client. +- **max_retries** (int | None) – Maximum number of retries to attempt for failed requests. If not set, it defaults to the default set by + the Anthropic client. + +#### warm_up + +```python +warm_up() -> None +``` + +Create the synchronous Anthropic Vertex client. + +#### warm_up_async + +```python +warm_up_async() -> None +``` + +Create the asynchronous Anthropic Vertex client. + +#### to_dict + +```python +to_dict() -> dict[str, Any] +``` + +Serialize this component to a dictionary. + +**Returns:** + +- dict\[str, Any\] – The serialized component as a dictionary. + +#### from_dict + +```python +from_dict(data: dict[str, Any]) -> AnthropicVertexChatGenerator +``` + +Deserialize this component from a dictionary. + +**Parameters:** + +- **data** (dict\[str, Any\]) – The dictionary representation of this component. + +**Returns:** + +- AnthropicVertexChatGenerator – The deserialized component instance. + +## haystack_integrations.token_counters.anthropic.token_counter + +### AnthropicTokenCounter + +Counts input tokens for Anthropic models using the Anthropic token counting API. + +Uses the `POST /v1/messages/count_tokens` endpoint, which returns an exact token +count without generating a response or incurring generation costs. + +Usage example: + +```python +from haystack.dataclasses import ChatMessage +from haystack_integrations.token_counters.anthropic import AnthropicTokenCounter + +counter = AnthropicTokenCounter(model="claude-sonnet-4-5") +messages = [ + ChatMessage.from_system("You are a helpful assistant."), + ChatMessage.from_user("How many tokens is this?"), +] +token_count = counter.count(messages) +print(token_count) +``` + +#### __init__ + +```python +__init__( + model: str, + *, + api_key: Secret = Secret.from_env_var("ANTHROPIC_API_KEY"), + timeout: float | None = None, + max_retries: int | None = None +) -> None +``` + +Create an AnthropicTokenCounter. + +**Parameters:** + +- **model** (str) – The Anthropic model to use for tokenization. Token counts are + model-specific; always count against the model you intend to use. +- **api_key** (Secret) – The Anthropic API key. Defaults to the `ANTHROPIC_API_KEY` + environment variable. +- **timeout** (float | None) – HTTP timeout in seconds for the Anthropic client. +- **max_retries** (int | None) – Maximum number of retries for failed requests. + +#### warm_up + +```python +warm_up() -> None +``` + +Initialize the Anthropic client. + +#### close + +```python +close() -> None +``` + +Close the Anthropic client and release its underlying HTTP resources. + +#### count + +```python +count(messages: list[ChatMessage], tools: ToolsType | None = None) -> int +``` + +Count the tokens for the given messages and optional tools. + +**Parameters:** + +- **messages** (list\[ChatMessage\]) – The list of ChatMessages to count tokens for. +- **tools** (ToolsType | None) – Optional list of Tools whose schemas are included in the count. + +**Returns:** + +- int – The number of input tokens, or `0` when there is nothing to measure. + +#### to_dict + +```python +to_dict() -> dict[str, Any] +``` + +Serialize this token counter to a dictionary. + +**Returns:** + +- dict\[str, Any\] – The serialized token counter. + +#### from_dict + +```python +from_dict(data: dict[str, Any]) -> AnthropicTokenCounter +``` + +Deserialize a token counter from a dictionary. + +**Parameters:** + +- **data** (dict\[str, Any\]) – The dictionary to deserialize from. + +**Returns:** + +- AnthropicTokenCounter – The deserialized token counter.