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
  c307d27a_5711_a0a0_93ca_406e62eb1e1d["count_tokens()"]
  8c902234_0aa5_f23d_186e_7c523acb8fed["Messages"]
  c307d27a_5711_a0a0_93ca_406e62eb1e1d -->|defined in| 8c902234_0aa5_f23d_186e_7c523acb8fed
  03c0ff6f_1277_a16b_a3c8_419f98e2dec3["count_tokens()"]
  03c0ff6f_1277_a16b_a3c8_419f98e2dec3 -->|calls| c307d27a_5711_a0a0_93ca_406e62eb1e1d
  03c0ff6f_1277_a16b_a3c8_419f98e2dec3["count_tokens()"]
  c307d27a_5711_a0a0_93ca_406e62eb1e1d -->|calls| 03c0ff6f_1277_a16b_a3c8_419f98e2dec3
  eedd35ac_a896_dc63_a3fe_c46cf577bffb["_validate_output_config_conflict()"]
  c307d27a_5711_a0a0_93ca_406e62eb1e1d -->|calls| eedd35ac_a896_dc63_a3fe_c46cf577bffb
  b3ae1af8_b549_4aef_c8c4_9309fd981a1a["_warn_output_format_deprecated()"]
  c307d27a_5711_a0a0_93ca_406e62eb1e1d -->|calls| b3ae1af8_b549_4aef_c8c4_9309fd981a1a
  618c478a_823f_e214_e25b_6e82a7278cf7["_merge_output_configs()"]
  c307d27a_5711_a0a0_93ca_406e62eb1e1d -->|calls| 618c478a_823f_e214_e25b_6e82a7278cf7
  style c307d27a_5711_a0a0_93ca_406e62eb1e1d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

src/anthropic/resources/beta/messages/messages.py lines 1598–1861

    def count_tokens(
        self,
        *,
        messages: Iterable[BetaMessageParam],
        model: ModelParam,
        context_management: Optional[BetaContextManagementConfigParam] | Omit = omit,
        mcp_servers: Iterable[BetaRequestMCPServerURLDefinitionParam] | Omit = omit,
        output_config: BetaOutputConfigParam | Omit = omit,
        output_format: Optional[BetaJSONOutputFormatParam] | Omit = omit,
        speed: Optional[Literal["standard", "fast"]] | Omit = omit,
        system: Union[str, Iterable[BetaTextBlockParam]] | Omit = omit,
        thinking: BetaThinkingConfigParam | Omit = omit,
        tool_choice: BetaToolChoiceParam | Omit = omit,
        tools: Iterable[message_count_tokens_params.Tool] | Omit = omit,
        betas: List[AnthropicBetaParam] | 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,
    ) -> BetaMessageTokensCount:
        """
        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:

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/beta/messages/messages.py.
Where is count_tokens() defined?
count_tokens() is defined in src/anthropic/resources/beta/messages/messages.py at line 1598.
What does count_tokens() call?
count_tokens() calls 4 function(s): _merge_output_configs, _validate_output_config_conflict, _warn_output_format_deprecated, 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