🎉 New kimi k2.5 Multi-modal Model released! Now supports multimodal understanding and processing.
Docs
API Reference
Estimate Token

Estimate Tokens

This API is used to calculate the token count for a request (including both plain text input and visual input).

Request Address

POST https://api.moonshot.ai/v1/tokenizers/estimate-token-count

Request Content

The input structure for estimate-token-count is almost identical to that of chat completion.

Example

{
    "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?" }
    ]
}

Parameters

FieldDescriptionTypeValues
messagesA list of messages in the conversation so far.List[Dict]This is a list of structures, with each element similar to: json{"role": "user", "content": "Hello"} The role can only be one of system, user, assistant, and the content must not be empty
modelModel ID, which can be obtained through List ModelsstringCurrently one of 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

Example Request

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?"
        }
    ]
}'
  • Visual input request
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()
 
# We use the built-in base64.b64encode function to encode the image into a base64 formatted 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", # <-- Use the image_url type to upload the image, the content is the base64 encoded image
                    "image_url": {
                        "url": image_url,
                    },
                },
                {
                    "type": "text",
                    "text": "Please describe the content of this image.", # <-- Use the text type to provide text instructions, such as "Describe the image content"
                },
            ],
        }
    ]
}
 
response = requests.post(
    endpoint,
    headers={
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    },
    data=json.dumps(payload)
)
 
print(response.json())

Response

{
    "data": {
        "total_tokens": 80
    }
}

If there is no error field, you can take data.total_tokens as the calculation result.