AI_COUNT_TOKENS

functiondeferredAI & ML

docs.snowflake.com ↗

String & binary functions

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

supportedpartialnot supportednot scored

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( <function_name>, <input_text> [, <return_error_details> ] )
AI_COUNT_TOKENS( <function_name>, <model_name>, <input_text> [, <return_error_details> ] )
AI_COUNT_TOKENS( <function_name>, <input_text>, <options> [, <return_error_details> ] )
AI_COUNT_TOKENS( <function_name>, <model_name>, <input_text>, <options> [, <return_error_details> ] )

AI_COUNT_TOKENS uses specific syntax variations for some functions. For example

AI_COUNT_TOKENS( 'ai_similarity', <input_text_1>, <input_text_2>, <options> [, <return_error_details> ] )
AI_COUNT_TOKENS( 'ai_classify', <input_text>, <categories> [, <return_error_details> ] )
AI_COUNT_TOKENS( 'ai_translate', <input_text>, <source_language>, <target_language> [, <return_error_details> ] )

Parameters

NameRequiredDescription
function_namerequiredString 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. For supported functions, see Supported functions and models.
input_text or input_text_1, input_text_2requiredInput text to count the tokens in.
model_nameoptionalString 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. For supported model names, see Supported functions and models. Snowflake intends to add support for additional models over time.
categoriesoptionalAn 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.
optionsoptionalA 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_detailsoptionalA 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.

Usage notes

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_details | Return value | Description FALSE Not passed | NULL | TRUE | OBJECT with value and error fields | value: 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.

Supported functions and models

AI_COUNT_TOKENS supports these functions: - AI_COMPLETE, for a model in the table below - AI_EMBED, for a model in the table below - AI_SIMILARITY, for the default model or a model in the table below - AI_CLASSIFY, AI_SENTIMENT, AI_SUMMARIZE, AI_REDACT, and AI_TRANSLATE, which use the default model Function | Model AI_COMPLETE | claude-fable-5 AI_COMPLETE | claude-haiku-4-5 AI_COMPLETE | claude-opus-4-5 AI_COMPLETE | claude-opus-4-6 AI_COMPLETE | claude-opus-4-7 AI_COMPLETE | claude-opus-4-8 AI_COMPLETE | claude-opus-5 AI_COMPLETE | claude-opus-5-5 AI_COMPLETE | claude-sonnet-4-5 AI_COMPLETE | claude-sonnet-4-6 AI_COMPLETE | claude-sonnet-5 AI_COMPLETE | claude-sonnet-5-5 AI_COMPLETE | gemini-3.1-flash-lite AI_COMPLETE | gemini-3.1-pro AI_COMPLETE | gemini-3.5-flash AI_COMPLETE | gemini-3.7-flash AI_COMPLETE | gemini-3.8-flash AI_COMPLETE | gemma-4-26b-a4b AI_COMPLETE | gemma-4-31b AI_COMPLETE | gemma-4-e2b AI_COMPLETE | llama3-70b AI_COMPLETE | llama3-8b AI_COMPLETE | llama3.1-70b AI_COMPLETE | llama3.1-8b AI_COMPLETE | llama3.3-70b AI_COMPLETE | llama4-maverick AI_COMPLETE | mistral-7b AI_COMPLETE | mistral-large2 AI_COMPLETE | mistral-large3 AI_COMPLETE | mixtral-8x7b AI_COMPLETE | openai-gpt-4.1 AI_COMPLETE | openai-gpt-5 AI_COMPLETE | openai-gpt-5-mini AI_COMPLETE | openai-gpt-5-nano AI_COMPLETE | openai-gpt-5.2 AI_COMPLETE | openai-gpt-5.4 AI_COMPLETE | openai-gpt-5.4-mini AI_COMPLETE | openai-gpt-5.4-nano AI_COMPLETE | openai-gpt-5.6-luna AI_COMPLETE | openai-gpt-5.6-terra AI_COMPLETE | openai-gpt-6-luna AI_COMPLETE | openai-gpt-6.1-sol AI_COMPLETE | qwen3-32b AI_COMPLETE | qwen3-next-80b-a3b AI_COMPLETE | qwen3-vl-235b-a22b AI_EMBED | e5-base-v2 AI_EMBED | multilingual-e5-large AI_EMBED | nv-embed-qa-4 AI_EMBED | snowflake-arctic-embed-l-v2.0 AI_EMBED | snowflake-arctic-embed-l-v2.0-8k AI_EMBED | snowflake-arctic-embed-m AI_EMBED | snowflake-arctic-embed-m-v1.5 AI_EMBED | voyage-multilingual-2 AI_SIMILARITY | Default model AI_SIMILARITY | e5-base-v2 AI_SIMILARITY | multilingual-e5-large AI_SIMILARITY | nv-embed-qa-4 AI_SIMILARITY | snowflake-arctic-embed-l-v2.0 AI_SIMILARITY | snowflake-arctic-embed-m AI_SIMILARITY | snowflake-arctic-embed-m-v1.5 AI_SIMILARITY | voyage-multilingual-2

Legal notices

Refer to Snowflake AI and ML.

Known gaps

Parts not fully supported, and recorded divergences from Snowflake.