The `count-tokens` endpoint lets you determine the number of tokens in a
message before sending it to Claude, helping you make informed decisions about
your prompts and usage.

There is no cost for using the `count-tokens` endpoint.

## Supported Claude models

The following models support count tokens:

- [Anthropic's Claude Sonnet 5.5 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-sonnet-5-5)
- [Anthropic's Claude Fable 5.2 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-fable-5-2)
- [Anthropic's Claude Fable 5.1 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-fable-5-1)
- [Anthropic's Claude Sonnet 5 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-sonnet-5)
- [Anthropic's Claude Opus 5.5 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-opus-5-5)
- [Anthropic's Claude Opus 5 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-opus-5)
- [Anthropic's Claude Fable 5 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-fable-5)
- [Anthropic's Claude Opus 4.8 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-opus-4-8)
- [Anthropic's Claude Opus 4.7 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-opus-4-7)
- [Anthropic's Claude Opus 4.6 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-opus-4-6)
- [Anthropic's Claude Sonnet 4.6 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-sonnet-4-6)
- [Anthropic's Claude Opus 4.5 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-opus-4-5)
- [Anthropic's Claude Sonnet 4.5 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-sonnet-4-5)
- [Anthropic's Claude Haiku 5.5 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-haiku-5-5)
- [Anthropic's Claude Haiku 4.5 on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-haiku-4-5)
- [Claude 3.5 Haiku on Google Cloud](https://console.cloud.google.com/agent-platform/publishers/anthropic/model-garden/claude-3-5-haiku)

<br />

## Supported regions

The following regions support count tokens:

- `global`
- `us`
- `eu`
- `asia-southeast1`

## Count tokens in basic messages

To count tokens, send a `rawPredict` request to the `count-tokens` endpoint. The
body of the request must contain the model ID of the model you want to count
tokens against.

> [!NOTE]
> **Note:** If your organization enforces the `constraints/vertexai.allowedModels` organization policy constraint with an allowlist, you must add `publishers/anthropic/models/count-tokens:predict` to your allowed values (`allowedValues`). Add this value in addition to the Claude model that you want to count tokens for (for example, `publishers/anthropic/models/claude-sonnet-5:predict`). For more information, see [Control access to Model Garden models](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/control-model-access.md.txt).

### REST


Before using any of the request data,
make the following replacements:

- <var class="edit" scope="LOCATION" translate="no">LOCATION</var>: A [region](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/partner-models/claude/count-tokens.md.txt#regions) that supports Anthropic Claude models. To use the global endpoint, see [Specify
  the global endpoint](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/partner-models/use-partner-models#global).
- <var class="edit" scope="MODEL" translate="no">MODEL</var>: The [model](https://docs.cloud.google.com/gemini-enterprise-agent-platform/models/partner-models/claude/count-tokens.md.txt#model-list) to count tokens against.
- <var translate="no">ROLE</var>: The role associated with a message. You can specify a `user` or an `assistant`. The first message must use the `user` role. Claude models operate with alternating `user` and `assistant` turns. If the final message uses the `assistant` role, then the response content continues immediately from the content in that message. You can use this to constrain part of the model's response.
- <var translate="no">CONTENT</var>: The content, such as text, of the `user` or `assistant` message.


HTTP method and URL:

```
POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/count-tokens:rawPredict
```


Request JSON body:

```
{
  "model": "MODEL",
  "messages": [
    {
      "role": "user",
      "content":"how many tokens are in this request?"
    }
  ],
}
```

To send your request, choose one of these options:

#### curl

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) , or by using [Cloud Shell](https://docs.cloud.google.com/shell/docs.md.txt), which automatically logs you into the `gcloud` CLI . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Save the request body in a file named `request.json`,
and execute the following command:

```
curl -X POST \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json; charset=utf-8" \
     -d @request.json \
     "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/count-tokens:rawPredict"
```

#### PowerShell

> [!NOTE]
> **Note:** The following command assumes that you have logged in to the `gcloud` CLI with your user account by running [`gcloud init`](https://docs.cloud.google.com/sdk/gcloud/reference/init) or [`gcloud auth login`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/login.md.txt) . You can check the currently active account by running [`gcloud auth list`](https://docs.cloud.google.com/sdk/gcloud/reference/auth/list.md.txt).


Save the request body in a file named `request.json`,
and execute the following command:

```
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
    -Method POST `
    -Headers $headers `
    -ContentType: "application/json; charset=utf-8" `
    -InFile request.json `
    -Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/anthropic/models/count-tokens:rawPredict" | Select-Object -Expand Content
```

You should receive a JSON response similar to the following.

#### Response

```
{ "input_tokens": 14 }
```

<br />

For information on how to count tokens in messages with tools, images, and PDFs,
see [Anthropic's documentation](https://docs.anthropic.com/en/docs/build-with-claude/token-counting).

## Quotas

By default, the quota for the `count-tokens` endpoint is 2000 requests per
minute.