Count tokens in a Message

Changed on

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

Request

  • Base URL
    https://api.anthropic.com
  • Auth
    none declared

Headers

anthropic-versionstring

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.

anthropic-workspace-idstring

Optional header to select the Workspace for this request. The value is a Workspace ID (for example, wrkspc_011CZkZaBF1tNoB5wlCeusgy).

Only needed for credentials that can act on more than one Workspace. A credential that belongs to a specific Workspace may omit it; if sent, it must match that Workspace.

Request body

speed'standard' | 'fast'

Example request

{
  "messages": [
    {
      "content": "Hello, world",
      "role": "user"
    }
  ],
  "model": "claude-opus-5-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

    • breaking

      the / request property type changed from object to no type

    • warning

      removed the request property //

    • warning

      removed the request property //

    • info

      added assistant system user discriminator mapping keys to the / request property

    • info

      added to the / request property oneOf list

    • info

      added the new optional request property