- Categories:
String & binary functions (AI Functions)
AI_COUNT_TOKENS¶
Note
AI_COUNT_TOKENS is the updated version of COMPLETE (SNOWFLAKE.CORTEX). For the latest functionality, use AI_COUNT_TOKENS.
Returns the number of tokens in a prompt for the specified large language model or task-specific function.
Syntax¶
AI_COUNT_TOKENS(<function_name>, <model_name> , <input_text> )
AI_COUNT_TOKENS(<function_name>, <input_text> )
AI_COUNT_TOKENS(<function_name>, <input_text>, <categories> )
Arguments¶
Required:
function_name
String 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_text
Input text to count the tokens in.
Optional:
model_name
String containing the name of the model you want to base the token content on. Required if the function specified by
function_name
requires 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. However, not all models are currently supported.
For AI_COMPLETE, the following models are not supported:
claude-4-opus
claude-4-sonnet
claude-3-7-sonnet
claude-3-5-sonnet
openai-gpt-4.1
openai-o4-mini
For AI_EMBED, the following models are not supported:
snowflake-arctic-embed-l-v2.0-8k
categories
An 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.
Returns¶
An INTEGER value that is the number of tokens of input text calculated using the given parameter values.
Usage notes¶
Use only lowercase letters in function and model names.
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_”.
COUNT_TOKENS accepts only text, not images or audio inputs.
COUNT_TOKENS does not use token-based billing and only incurs compute costs.
COUNT_TOKENS is available in all regions, even for models not available in a given region.
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:
SELECT AI_COUNT_TOKENS('ai_complete', 'llama3.3-70b', 'Summarize the insights from this
call transcript in 20 words: "I finally splurged on these after months of hesitation about
the price, and I\'m mostly impressed. The Nulu fabric really is as buttery-soft as everyone says,
and they\'re incredibly comfortable for yoga and lounging. The high-rise waistband stays put
and doesn\'t dig in, which is rare for me. However, I\'m already seeing some pilling after
just a few wears, and they definitely require gentle care. They\'re also quite delicate -
I snagged them slightly on my gym bag zipper. Great for low-impact activities, but I wouldn\'t
recommend for high-intensity workouts. Worth it for the comfort factor"');
Response:
158
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:
SELECT AI_COUNT_TOKENS('ai_embed', 'nv-embed-qa-4', '"I finally splurged on these after months
of hesitation about the price, and I\'m mostly impressed. The Nulu fabric really is as buttery-soft
as everyone says, and they\'re incredibly comfortable for yoga and lounging. The high-rise waistband
stays put and doesn\'t dig in, which is rare for me. However, I\'m already seeing some pilling after
just a few wears, and they definitely require gentle care. They\'re also quite delicate - I snagged
them slightly on my gym bag zipper. Great for low-impact activities, but I wouldn\'t recommend for
high-intensity workouts. Worth it for the comfort factor"');
Response:
142
AI_CLASSIFY examples¶
This example calculates the total number of input tokens required for text classification with given input and labels:
SELECT AI_COUNT_TOKENS('ai_classify',
'One day I will see the world and learn to cook my favorite dishes',
[
{'label': 'travel'},
{'label': 'cooking'},
{'label': 'reading'},
{'label': 'driving'}
]
);
Response:
187
The following example adds per-label descriptions and an overall task description to the previous example:
SELECT AI_COUNT_TOKENS('ai_classify',
'One day I will see the world and learn to cook my favorite dishes',
[
{'label': 'travel', 'description': 'content related to traveling'},
{'label': 'cooking','description': 'content related to food preparation'},
{'label': 'reading','description': 'content related to reading'},
{'label': 'driving','description': 'content related to driving a car'}
],
{
'task_description': 'Determine topics related to the given text'
};
Response:
254
The following example builds upon the previous two examples by adding label examples:
SELECT AI_COUNT_TOKENS('ai_classify',
'One day I will see the world and learn to cook my favorite dishes',
[
{'label': 'travel', 'description': 'content related to traveling'},
{'label': 'cooking','description': 'content related to food preparation'},
{'label': 'reading','description': 'content related to reading'},
{'label': 'driving','description': 'content related to driving a car'}
],
{
'task_description': 'Determine topics related to the given text',
'examples': [
{
'input': 'i love traveling with a good book',
'labels': ['travel', 'reading'],
'explanation': 'the text mentions traveling and a good book which relates to reading'
}
]
}
);
Response:
298
AI_SENTIMENT examples¶
The following SQL statement counts the number of tokens in text being analyzed for sentiment using the AI_SENTIMENT function:
SELECT AI_COUNT_TOKENS('ai_sentiment',
'This place makes the best truffle pizza in the world! Too bad I cannot afford it');
Response:
139
The following example adds labels to the previous example:
SELECT AI_COUNT_TOKENS('ai_sentiment',
'This place makes the best truffle pizza in the world! Too bad I cannot afford it',
[
{'label': 'positive'},
{'label': 'negative'},
{'label': 'neutral'}
]
);
Response:
148
Legal notices¶
Refer to Snowflake AI and ML.