Counting tokens

For a detailed guide on counting tokens using the Gemini API, including how images, audio and video are counted, see the Token counting guide and accompanying Cookbook recipe.

Method: models.countTokens

Runs a model's tokenizer on input Content and returns the token count. Refer to the tokens guide to learn more about tokens.

Endpoint

post https://generativelanguage.googleapis.com/v1beta/{model=models/*}:countTokens

Path 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} It takes the form models/{model}.

Request body

The request body contains data with the following structure:

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. This includes the prompt as well as other model steering information like system instructions, and/or function declarations for function calling. Models/Contents and generateContentRequests are mutually exclusive. You can either send