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:
- Claude Opus 4.1
- Claude Opus 4
- Claude Sonnet 4.5
- Claude Sonnet 4
- Claude 3.7 Sonnet
- Claude 3.5 Sonnet v2
- Claude Haiku 4.5
- Claude 3.5 Haiku
- Claude 3.5 Sonnet
- Claude 3 Opus
- Claude 3 Haiku
Supported regions
The following regions support count tokens:
us-east5europe-west1asia-east1asia-southeast1us-central1europe-west4
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.
REST
Before using any of the request data, make the following replacements:
- LOCATION: A region that supports Anthropic Claude models. To use the global endpoint, see Specify the global endpoint.
- MODEL: The model to count tokens against.
- ROLE: The role associated with a
message. You can specify a
useror anassistant. The first message must use theuserrole. Claude models operate with alternatinguserandassistantturns. If the final message uses theassistantrole, then the response content continues immediately from the content in that message. You can use this to constrain part of the model's response. - CONTENT: The content, such as text, of the
userorassistantmessage.
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
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
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.
For information on how to count tokens in messages with tools, images, and PDFs, see Anthropic's documentation.
Quotas
By default, the quota for the count-tokens endpoint is 2000 requests per
minute.