Token Count
Calculate the token count for a conversation. The request format is similar to the Chat Completion API.
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.
Expand/Collapse
-
System message
objectExpand/Collapse
rolestring
Alwayssystem.contentstring
System message text.
-
User message
objectExpand/Collapse
-
rolestring
Alwaysuser. -
contentstring or object array
User message content: either amultipartlist or a plain text string.Expand/Collapse
-
Plain textstring -
multipartlistobject array
Structured multimodal message.Expand/Collapse
-
Text message
objectExpand/Collapse
typestring
Alwaystext.textstring
Text content.
-
Image message
objectExpand/Collapse
-
typestring
Alwaysimage_url. -
image_urlobjectExpand/Collapse
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.
-
-
-
-
-
Assistant message
objectExpand/Collapse
rolestring
Alwaysassistant.contentstring | null
Assistant message text.
Response
dataobject
Token count result.Expand/Collapse
total_tokensint
Total tokens in the request.
Examples
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!"
}
]
}'Response
{
"data": {
"total_tokens": 85
}
}Last updated on