What you will be able to do
- Grant the privileges a role needs to call Cortex AI functions
- Choose the task-specific function (AI_CLASSIFY, AI_SENTIMENT, AI_EXTRACT, AI_SUMMARIZE, AI_TRANSLATE, AI_FILTER) that fits a row-level text job
- Structure an AI_COMPLETE call with a model, model_parameters and a response_format
- Estimate and reduce the token cost of a batch AI function job
Key concept
LLMs as SQL functions — Snowflake Cortex exposes large language models as SQL functions that run once per row of a query. Because each call is billed by the tokens it processes, the prompt you write and the rows you select determine both what you get back and what you pay.
1.LLMs as SQL functions and who may call them
Snowflake Cortex lets you call large language models as ordinary SQL functions. You can classify, summarise, translate or generate text across a table without moving the data out of Snowflake. The models come from providers such as OpenAI, Anthropic, Meta and Mistral AI, and they run inside the Snowflake service perimeter. The same functions can also be called from Python.
Calling them takes two grants. The role needs the account-level USE AI FUNCTIONS privilege, plus one of the CORTEX_USER or AI_FUNCTIONS_USER database roles. ACCOUNTADMIN grants SNOWFLAKE.CORTEX_USER. Warehouse access does not cover Cortex. An analyst who can run queries on a warehouse but gets an insufficient-privileges error on an AI function is missing the database role, not warehouse rights. AI_EMBED also accepts the narrower SNOWFLAKE.CORTEX_EMBED_USER role.
The functions are built for throughput, not latency. Use them for batch jobs over large tables. For interactive use where each response must come back quickly, use the REST APIs instead: Complete for inference, Embed for embeddings and Agents for agentic applications.
The AI_* functions replace the older SNOWFLAKE.CORTEX functions. Exam questions and existing pipelines may still use the old names, so learn both:
| Legacy function | Replacement | What changes |
|---|---|---|
| SNOWFLAKE.CORTEX.COMPLETE | AI_COMPLETE | Drop-in for the basic (model, prompt) form; an options object splits into model_parameters, response_format and show_details |
| SNOWFLAKE.CORTEX.CLASSIFY_TEXT | AI_CLASSIFY | Result is an object whose labels field is an array: read :labels[0] |
| SNOWFLAKE.CORTEX.EXTRACT_ANSWER | AI_EXTRACT | Returns a JSON object; questions go in responseFormat |
| SNOWFLAKE.CORTEX.SENTIMENT | AI_SENTIMENT | Returns a categories array instead of a FLOAT score |
| SNOWFLAKE.CORTEX.SUMMARIZE / TRANSLATE | AI_SUMMARIZE / AI_TRANSLATE | Direct replacement |
| SNOWFLAKE.CORTEX.EMBED_TEXT_768 / EMBED_TEXT_1024 | AI_EMBED | The model sets the output dimension |
Checkpoint 1 of 5· Check yourself
An analyst can run ordinary queries on the team warehouse. When they call AI_CLASSIFY on a table, they get an insufficient-privileges error. What fixes it?
Warehouse access does not cover Cortex. To call an AI function, the role needs the account-level USE AI FUNCTIONS privilege and either the CORTEX_USER or the AI_FUNCTIONS_USER database role.
“your role needs the USE AI FUNCTIONS account-level privilege and one of the CORTEX_USER or AI_FUNCTIONS_USER database roles”Source: docs.snowflake.com
Checkpoint 2 of 5· Check yourself
A team is building a customer-facing chat assistant. Each user waits on screen for an LLM answer to their own question. Which Cortex option suits this?
The SQL AI functions are built for throughput on large batch jobs. When each response must come back quickly, Snowflake points you to the REST APIs, and Complete is the one for inference.
“For more interactive use cases where latency is important, use the REST API. These are available for simple inference (Complete API)”Source: docs.snowflake.com
2.Task-specific functions over a table
Task-specific functions are managed functions built for one routine job each. You don't write a prompt or pick a model. You pass a column and get a structured result back. Each of the four exam categories maps to its own function:
- Categorisation:
AI_CLASSIFYtakes the input and an array of at least two categories, and returns an object with alabelsarray. Setoutput_modeto'multi'for multi-label results. Atask_description, label descriptions and few-shotexamplescan all raise accuracy. Categories are case sensitive. Use mutually exclusive labels, and keep the list to twenty or fewer. - Sentiment analysis:
AI_SENTIMENTreturns a categories array with values such as positive, negative, neutral, mixed or unknown. Pass entities as a second argument for aspect-based sentiment. - Information extraction:
AI_EXTRACTtakes the questions to answer inresponseFormatand returns a JSON object. Setscoresto TRUE to add confidence scores. - Summarisation:
AI_SUMMARIZEsummarises one value.AI_SUMMARIZE_AGGandAI_AGGwork across many rows and aren't subject to context window limits.
SELECT AI_CLASSIFY(review, ['refund','shipping','other']):labels[0] AS topic
FROM tickets;SELECT AI_EXTRACT(
text => doc_text,
responseFormat => {'total': 'What is the invoice total?'},
scores => TRUE
) AS answer
FROM docs;Two more functions cover selection and language. AI_FILTER returns TRUE or FALSE, so a plain-language condition can sit directly in a WHERE or JOIN clause. Take keeping only tickets where the customer threatens to cancel: you need no labelled data and no model to train. Write the condition as a specific question, such as "In the following ticket, does the customer threaten to cancel?", and combine it with the column using CONCAT or PROMPT(). AI_TRANSLATE translates text between supported languages. It chains naturally ahead of other functions. If a review table mixes languages but a sentiment dashboard was built for English, run AI_TRANSLATE to English first, then AI_SENTIMENT on the result.
A batch call over millions of rows must not fail because of one bad record. By default, a row that can't be processed returns NULL and the rest of the query completes. To see why a row failed, pass return_error_details => TRUE. Each row then returns an object with a value and an error field, so you can find failures and rerun just those rows.
Checkpoint 3 of 5· Match them up
A support-analytics team has five jobs to run over its ticket and document tables. Match each Cortex task-specific function to the job it fits.
Tap a term, then the definition that fits it.
Each task-specific function does one routine job. AI_CLASSIFY assigns labels, AI_FILTER returns TRUE/FALSE for use in WHERE or JOIN, AI_EXTRACT answers questions given in responseFormat as JSON, and AI_SENTIMENT returns sentiment categories. AI_SUMMARIZE_AGG summarises across many rows, while AI_SUMMARIZE handles only one value.
“AI_SUMMARIZE_AGG: Aggregates a text column and returns a summary across multiple rows. This function isn’t subject to context window limitations.”Source: docs.snowflake.com
3.Prompt engineering with AI_COMPLETE
AI_COMPLETE is the general-purpose function. Use it for any generative task the task-specific functions don't cover. Its signature is AI_COMPLETE(<model>, <prompt> [, <model_parameters>, <response_format>, <show_details>]). Each argument controls a different part of the result.
Model choice trades capability against cost, because different models cost different amounts. Availability also changes over time: run SHOW CORTEX BASE MODELS to list the models your account can use and their lifecycle status. The prompt is usually assembled per row with CONCAT. Wrapping the row's text in tags, as in 'Critique this review in bullet points: <review>', content, '</review>', steers the kind of response you get back.
| Option | Effect | Default |
|---|---|---|
| temperature | 0 to 1; higher gives more diverse output, lower gives more deterministic and focused output | 0 |
| top_p | 0 to 1; restricts the set of possible tokens, an alternative to temperature | 0 |
| max_tokens | Maximum output tokens; small values truncate the response | 4096 |
| guardrails | TRUE filters unsafe responses through Cortex Guard | FALSE |
Structured output turns free text into something a table can store. response_format takes either a JSON schema, which defines the fields, their types and which ones are required, or a SQL type literal such as TYPE OBJECT(score FLOAT). It must be passed as its own argument. If you put it inside model_parameters, it is ignored and you get an unstructured string back. Only the single-string form accepts a schema; the prompt-object form, which carries images or several inputs, does not.
SELECT AI_COMPLETE(
model => '<model>',
prompt => CONCAT('Rate the sentiment of the following text from -1.0 (very negative) to 1.0 (very positive). Respond with only the score. Text: ', review),
model_parameters => {'temperature': 0},
response_format => TYPE OBJECT(score FLOAT)
):score::FLOAT AS score
FROM reviews;Roles organise a conversation. The legacy COMPLETE function takes an array of objects, each with a role and a content key. A system message sets background and response style, comes first, and appears at most once. user messages are prompts, and assistant messages are earlier model replies. The function keeps no state between calls, so every turn resends the whole history and the token count grows each round. With AI_COMPLETE, use the prompt-object form for multi-turn prompts. Set show_details => TRUE to get back a JSON object whose usage field reports prompt_tokens, completion_tokens and total_tokens.
Checkpoint 4 of 5· Check yourself
Over a ten-turn conversation, a chat feature calls AI_COMPLETE with the prompt-object form and resends the system, user and assistant messages on every turn. How does the token cost of each call change as the conversation goes on?
The function keeps no state between calls, so every turn has to resend the full history. Input tokens are billed along with output tokens, so each call costs more than the one before.
“For the COMPLETE function, which generates new text in the response, both input and output tokens are counted.”Source: docs.snowflake.com
4.Token-based cost and batch design
Cortex AI functions are billed by the tokens they process. A token is roughly four characters of text, though the exact ratio varies by model. Which tokens count depends on the function:
| Function | Billed tokens |
|---|---|
| AI_COMPLETE, AI_CLASSIFY, AI_FILTER, AI_AGG, AI_SUMMARIZE, AI_TRANSLATE | Input and output |
| AI_EMBED, AI_SIMILARITY | Input only |
| AI_EXTRACT | Input and output; responseFormat counts as input |
| Cortex Guard | Input tokens, based on the AI_COMPLETE output, billed on top of AI_COMPLETE |
| AI_COUNT_TOKENS | No token cost; only the compute to run it |
You are billed for more text than you wrote. Functions such as AI_CLASSIFY, AI_FILTER, AI_SENTIMENT and AI_TRANSLATE add their own prompt to your input. AI_CLASSIFY's labels, descriptions and examples are counted again for every record processed, not once per call. So ten carefully described categories over five million rows means five million copies of those descriptions.
Warehouse cost still applies while the query runs, on top of token charges. Keep the warehouse at MEDIUM or below for Cortex AI function calls. To cut the cost of a large nightly AI_COMPLETE job without changing the task, use these levers: a shorter fixed instruction, because it repeats on every row; a lower max_tokens, because output tokens are billed; a cheaper model that still does the job; and filtering down to only the rows that need processing. Before running at scale, AI_COUNT_TOKENS('ai_complete', '<model>', prompt) estimates the input size. Afterwards, the SNOWFLAKE.ACCOUNT_USAGE.CORTEX_AISQL_USAGE_HISTORY view shows tokens and credits by function, model and query, split into input and output.
Checkpoint 5 of 5· Check yourself
Before a nightly job of five million support tickets, an engineer adds a 25-word description to each of ten AI_CLASSIFY categories, plus three few-shot examples, to improve accuracy. What does this do to the token bill?
AI_CLASSIFY adds the labels, their descriptions and the examples to the input of every call, and it is called once per row. Over five million rows, that extra text is billed five million times, so accuracy gained this way costs more as the table grows.
“Each label, description, and example increases the number of input tokens for every AI_CLASSIFY call, which affects cost.”Source: docs.snowflake.com
Sources
(11)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“To call any of these functions, your role needs the USE AI FUNCTIONS account-level privilege and one of the CORTEX_USER or AI_FUNCTIONS_USER database roles.”
↩︎ LLMs as SQL functions and who may call them“Batch processing is typically better suited for AI Functions. For more interactive use cases where latency is important, use the REST API.”
↩︎ LLMs as SQL functions and who may call them“Returns True or False for a given text or image input, allowing you to filter results in SELECT, WHERE, or JOIN”
↩︎ Task-specific functions over a table“Given an input text, returns the token count based on the model or Cortex function specified.”
↩︎ Token-based cost and batch design“Snowflake Cortex features are provided as SQL functions and are also available in Python.”
↩︎ Key concept“your role needs the USE AI FUNCTIONS account-level privilege and one of the CORTEX_USER or AI_FUNCTIONS_USER database roles”
↩︎ Checkpoint“For more interactive use cases where latency is important, use the REST API. These are available for simple inference (Complete API)”
↩︎ Checkpoint“AI_SUMMARIZE_AGG: Aggregates a text column and returns a summary across multiple rows. This function isn’t subject to context window limitations.”
↩︎ Checkpoint - 2.
“the ACCOUNTADMIN role must grant the SNOWFLAKE.CORTEX_USER database role to the user who will call the function”
↩︎ LLMs as SQL functions and who may call them“For the COMPLETE function, which generates new text in the response, both input and output tokens are counted.”
↩︎ Checkpoint - 3.
“SNOWFLAKE.CORTEX_EMBED_USER database role”
↩︎ LLMs as SQL functions and who may call them - 4.
“exceeding twenty categories might reduce classification accuracy”
↩︎ Task-specific functions over a table“Use mutually exclusive categories.”
↩︎ Task-specific functions over a table“Each label, description, and example increases the number of input tokens for every AI_CLASSIFY call, which affects cost.”
↩︎ Checkpoint - 5.https://docs.snowflake.com/en/user-guide/snowflake-cortex/aisql-migrate-legacy-functionsOfficial docs
“AI_SENTIMENT returns an object with a categories array instead of a FLOAT score.”
↩︎ Task-specific functions over a table“AI_EXTRACT returns a JSON object instead of a string. Provide the questions to answer in the responseFormat argument”
↩︎ Task-specific functions over a table“By default, failed records return NULL. Set return_error_details to TRUE to return the error associated with each failed record.”
↩︎ Task-specific functions over a table“For multi-turn prompts or conversation history, use the prompt object form of AI_COMPLETE.”
↩︎ Prompt engineering with AI_COMPLETE - 6.
“Consider phrasing the input in the form of a question.”
↩︎ Task-specific functions over a table - 7.
“Run SHOW CORTEX BASE MODELS to see which models are currently available to your account and their lifecycle status.”
↩︎ Prompt engineering with AI_COMPLETE“To get a structured output as the response, use the response_format parameter with AI_COMPLETE (Single string).”
↩︎ Exam trap 3 - 8.
“you can use tagging in the prompt to control the kind of response generated”
↩︎ Prompt engineering with AI_COMPLETE“a lower temperature (such as 0.2) makes the output more deterministic and focused”
↩︎ Prompt engineering with AI_COMPLETE“Usage of Cortex Guard incurs compute charges based on the number of input tokens processed, in addition to the charges for the AI_COMPLETE function.”
↩︎ Token-based cost and batch design - 9.
“The supplied JSON schema defines the structure, data types, and constraints that the generated text must conform to, including required fields.”
↩︎ Prompt engineering with AI_COMPLETE - 10.
“Only one system prompt may be provided, and if it is present, it must be the first in the array.”
↩︎ Prompt engineering with AI_COMPLETE“COMPLETE does not retain any state from one call to the next.”
↩︎ Prompt engineering with AI_COMPLETE - 11.
“An industry convention for text is that a token is approximately equal to four characters, although this can vary by model”
↩︎ Token-based cost and batch design“As a result, the billed token count is higher than the number of tokens in the text you provide.”
↩︎ Token-based cost and batch design“AI_CLASSIFY labels, descriptions, and examples are counted as input tokens for each record processed, not just once for each AI_CLASSIFY call.”
↩︎ Exam trap 1“Snowflake recommends using a warehouse size no larger than MEDIUM when calling Snowflake Cortex AI Functions.”
↩︎ Exam trap 2“Using a larger warehouse than necessary does not increase performance, but can result in unnecessary costs.”
↩︎ Prediction