Calculate the token count for a conversation. The request format is similar to the Chat Completion API.Documentation Index
Fetch the complete documentation index at: https://platform.stepfun.ai/docs/llms.txt
Use this file to discover all available pages before exploring further.
Endpoint
POST https://api.stepfun.ai/v1/token/count
Request parameters
modelstringrequired
Model name to use.messagesobject arrayrequired
Conversation so far, including user and assistant messages.
Show child attributes
Show child attributes
-
System message
objectShow child attributes
rolestring
Alwayssystem.contentstring
System message text.
-
User message
objectShow child attributes
-
rolestring
Alwaysuser. -
contentstring or object array
User message content: either amultipartlist or a plain text string.Show child attributes
-
Plain textstring -
multipartlistobject array
Structured multimodal message.Show child attributes
-
Text message
objectShow child attributes
typestring
Alwaystext.textstring
Text content.
-
Image message
objectShow child attributes
-
typestring
Alwaysimage_url. -
image_urlobjectShow child attributes
urlstringImage URL or base64 string.
URLs support only http and https.
Base64 example:
data:image/jpeg;base64,${base64_string}(replace jpeg with your format and the encoded data).Supported image formats: jpg/jpeg, png, webp, static gif.
-
-
Text message
-
-
-
Assistant message
objectShow child attributes
rolestring
Alwaysassistant.contentstring | null
Assistant message text.
Response
-
dataobject
Token count result.Show child attributes
total_tokensint
Total tokens in the request.
Examples
- Text chat
- Mixed media
- Base64 image
curl https://api.stepfun.ai/v1/token/count \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $STEP_API_KEY" \
-d '{
"model": "step-1-8k",
"messages": [
{
"role": "system",
"content": "You are an AI assistant provided by StepFun. You excel at conversations in Chinese, English, and other languages. While keeping user data safe, you should answer quickly and accurately, and reject any content related to pornography, drugs, gambling, violence, or terrorism."
},
{
"role": "user",
"content": "Hi, please introduce StepFun'\''s AI!"
}
]
}'
{
"data": {
"total_tokens": 85
}
}
curl https://api.stepfun.ai/v1/token/count \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $STEP_API_KEY" \
-d '{
"model": "step-1v-8k",
"messages": [
{
"role": "system",
"content": "You are an AI assistant provided by StepFun. You excel at Chinese, English, and many other languages, and you can describe user-provided images precisely. Keep user data safe, answer quickly and accurately, and refuse content related to pornography, drugs, gambling, violence, or terrorism."
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this picture in elegant language."
},
{
"type": "image_url",
"image_url": {
"url": "https://www.stepfun.com/assets/section-1-CTe4nZiO.webp"
}
}
]
}
]
}'
{
"data": {
"total_tokens": 497
}
}
# Load an image into memory (demo only; read from a file as needed)
image_base64="data:image/webp;base64,"$(curl -s "https://www.stepfun.com/assets/section-1-CTe4nZiO.webp" | base64)
curl https://api.stepfun.ai/v1/token/count \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $STEP_API_KEY" \
-d "{
\"model\": \"step-1v-8k\",
\"messages\": [
{
\"role\": \"system\",
\"content\": \"You are an AI assistant provided by StepFun. You excel at Chinese, English, and many other languages, and you can describe user-provided images precisely. Keep user data safe, answer quickly and accurately, and refuse content related to pornography, drugs, gambling, violence, or terrorism.\"
},
{
\"role\": \"user\",
\"content\": [
{
\"type\": \"text\",
\"text\": \"Describe this image in elegant language.\"
},
{
\"type\": \"image_url\",
\"image_url\": {
\"url\": \"${image_base64}\"
}
}
]
}
]
}"
{
"data": {
"total_tokens": 497
}
}