# get_num_tokens_from_messages

> **Method** in `langchain_anthropic`

📖 [View in docs](https://reference.langchain.com/python/langchain-anthropic/chat_models/ChatAnthropic/get_num_tokens_from_messages)

Count tokens in a sequence of input messages.

This uses Anthropic's official [token counting API](https://platform.claude.com/docs/en/build-with-claude/token-counting).

## Signature

```python
get_num_tokens_from_messages(
    self,
    messages: list[BaseMessage],
    tools: Sequence[dict[str, Any] | type | Callable | BaseTool] | None = None,
    kwargs: Any = {},
) -> int
```

## Description

???+ example "Basic usage"

    ```python
    from langchain_anthropic import ChatAnthropic
    from langchain_core.messages import HumanMessage, SystemMessage

    model = ChatAnthropic(model="claude-sonnet-4-5-20250929")

    messages = [
        SystemMessage(content="You are a scientist"),
        HumanMessage(content="Hello, Claude"),
    ]
    model.get_num_tokens_from_messages(messages)
    ```

    ```txt
    14
    ```

??? example "Pass tool schemas"

    ```python
    from langchain_anthropic import ChatAnthropic
    from langchain_core.messages import HumanMessage
    from langchain_core.tools import tool

    model = ChatAnthropic(model="claude-sonnet-4-5-20250929")

    @tool(parse_docstring=True)
    def get_weather(location: str) -> str:
        """Get the current weather in a given location

        Args:
            location: The city and state, e.g. San Francisco, CA
        """
        return "Sunny"

    messages = [
        HumanMessage(content="What's the weather like in San Francisco?"),
    ]
    model.get_num_tokens_from_messages(messages, tools=[get_weather])
    ```

    ```txt
    403
    ```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `messages` | `list[BaseMessage]` | Yes | The message inputs to tokenize. |
| `tools` | `Sequence[dict[str, Any] \| type \| Callable \| BaseTool] \| None` | No | If provided, sequence of `dict`, `BaseModel`, function, or `BaseTool` objects to be converted to tool schemas. (default: `None`) |
| `kwargs` | `Any` | No | Additional keyword arguments are passed to the Anthropic `messages.count_tokens` method. (default: `{}`) |

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/bbcdd568cd02b81e5ede5aa525744e79e67c2278/libs/partners/anthropic/langchain_anthropic/chat_models.py#L3050)