Updated on 2025-07-28 GMT+08:00

Token Calculator

Function

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

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

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

deployment_id

Yes

String

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

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

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 Authentication.

Content-Type

Yes

String

MIME type of the request body. The value is application/json.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

data

Yes

List<String>

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

with_prompt

No

Boolean

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

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.

Response Parameters

Table 4 Response body parameters

Parameter

Type

Description

tokens

List<String>

Token list split from the text.

token_number

Integer

Total number of tokens.

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.