# Count input tokens

```{raw} html
<div class="mo-api-page-show-toc" aria-hidden="true"></div>
```

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

```text
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](../../../../guides/billing/credentials.md#choose-credential-type) for credential selection and permissions.
2. Use [List callable models](../models.md) to select a model that supports Messages.

## Request body

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

:::::::{div} mo-api-tabs
::::::{tab-set}
:::::{tab-item} Request example

```bash
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."
        }
      ]
    }
  ]
}'
```

:::::
:::::{tab-item} Parameters

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

::::{div} mo-api-parameter-table

| 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.

:::::::{div} mo-api-tabs mo-api-response-tabs
::::::{tab-set}
:::::{tab-item} Response example

```json
{
  "input_tokens": 12
}
```

:::::
:::::{tab-item} Fields

| 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.

:::::::{div} mo-api-tabs mo-api-response-tabs
::::::{tab-set}
:::::{tab-item} Response example

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

:::::
:::::{tab-item} Fields

| 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](anthropic-messages.md).
