Count tokens in a Message

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

post/v1/messages/count_tokens

Headers

anthropic-versionstring

The version of the Claude API you want to use.

Read more about versioning and our version history here.

The version of the Claude API you want to use.

Read more about versioning and our version history here.

anthropic-user-profile-idstring

The user profile ID to attribute this request to. Use when acting on behalf of a party other than your organization. Requires the user-profiles beta header.

The user profile ID to attribute this request to. Use when acting on behalf of a party other than your organization. Requires the user-profiles beta header.

Request body

Example request

{
  "messages": [
    {
      "content": "Hello, world",
      "role": "user"
    }
  ],
  "model": "claude-opus-5"
}

Response

Successful Response

input_tokensinteger required

The total number of tokens across the provided list of messages, system prompt, and tools.

Example response

{
  "input_tokens": 2095
}

Changes

Changed in 22 of the 68 revisions of this API.123142

    • removed the request property ///

      request-property-removed

    • removed the request property ///

      request-property-removed

    • added subschema #2 subschema #3 to the request property anyOf list

      request-property-any-of-added

    This revision also has 1 change that name no endpoint, such as unreferenced schemas being removed. See the revision's changelog

    • added the new optional request property ///////////////

      new-optional-request-property

    • added the new optional request property //////

      new-optional-request-property

    • added the new optional request property ////////////

      new-optional-request-property

    • added the new optional request property //////

      new-optional-request-property

    • added the new optional request property //////////

      new-optional-request-property

    • added the new optional request property ////////////////

      new-optional-request-property

    • added the new optional request property //////

      new-optional-request-property

    • added browser_state discriminator mapping keys to the //////// request property

      request-property-discriminator-mapping-added

    • added file discriminator mapping keys to the ///////// request property

      request-property-discriminator-mapping-added

    • added file discriminator mapping keys to the /////////////// request property

      request-property-discriminator-mapping-added

    • added file discriminator mapping keys to the ////// request property

      request-property-discriminator-mapping-added

    • added file discriminator mapping keys to the ////// request property

      request-property-discriminator-mapping-added

    • added file discriminator mapping keys to the //////////// request property

      request-property-discriminator-mapping-added

    • added file discriminator mapping keys to the ////////// request property

      request-property-discriminator-mapping-added

    • added file discriminator mapping keys to the ////////// request property

      request-property-discriminator-mapping-added

    • added file discriminator mapping keys to the //////////////// request property

      request-property-discriminator-mapping-added

    • added to the / request property oneOf list

      request-property-one-of-added

    • added to the ///////// request property oneOf list

      request-property-one-of-added

    • added to the ////// request property oneOf list

      request-property-one-of-added

    • added to the ////////// request property oneOf list

      request-property-one-of-added

    • added to the /////////////// request property oneOf list

      request-property-one-of-added

    • added to the ////// request property oneOf list

      request-property-one-of-added

    • added to the //////////// request property oneOf list

      request-property-one-of-added

    • added to the ////////// request property oneOf list

      request-property-one-of-added

    • added to the //////////////// request property oneOf list

      request-property-one-of-added

    • added to the //////// request property oneOf list

      request-property-one-of-added

  • e9d8152420c311See the full diff
    • removed from the //// request property oneOf list

      request-property-one-of-removed

    • removed mid_conv_system discriminator mapping keys from the //// request property

      request-property-discriminator-mapping-deleted

    This revision also has 2 changes that name no endpoint, such as unreferenced schemas being removed. See the revision's changelog

    • the //////////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the //////////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the //////////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the /////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the /////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the /////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the /////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the /////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the /////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the ///////////////////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the //////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the //////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • the //////// request property's maxLength was increased from 255 to 500

      request-property-max-length-increased

    • removed subschema #17 subschema #18 from the request property anyOf list

      request-property-any-of-removed

  • a97790bd7f1811See the full diff
    • removed subschema #5 subschema #6 subschema #8 subschema #12 subschema #13 subschema #16 subschema #17 from the request property anyOf list

      request-property-any-of-removed

    • added subschema #5 subschema #6 subschema #7 subschema #9 subschema #13 subschema #14 subschema #17 subschema #18 to the request property anyOf list

      request-property-any-of-added

    • added subschema #2 to the request property anyOf list

      request-property-any-of-added

    • added to the / request property oneOf list

      request-property-one-of-added

    • added the new optional header request parameter anthropic-user-profile-id

      new-optional-request-parameter

  • 063bcb9d6cf9117See the full diff
    • removed subschema #17 subschema #18 subschema #19 subschema #20 subschema #21 from the request property anyOf list

      request-property-any-of-removed

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added the new code_execution_20260521 enum value to the request property ////

      request-property-enum-value-added

    • added to the / request property oneOf list

      request-property-one-of-added

    This revision also has 1 change that name no endpoint, such as unreferenced schemas being removed. See the revision's changelog