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¶
Prepare a personal access token or service account API key for Genesis. See Access credentials for credential selection and permissions.
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 |
|---|---|---|---|
|
string |
Yes |
Selected model ID from |
|
array of object |
Yes |
Nonempty messages in conversation order. |
|
string |
Yes |
Message role: |
|
string or array of object |
Yes |
Text or text blocks. |
|
string |
Required for a text block |
Use |
|
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 |
|---|---|---|
|
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 |
|---|---|---|
|
object |
Error details. |
|
string |
Error description; the example indicates a missing model ID. |
|
string |
Error type; may be omitted by a model service. |
|
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.