Updated on 2026-01-07 GMT+08:00

Token Calculator

Function

To help you better manage tokens and optimize token usage, the platform provides the token calculator. The token calculator can help you evaluate the number of tokens in a text before model inference, estimate the cost, and optimize the data preprocessing policy.

Authorization Information

An account has required permissions to call all APIs by default. To call this API as an IAM user, the IAM user must be granted the required permissions. For details, see Permissions and Supported Actions.

URI

POST /v1/{project_id}/deployments/{deployment_id}/caltokens

For details about how to obtain the URI, see Request URI.

Table 1 URI parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Definition:

Project ID. For details about how to obtain a project ID, see Obtaining the Project ID.

Constraints:

N/A

Value range:

N/A

Default value:

N/A

deployment_id

Yes

String

Definition:

Model deployment ID. For details about how to obtain the deployment ID, see Obtaining the Model Deployment ID.

Constraints:

N/A

Value range:

N/A

Default value:

N/A

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

Definition:

User token.

Used to obtain the permission required to call APIs. The token is the value of X-Subject-Token in the response header in Figure 4.

Constraints:

N/A

Value range:

N/A

Default value:

N/A

Content-Type

Yes

String

Definition:

MIME type of the body in the request.

Constraints:

N/A

Value range:

N/A

Default value:

application/json

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

data

Yes

List<String>

Definition:

Character string of the number of tokens to be counted. The list length must be an odd number.

Constraints:

N/A

Value range:

N/A

Default value:

N/A

with_prompt

No

Boolean

Definition:

Whether to calculate only the number of tokens for input characters.

Constraints:

N/A

Value range:

  • true: Only the number of tokens for the input character string is calculated.
  • false: Total number of tokens for input character string and characters generated during inference.

Default value:

N/A

Response Parameters

Table 4 Response body parameters

Parameter

Type

Description

tokens

List<String>

Definition:

Token list split from the text.

Constraints:

N/A

Value range:

N/A

Default value:

N/A

token_number

Integer

Definition:

Total number of tokens.

Constraints:

N/A

Value range:

N/A

Default value:

N/A

Example Request

{
    "data": [
        "Hello, please introduce Xi'an."
    ],
    "with_prompt": true
}

Example Response

{
    "tokens": [
        "Hello",
        ",",
        "Please",
        "introduce",
        "Xi'an",
        "."
    ],
    "token_number": 6
}

Status Codes

For details, see Status Codes.

Error Codes

For details, see Error Codes.