token_counts
Creates, updates, deletes, gets or lists a token_counts resource.
Overview
| Name | token_counts |
| Type | Resource |
| Id | gemini.models.token_counts |
Fields
The following fields are returned by SELECT queries:
- count_tokens
| Name | Datatype | Description |
|---|---|---|
cachedContentTokenCount | integer (int32) | Number of tokens in the cached part of the prompt (the cached content). |
cacheTokensDetails | array | Output only. List of modalities that were processed in the cached content. |
promptTokensDetails | array | Output only. List of modalities that were processed in the request input. |
totalTokens | integer (int32) | The number of tokens that the `Model` tokenizes the `prompt` into. Always non-negative. |
Methods
The following methods are available for this resource:
| Name | Accessible by | Required Params | Optional Params | Description |
|---|---|---|---|---|
count_tokens | select | modelsId | generateContentRequest, contents | Runs a model's tokenizer on input Content and returns the token count. Refer to the tokens guide to learn more about tokens. |
Parameters
Parameters can be passed in the WHERE clause of a query. Check the Methods section to see which parameters are required or optional for each operation.
| Name | Datatype | Description |
|---|---|---|
modelsId | string | |
contents | array | Optional. The input given to the model as a prompt. This field is ignored when generate_content_request is set. |
generateContentRequest | object | Request to generate a completion from the model. |
SELECT examples
- count_tokens
Runs a model's tokenizer on input Content and returns the token count. Refer to the tokens guide to learn more about tokens.
- Query Shape
- Query Example
SELECT
cacheTokensDetails,
cachedContentTokenCount,
promptTokensDetails,
totalTokens
FROM gemini.models.token_counts
WHERE modelsId = '{{ modelsId }}' -- required
AND generateContentRequest = '{{ generateContentRequest }}'
AND contents = '{{ contents }}'
;
SELECT
totalTokens
FROM gemini.models.token_counts
WHERE modelsId = 'gemini-2.5-flash'
AND contents = '[
{
"role": "user",
"parts": [{"text": "how does stackql work?"}]
}
]'
;