Method: models.countTokens

Runs a model's tokenizer on input content and returns the token count.

HTTP request

POST https://generativelanguage.googleapis.com/v1/{model=models/*}:countTokens

The URL uses gRPC Transcoding syntax.

Path parameters

Parameters
model

string

Required. The model's resource name. This serves as an ID for the Model to use.

This name should match a model name returned by the models.list method.

Format: models/{model}

Request body

The request body contains data with the following structure:

JSON representation
{
  "contents": [
    {
      object (Content)
    }
  ]
}
Fields
contents[]

object (Content)

Required. The input given to the model as a prompt.

Response body

A response from models.countTokens.

It returns the model's tokenCount for the prompt.

If successful, the response body contains data with the following structure:

JSON representation
{
  "totalTokens": integer
}
Fields
totalTokens

integer

The number of tokens that the model tokenizes the prompt into.

Always non-negative.

Authorization scopes

Requires one of the following OAuth scopes:

  • https://www.googleapis.com/auth/generative-language
  • https://www.googleapis.com/auth/generative-language.tuning
  • https://www.googleapis.com/auth/generative-language.tuning.readonly
  • https://www.googleapis.com/auth/generative-language.retriever
  • https://www.googleapis.com/auth/generative-language.retriever.readonly

For more information, see the Authentication Overview.