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": "user",
"content": "Hello",
"name": null,
"partial": false
}
]
}
'{
"data": {
"total_tokens": 80
}
}Estimate Tokens
Estimates the number of tokens that would be used for a given set of messages and model. The input structure is almost identical to that of chat completion.
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": "user",
"content": "Hello",
"name": null,
"partial": false
}
]
}
'{
"data": {
"total_tokens": 80
}
}The input structure for
If there is no
estimate-token-count is almost identical to that of chat completion.
Plain Text Call Example
Plain Text Call Example
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.6",
"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?"
}
]
}'
Vision Call Example
Vision Call Example
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].lstrip('.')};base64,{base64.b64encode(image_data).decode('utf-8')}"
payload = {
"model": "kimi-k2.6",
"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())
error field, you can take data.total_tokens as the calculation result.Authorizations
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 ID
Available options:
kimi-k2.7-code, kimi-k2.7-code-highspeed, kimi-k2.6, kimi-k2.5, 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 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
Show child attributes
Show child attributes
Response
Token count estimation
Show child attributes
Show child attributes
Was this page helpful?
⌘I