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)
    }
  ],
  "generateContentRequest": {
    object (GenerateContentRequest)
  }
}
Fields
contents[]

object (Content)

Optional. The input given to the model as a prompt. This field is ignored when generateContentRequest is set.

generateContentRequest

object (GenerateContentRequest)

Optional. The overall input given to the model. models.countTokens will count prompt, function calling, etc.

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.

GenerateContentRequest

Request to generate a completion from the model.

JSON representation
{
  "model": string,
  "contents": [
    {
      object (Content)
    }
  ],
  "safetySettings": [
    {
      object (SafetySetting)
    }
  ],
  "generationConfig": {
    object (GenerationConfig)
  }
}
Fields
model

string

Required. The name of the Model to use for generating the completion.

Format: name=models/{model}.

contents[]

object (Content)

Required. The content of the current conversation with the model.

For single-turn queries, this is a single instance. For multi-turn queries, this is a repeated field that contains conversation history + latest request.

safetySettings[]

object (SafetySetting)

Optional. A list of unique SafetySetting instances for blocking unsafe content.

This will be enforced on the GenerateContentRequest.contents and GenerateContentResponse.candidates. There should not be more than one setting for each SafetyCategory type. The API will block any contents and responses that fail to meet the thresholds set by these settings. This list overrides the default settings for each SafetyCategory specified in the safetySettings. If there is no SafetySetting for a given SafetyCategory provided in the list, the API will use the default safety setting for that category. Harm categories HARM_CATEGORY_HATE_SPEECH, HARM_CATEGORY_SEXUALLY_EXPLICIT, HARM_CATEGORY_DANGEROUS_CONTENT, HARM_CATEGORY_HARASSMENT are supported.

generationConfig

object (GenerationConfig)

Optional. Configuration options for model generation and outputs.