- Categories:
String & binary functions (AI Functions)
COUNT_TOKENS (SNOWFLAKE.CORTEX)¶
Note
AI_COUNT_TOKENS is the latest version of this function. Use AI_COUNT_TOKENS for the latest functionality. You can continue to use COUNT_TOKENS (SNOWFLAKE.CORTEX).
Returns the number of tokens in a prompt for the large language model or the task-specific function specified in the argument. This function does not support fine-tuned models.
Syntax¶
SNOWFLAKE.CORTEX.COUNT_TOKENS( <model_name> , <input_text> )
Arguments¶
Required:
model_nameName of the model you want to base the token count on. Specify one of the following values:
deepseek-r1e5-base-v2e5-large-v2gemma-7bjamba-1.5-largejamba-1.5-minijamba-instructllama2-70b-chatllama3-70bllama3-8bllama3.1-405bllama3.1-70bllama3.1-8bllama3.2-1bllama3.2-3bllama3.3-70bllama4-maverickllama4-scoutmistral-7bmistral-largemistral-large2mixtral-8x7bnv-embed-qa-4reka-corereka-flashsnowflake-arctic-embed-l-v2.0snowflake-arctic-embed-m-v1.5snowflake-arctic-embed-msnowflake-arcticsnowflake-llama-3.1-405bsnowflake-llama-3.3-70bvoyage-multilingual-2
input_textInput text to count the tokens in.
Returns¶
Returns an INT , INTEGER , BIGINT , SMALLINT , TINYINT , BYTEINT type that is the number of tokens in the input text based on the model or function specified.
Usage notes¶
If a function name is specified, the token count is based on the model used by the function.
Use lowercase letters in function names.
Note
COUNT_TOKENS does not account for the managed system prompt that is automatically added to the beginning of the input text when using a Cortex AISQL functions. As a result, the value returned by COUNT_TOKENS is lower than the actual number of tokens processed by these functions.
Examples¶
The following example returns the token count for the specified prompt using the llama3.1-70b model:
SELECT SNOWFLAKE.CORTEX.COUNT_TOKENS( 'llama3.1-70b', 'what is a large language model?' );
+---+
| 6 |
+---+
Legal notices¶
Refer to Snowflake AI and ML.