Home / Function/ count_tokens() — anthropic-sdk-python Function Reference

count_tokens() — anthropic-sdk-python Function Reference

Architecture documentation for the count_tokens() function in messages.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  ad2ba7ce_f9ac_913e_2c74_215533e7f0d3["count_tokens()"]
  bafd0838_b212_5f78_7082_11123724d722["AsyncMessages"]
  ad2ba7ce_f9ac_913e_2c74_215533e7f0d3 -->|defined in| bafd0838_b212_5f78_7082_11123724d722
  e7ad2d22_c063_ff41_ff5b_c612e290358f["count_tokens()"]
  e7ad2d22_c063_ff41_ff5b_c612e290358f -->|calls| ad2ba7ce_f9ac_913e_2c74_215533e7f0d3
  e7ad2d22_c063_ff41_ff5b_c612e290358f["count_tokens()"]
  ad2ba7ce_f9ac_913e_2c74_215533e7f0d3 -->|calls| e7ad2d22_c063_ff41_ff5b_c612e290358f
  style ad2ba7ce_f9ac_913e_2c74_215533e7f0d3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/resources/messages/messages.py lines 2653–2909

    async def count_tokens(
        self,
        *,
        messages: Iterable[MessageParam],
        model: ModelParam,
        output_config: OutputConfigParam | Omit = omit,
        output_format: None | JSONOutputFormatParam | type | Omit = omit,
        system: Union[str, Iterable[TextBlockParam]] | Omit = omit,
        thinking: ThinkingConfigParam | Omit = omit,
        tool_choice: ToolChoiceParam | Omit = omit,
        tools: Iterable[MessageCountTokensToolParam] | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> MessageTokensCount:
        """
        Count the number of tokens in a Message.

        The Token Count API can be used to count the number of tokens in a Message,
        including tools, images, and documents, without creating it.

        Learn more about token counting in our
        [user guide](https://docs.claude.com/en/docs/build-with-claude/token-counting)

        Args:
          messages: Input messages.

              Our models are trained to operate on alternating `user` and `assistant`
              conversational turns. When creating a new `Message`, you specify the prior
              conversational turns with the `messages` parameter, and the model then generates
              the next `Message` in the conversation. Consecutive `user` or `assistant` turns
              in your request will be combined into a single turn.

              Each input message must be an object with a `role` and `content`. You can
              specify a single `user`-role message, or you can include multiple `user` and
              `assistant` messages.

              If the final message uses the `assistant` role, the response content will
              continue immediately from the content in that message. This can be used to
              constrain part of the model's response.

              Example with a single `user` message:

              ```json
              [{ "role": "user", "content": "Hello, Claude" }]
              ```

              Example with multiple conversational turns:

              ```json
              [
                { "role": "user", "content": "Hello there." },
                { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" },
                { "role": "user", "content": "Can you explain LLMs in plain English?" }
              ]
              ```

              Example with a partially-filled response from Claude:

              ```json
              [
                {
                  "role": "user",
                  "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun"
                },
                { "role": "assistant", "content": "The best answer is (" }
              ]
              ```

              Each input message `content` may be either a single `string` or an array of
              content blocks, where each block has a specific `type`. Using a `string` for
              `content` is shorthand for an array of one content block of type `"text"`. The
              following input messages are equivalent:

              ```json
              { "role": "user", "content": "Hello, Claude" }
              ```

Subdomains

Called By

Frequently Asked Questions

What does count_tokens() do?
count_tokens() is a function in the anthropic-sdk-python codebase, defined in src/anthropic/resources/messages/messages.py.
Where is count_tokens() defined?
count_tokens() is defined in src/anthropic/resources/messages/messages.py at line 2653.
What does count_tokens() call?
count_tokens() calls 1 function(s): count_tokens.
What calls count_tokens()?
count_tokens() is called by 1 function(s): count_tokens.

Analyze Your Own Codebase

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

Try Supermodel Free