Skip to main content
POST
/
v1
/
tokenizers
/
estimate-token-count
Estimate Token Count
curl --request POST \
  --url https://api.moonshot.ai/v1/tokenizers/estimate-token-count \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "model": "kimi-k2.5",
  "messages": [
    {
      "role": "system",
      "content": "<string>",
      "name": null,
      "partial": false
    }
  ]
}
'
{
  "data": {
    "total_tokens": 80
  }
}
The input structure for estimate-token-count is almost identical to that of chat completion.
curl 'https://api.moonshot.ai/v1/tokenizers/estimate-token-count' \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $MOONSHOT_API_KEY" \
  -d '{
    "model": "kimi-k2.5",
    "messages": [
        {
            "role": "system",
            "content": "You are Kimi, an AI assistant provided by Moonshot AI. You excel in Chinese and English conversations. You provide users with safe, helpful, and accurate answers. You refuse to answer any questions involving terrorism, racism, pornography, or violence. Moonshot AI is a proper noun and should not be translated into other languages."
        },
        {
            "role": "user",
            "content": "Hello, my name is Li Lei. What is 1+1?"
        }
    ]
}'
import os
import base64
import json
import requests

api_key = os.environ.get("MOONSHOT_API_KEY")
endpoint = "https://api.moonshot.ai/v1/tokenizers/estimate-token-count"
image_path = "image.png"

with open(image_path, "rb") as f:
    image_data = f.read()

# Encode the image to base64 format for the image_url
image_url = f"data:image/{os.path.splitext(image_path)[1]};base64,{base64.b64encode(image_data).decode('utf-8')}"

payload = {
    "model": "kimi-k2.5",
    "messages": [
        {
            "role": "system",
            "content": "You are Kimi, an AI assistant provided by Moonshot AI. You excel in Chinese and English conversations. You provide users with safe, helpful, and accurate answers. You refuse to answer any questions involving terrorism, racism, pornography, or violence. Moonshot AI is a proper noun and should not be translated into other languages."
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": image_url,
                    },
                },
                {
                    "type": "text",
                    "text": "Please describe the content of the image.",
                },
            ],
        }
    ]
}

response = requests.post(
    endpoint,
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    },
    data=json.dumps(payload)
)

print(response.json())
If there is no error field, you can take data.total_tokens as the calculation result.

Authorizations

Authorization
string
header
required

The Authorization header expects a Bearer token. Use an MOONSHOT_API_KEY as the token. This is a server-side secret key. Generate one on the API keys page in your dashboard.

Body

application/json
model
enum<string>
default:kimi-k2.5
required

Model ID

Available options:
kimi-k2.5,
kimi-k2-0905-preview,
kimi-k2-0711-preview,
kimi-k2-turbo-preview,
moonshot-v1-8k,
moonshot-v1-32k,
moonshot-v1-128k,
moonshot-v1-auto,
moonshot-v1-8k-vision-preview,
moonshot-v1-32k-vision-preview,
moonshot-v1-128k-vision-preview
messages
object[]
required

A list of messages in the conversation so far. Each element has the format {"role": "user", "content": "Hello"}. role supports system, user, or assistant. content must not be empty

Response

Token count estimation

data
object
required