Count input tokens

Count input tokens for a Messages request without generating a reply.

POST https://token.moi.matrixorigin.cn/v1/messages/count_tokens

Before you begin

  1. Prepare a personal access token or service account API key for Genesis. See Access credentials for credential selection and permissions.

  2. Use List callable models to select a model that supports Messages.

Request body

Replace $GENESIS_ACCESS_TOKEN and $MODEL_ID with your credential and the selected model ID.

curl -X POST \
  "https://token.moi.matrixorigin.cn/v1/messages/count_tokens" \
  -H "Authorization: Bearer $GENESIS_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
  "model": "'"$MODEL_ID"'",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Reply with OK."
        }
      ]
    }
  ]
}'

The fields below cover text messages; a count request does not require an output token limit.

Parameter

Type

Required

Description

model

string

Yes

Selected model ID from data[].id in the model list.

messages

array of object

Yes

Nonempty messages in conversation order.

messages[].role

string

Yes

Message role: user or assistant for text conversations.

messages[].content

string or array of object

Yes

Text or text blocks.

messages[].content[].type

string

Required for a text block

Use text for a text block.

messages[].content[].text

string

Required for a text block

Nonempty text content.

Successful response

A successful request returns the number of input tokens in the messages.

{
  "input_tokens": 12
}

Field

Type

Description

input_tokens

integer

Nonnegative input token count.

Error response

A missing model ID returns HTTP 400. Supply the model ID before sending the request again.

{
  "error": {
    "message": "missing required parameter: model",
    "type": "invalid_request",
    "code": "invalid_request"
  }
}

Field

Type

Description

error

object

Error details.

error.message

string

Error description; the example indicates a missing model ID.

error.type

string

Error type; may be omitted by a model service.

error.code

string or null

Error code; may be omitted by a model service.

Next steps

After checking the input length, use the same model and messages to Create a Message.

Last updated on