- Categories:
String & binary functions (AI Functions)
AI_ COUNT_ TOKENS¶
Returns an estimate of the number of input tokens in a prompt for the specified large language model or task-specific function. For functions that can take additional inputs that affect the input token count, such as model name or categories/labels, those inputs can also be specified.
Syntax¶
The syntax can vary based on the function used. In general, you pass the function name, model name if applicable, input text, and any additional options that affect token count.
AI_COUNT_TOKENS uses specific syntax variations for some functions. For example:
See Examples for function specific usage patterns.
Arguments¶
Required:
function_nameString containing the name of the function you want to base the token count on, such as
'ai_complete'or'ai_sentiment'. The function’s name must begin with “ai_” and use only lowercase letters.A complete list of supported functions is available in the Regional availability table.
input_textorinput_text_1,input_text_2Input text to count the tokens in.
Optional:
model_nameString containing the name of the model you want to base the token content on. Required if the function specified by
function_namerequires you to choose the model to use, such as AI_COMPLETE or AI_EMBED.A list of available LLM models is available in the Regional availability table. Snowflake intends to add support for additional models over time.
categoriesAn array of VARIANT values that specify one or more categories or labels to use, for functions that require this data. Categories are included in the input token count.
optionsA VARIANT that specifies additional options that affect how the function processes the input. For functions that take two text inputs, such as AI_SIMILARITY, options are used to specify the model.
return_error_detailsA BOOLEAN flag that indicates whether to return error details in case of error. When set to TRUE, the function returns an OBJECT that contains the value and the error message, one of which is NULL depending on whether the function succeeded or failed. See Error behavior for details.
Returns¶
An INTEGER value that is the estimated number of input tokens for the input text and other parameter values you provide. This value does not include output (generated) tokens.
Error behavior¶
By default, if AI_COUNT_TOKENS can’t process the input, the function returns NULL. If the query processes multiple rows, rows with errors return NULL and don’t prevent the query from completing.
The return value on error depends on the return_error_details
argument. The following table shows the return value based on the return_error_details argument:
return_error_detailsReturn value Description FALSE Not passed NULL TRUE OBJECT with valueanderrorfieldsvalue: An INTEGER value that is the token count, or NULL if an error occurred.error: A VARCHAR value that contains the error message if an error occurred, or NULL if the function succeeded.
For more information about error handling for AI functions, see Snowflake Cortex AI Function: Multirow error handling improvements.
Usage notes¶
- Although function names are usually written in all uppercase, use only lowercase letters in function and model names.
- AI_COUNT_TOKENS does not work with LLM functions in the SNOWFLAKE.CORTEX namespace or with fine-tuned models. You must specify a function name that begins with “ai_”.
- AI_COUNT_TOKENS accepts only text, not image, audio, or video inputs.
- AI_COUNT_TOKENS estimates input tokens only. It does not estimate output (generated) tokens, which also contribute to billing. To see actual billed input and output tokens for a query, use the CORTEX_FUNCTIONS_QUERY_USAGE_HISTORY view.
- The accuracy of the token count depends on the model. See Token count accuracy for details.
- When you use AI_COMPLETE with the
response_formatargument (structured output) on Anthropic Claude models, additional request content is generated to support structured output. This content is included in the billed input tokens but is not reflected in the AI_COUNT_TOKENS estimate. As a result, the billed input token count for these requests can be materially higher than the estimated token count. - AI_COUNT_TOKENS only incurs compute costs and does not bill based on token count.
- AI_COUNT_TOKENS is available in all regions, even for models not available in a given region.
Token count accuracy¶
The accuracy of the token count depends on the model. For most models, AI_COUNT_TOKENS returns an exact count. The following table shows the expected accuracy by model:
| Model | Token count accuracy |
|---|---|
| Anthropic Claude | Estimate; relative error under 3% |
| Google Gemini | Estimate; relative error under 3% |
| OpenAI | Near-exact |
| All other models | Exact |
Note
If you use structured outputs (the response_format argument, supported on Anthropic Claude models), the difference
between the estimate and the billed input token count can be a lot larger than the values shown here. For details, see
the structured output note in Usage notes.
Examples¶
AI_ COMPLETE example¶
The following SQL statement counts the number of tokens in a prompt for AI_COMPLETE and the llama3.3-70b model:
Response:
AI_ COMPLETE with structured output example¶
The following SQL statement estimates the input tokens for an AI_COMPLETE call that uses the response_format
argument (structured output) on an Anthropic Claude model:
Response:
Note
Structured output on Claude models generates additional request content that is billed as input tokens but is not reflected in this estimate, so the billed input token count can be materially higher than the estimated token count. See Usage notes for details.
AI_ EMBED example¶
The following SQL statement counts the number of tokens in text being embedded using the AI_EMBED function and the nv-embed-qa-4 model:
Response:
AI_ CLASSIFY examples¶
This example calculates the total number of input tokens required for text classification with given input and labels:
Response:
The following example adds per-label descriptions and an overall task description to the previous example:
Response:
The following example builds upon the previous two examples by adding label examples:
Response:
AI_ SENTIMENT examples¶
The following SQL statement counts the number of tokens in text being analyzed for sentiment using the AI_SENTIMENT function:
Response:
The following example adds labels to the previous example:
Response:
AI_ SIMILARITY examples¶
The following SQL statement counts the number of tokens in an AI_SIMILARITY call that uses the default model.
Response:
The following SQL statement counts the number of tokens in an AI_SIMILARITY that uses the e5-base-v2 model:
Response:
AI_ TRANSLATE example¶
The following SQL statement counts the number of tokens used by AI_TRANSLATE when translating text from English to German.
Response:
AI_ REDACT examples¶
The following SQL statement counts the number of input tokens for a basic AI_REDACT request:
Response:
The following example includes a categories argument to estimate tokens when redacting only names and email addresses:
Response:
Note
AI_COUNT_TOKENS is the updated version of COUNT_TOKENS. For the latest functionality, use AI_COUNT_TOKENS.
Legal notices¶
Refer to Snowflake AI and ML.