🎉 Kimi K2.6 has been released with improved long-context coding stability.
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
}
}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.
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 forDocumentation Index
Fetch the complete documentation index at: https://platform.kimi.ai/docs/llms.txt
Use this file to discover all available pages before exploring further.
estimate-token-count is almost identical to that of chat completion.
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
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.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.
Model ID
kimi-k2.6, 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 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
Token count estimation
Show child attributes
Was this page helpful?