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
- Endpoint
- Path parameters
- Request body
- Response body
- Authorization scopes
- Example request
- GenerateContentRequest
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:
Optional. The input given to the model as a prompt. This field is ignored when generateContentRequest
is set.
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. Model
s/Content
s and generateContentRequest
s are mutually exclusive. You can either send Model
+ Content
s or a generateContentRequest
, but never both.