snowflake.snowpark.DataFrameAIFunctions.count_tokens¶
- DataFrameAIFunctions.count_tokens(function_name: str, prompt: Union[snowflake.snowpark.column.Column, str], *, model: Optional[str] = None, output_column: Optional[str] = None, options: Optional[Dict[str, Any]] = None, return_error_details: Optional[bool] = None) snowflake.snowpark.DataFrame[source]¶
Count the number of tokens in text for a specified AI function.
This method returns an estimate of the number of tokens that would be consumed by a call to the given AI function. This is useful for estimating costs and ensuring inputs fit within model token limits.
- Parameters:
function_name – The AI function whose tokenizer should be used for counting (e.g.
'ai_complete','ai_classify','ai_embed','ai_sentiment'). Must begin with'ai_'and use only lowercase letters.prompt – The column (Column object or column name as string) containing the text to count tokens for.
model –
The model name, required for functions that accept a model parameter (e.g.
ai_complete,ai_embed). Supported models include:deepseek-r1,e5-base-v2,e5-large-v2gemma-7b,jamba-1.5-large,jamba-1.5-mini,jamba-instructllama2-70b-chat,llama3-70b,llama3-8bllama3.1-405b,llama3.1-70b,llama3.1-8bllama3.2-1b,llama3.2-3b,llama3.3-70bllama4-maverick,llama4-scoutmistral-7b,mistral-large,mistral-large2,mixtral-8x7bnv-embed-qa-4,reka-core,reka-flashsnowflake-arctic-embed-l-v2.0,snowflake-arctic-embed-m-v1.5snowflake-arctic-embed-m,snowflake-arcticsnowflake-llama-3.1-405b,snowflake-llama-3.3-70bvoyage-multilingual-2
output_column – The name of the output column to be appended. If not provided, a column named
COUNT_TOKENS_OUTPUTis appended.options – Optional dict specifying additional processing parameters for the underlying
AI_COUNT_TOKENScall.return_error_details – When
True, returns an OBJECT withvalueanderrorfields instead of returning NULL on failure.
- Returns:
A new DataFrame with an appended output column containing the token count as an integer. When
return_error_details=True, the column contains an OBJECT withvalueanderrorfields instead.
Examples:
Note
The token count does not account for any managed system prompt that may be automatically added when using other Cortex AI functions. The actual token usage may be higher when using those functions.