Skip to Content
API ReferenceToken-count

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

  • model string required
    Model name to use.
  • messages object array required
    Conversation so far, including user and assistant messages.
Expand/Collapse
  • System message object

    Expand/Collapse
    • role string
      Always system.
    • content string
      System message text.
  • User message object

    Expand/Collapse
    • role string
      Always user.

    • content string or object array
      User message content: either a multipart list or a plain text string.

      Expand/Collapse
      • Plain text string

      • multipart list object array
        Structured multimodal message.

        Expand/Collapse
        • Text message object

          Expand/Collapse
          • type string
            Always text.
          • text string
            Text content.
        • Image message object

          Expand/Collapse
          • type string
            Always image_url.

          • image_url object

            Expand/Collapse
            • url string

              Image 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 object

    Expand/Collapse
    • role string
      Always assistant.
    • content string | null
      Assistant message text.

Response

  • data object
    Token count result.
    Expand/Collapse
    • total_tokens int

    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