Home / Function/ get_num_tokens_from_messages() — langchain Function Reference

get_num_tokens_from_messages() — langchain Function Reference

Architecture documentation for the get_num_tokens_from_messages() function in chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  69a9ca1f_89ff_e8cc_0a13_8af61d334b1d["get_num_tokens_from_messages()"]
  977b57b2_5d0e_bcf4_a43e_b52857105005["ChatAnthropic"]
  69a9ca1f_89ff_e8cc_0a13_8af61d334b1d -->|defined in| 977b57b2_5d0e_bcf4_a43e_b52857105005
  a1122c23_c68d_f096_a90e_42f0e4d39d61["_format_messages()"]
  69a9ca1f_89ff_e8cc_0a13_8af61d334b1d -->|calls| a1122c23_c68d_f096_a90e_42f0e4d39d61
  b9533de7_77f0_ac15_f27f_92372b897314["convert_to_anthropic_tool()"]
  69a9ca1f_89ff_e8cc_0a13_8af61d334b1d -->|calls| b9533de7_77f0_ac15_f27f_92372b897314
  style 69a9ca1f_89ff_e8cc_0a13_8af61d334b1d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/chat_models.py lines 1708–1793

    def get_num_tokens_from_messages(
        self,
        messages: list[BaseMessage],
        tools: Sequence[dict[str, Any] | type | Callable | BaseTool] | None = None,
        **kwargs: Any,
    ) -> int:
        """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).

        Args:
            messages: The message inputs to tokenize.
            tools: If provided, sequence of `dict`, `BaseModel`, function, or `BaseTool`
                objects to be converted to tool schemas.
            kwargs: Additional keyword arguments are passed to the Anthropic
                `messages.count_tokens` method.

        ???+ 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
            ```
        """  # noqa: D214
        formatted_system, formatted_messages = _format_messages(messages)
        if isinstance(formatted_system, str):
            kwargs["system"] = formatted_system
        if tools:
            kwargs["tools"] = [convert_to_anthropic_tool(tool) for tool in tools]
        if self.context_management is not None:
            kwargs["context_management"] = self.context_management

        if self.betas is not None:
            beta_response = self._client.beta.messages.count_tokens(
                betas=self.betas,
                model=self.model,
                messages=formatted_messages,  # type: ignore[arg-type]
                **kwargs,
            )
            return beta_response.input_tokens
        response = self._client.messages.count_tokens(

Domain

Subdomains

Frequently Asked Questions

What does get_num_tokens_from_messages() do?
get_num_tokens_from_messages() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/chat_models.py.
Where is get_num_tokens_from_messages() defined?
get_num_tokens_from_messages() is defined in libs/partners/anthropic/langchain_anthropic/chat_models.py at line 1708.
What does get_num_tokens_from_messages() call?
get_num_tokens_from_messages() calls 2 function(s): _format_messages, convert_to_anthropic_tool.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free