AI_COUNT_TOKENS
functiondeferredAI & MLReturns 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
| Name | Required | Description |
|---|---|---|
function_name | required | 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. For supported functions, see Supported functions and models. |
input_text or input_text_1, input_text_2 | required | Input text to count the tokens in. |
model_name | optional | 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. For supported model names, see Supported functions and models. Snowflake intends to add support for additional models over time. |
categories | optional | 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. |
options | optional | A 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_details | optional | A 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
- 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 function and model. See Token count accuracy for details.
- When you use AI_COMPLETE with the response_format argument (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.
- When you use AI_TRANSLATE, AI_COUNT_TOKENS estimates the tokens in the source text only. The billed token count also includes the generated translation, so the billed total is higher than the estimate.
- When you use AI_SUMMARIZE or AI_REDACT, AI_COUNT_TOKENS estimates the tokens in the source text only. The billed token count also includes the generated text, so the billed total is higher than the estimate.
- AI_COUNT_TOKENS only incurs compute costs and does not bill based on token count.
- You can call AI_COUNT_TOKENS in all regions, even when a supported model isn’t available in that region.
- The following table shows the measured accuracy for synthetic plain-text prompts with target lengths of about 100, 1,000, and 10,000 tokens.
- For each function and model, the percentage is the mean absolute percentage error between the AI_COUNT_TOKENS estimate and the verified input-token count. Exact means the estimate matched the verified count on every scored request.
- Function | Model | Plain text
- AI_COMPLETE | claude-fable-5 | Estimate; 6.3%
- AI_COMPLETE | claude-haiku-4-5 | Estimate; 6.6%
- AI_COMPLETE | claude-opus-4-5 | Estimate; 6.8%
- AI_COMPLETE | claude-opus-4-6 | Estimate; 6.8%
- AI_COMPLETE | claude-opus-4-7 | Estimate; 7.5%
- AI_COMPLETE | claude-opus-4-8 | Estimate; 7.1%
- AI_COMPLETE | claude-opus-5 | Estimate; 6.9%
- AI_COMPLETE | claude-sonnet-4-5 | Estimate; 8.1%
- AI_COMPLETE | claude-sonnet-4-6 | Estimate; 7.3%
- AI_COMPLETE | claude-sonnet-5 | Estimate; 6.8%
- AI_COMPLETE | gemini-3.1-flash-lite | Estimate; 14.2%
- AI_COMPLETE | gemini-3.1-pro | Estimate; 14.2%
- AI_COMPLETE | gemini-3.5-flash | Estimate; 14.2%
- AI_COMPLETE | gemma-4-26b-a4b | Estimate; 10.4%
- AI_COMPLETE | gemma-4-31b | Estimate; 10.4%
- AI_COMPLETE | gemma-4-e2b | Estimate; 9.5%
- AI_COMPLETE | llama3-70b | Exact
- AI_COMPLETE | llama3-8b | Exact
- AI_COMPLETE | llama3.1-70b | Exact
- AI_COMPLETE | llama3.1-8b | Exact
- AI_COMPLETE | llama3.3-70b | Exact
- AI_COMPLETE | llama4-maverick | Exact
- AI_COMPLETE | mistral-7b | Exact
- AI_COMPLETE | mistral-large2 | Exact
- AI_COMPLETE | mixtral-8x7b | Exact
- AI_COMPLETE | openai-gpt-4.1 | Estimate; 3.6%
- AI_COMPLETE | openai-gpt-5 | Estimate; 2.0%
- AI_COMPLETE | openai-gpt-5-mini | Estimate; 2.0%
- AI_COMPLETE | openai-gpt-5-nano | Estimate; 2.0%
- AI_COMPLETE | openai-gpt-5.4-mini | Estimate; 2.0%
- AI_COMPLETE | openai-gpt-5.4-nano | Estimate; 2.0%
- AI_COMPLETE | qwen3-32b | Estimate; 11.0%
- AI_COMPLETE | qwen3-next-80b-a3b | Estimate; 11.8%
- AI_COMPLETE | qwen3-vl-235b-a22b | Estimate; 11.8%
- AI_EMBED | e5-base-v2 | Exact
- AI_EMBED | multilingual-e5-large | Exact
- AI_EMBED | nv-embed-qa-4 | Exact
- AI_EMBED | snowflake-arctic-embed-l-v2.0 | Exact
- AI_EMBED | snowflake-arctic-embed-l-v2.0-8k | Exact
- AI_EMBED | snowflake-arctic-embed-m | Exact
- AI_EMBED | snowflake-arctic-embed-m-v1.5 | Exact
- AI_EMBED | voyage-multilingual-2 | Exact
- AI_SIMILARITY | Default model | Exact
- AI_SIMILARITY | e5-base-v2 | Exact
- AI_SIMILARITY | multilingual-e5-large | Exact
- AI_SIMILARITY | nv-embed-qa-4 | Exact
- AI_SIMILARITY | snowflake-arctic-embed-l-v2.0 | Exact
- AI_SIMILARITY | snowflake-arctic-embed-m | Exact
- AI_SIMILARITY | snowflake-arctic-embed-m-v1.5 | Exact
- AI_SIMILARITY | voyage-multilingual-2 | Exact
- AI_CLASSIFY | Default model | Exact
- AI_SENTIMENT | Default model | Exact
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
- The emulator does not run language, embedding, document or speech models, so Cortex AI functions have no model to answer with.