Snowflake Cortex AI Functions
Use Snowflake Cortex AI SQL functions for classification, sentiment, summarization, and completion while accounting for access, evaluation, privacy, and cost.
Apply Language Models from Governed SQL Workflows
Prerequisites: SQL, roles and grants, data classification, and a governed Snowflake account. Learning objective: select an AI function for a bounded task and add evaluation, privacy, access, and cost controls.
Snowflake Cortex AI Functions expose selected AI capabilities through SQL functions, allowing an analyst or data engineer to classify, summarize, translate, extract information from, or generate text as part of a Snowflake workflow. This can remove the need to export a table to a separate application for a straightforward text-enrichment task. The function call still has latency, cost, regional availability, permissions, and quality considerations; it should be treated as a production dependency rather than a magic expression.
Examples in this lesson use functions such as AI_CLASSIFY, AI_SENTIMENT, AI_SUMMARIZE, and AI_COMPLETE. Availability and exact input/output behavior can vary by function, account region, and product updates. Check current Snowflake documentation and your account's feature availability before selecting a function for production. Do not infer the result schema from a dashboard preview; inspect the returned value and validate it explicitly.
Good fit: enrich a governed support-ticket table with a coarse category or draft summary for an analyst to review. Not a shortcut: make an unreviewed model response the authority for a payment, medical decision, eligibility decision, or security control.
Start with a Bounded, Governed Dataset
Consider a support team that wants to triage customer messages. The source table contains a stable ticket ID, text, created timestamp, and an approved subset of customer context. First choose a small set of labels that match a human workflow, define which columns may be sent to the function, and sample representative records. Remove unnecessary personal data before invoking the model. A smaller, clearer prompt is easier to evaluate and usually cheaper to operate.
CREATE OR REPLACE TEMP TABLE TICKET_SAMPLE AS
SELECT
TICKET_ID,
CREATED_AT,
LEFT(MESSAGE_TEXT, 4000) AS MESSAGE_TEXT
FROM ANALYTICS.SUPPORT.TICKETS
WHERE CREATED_AT >= DATEADD(day, -7, CURRENT_TIMESTAMP())
AND MESSAGE_TEXT IS NOT NULL
QUALIFY ROW_NUMBER() OVER (ORDER BY CREATED_AT DESC) <= 500;The row limit is an evaluation boundary, not a production sampling recipe. For a real evaluation set, include different channels, languages, message lengths, rare categories, and known difficult examples. Ensure your data policy permits the selected fields to be processed by the feature in the target account and region.
| Function family | Useful for | Human review focus |
|---|---|---|
| AI_CLASSIFY | Assign one or more task labels to text. | Label definitions, ambiguity, and rare classes. |
| AI_SENTIMENT | Estimate sentiment for text. | Sarcasm, mixed sentiment, language, and confidence needs. |
| AI_SUMMARIZE | Create a shorter representation of a passage. | Omitted caveats, names, dates, and unsupported claims. |
| AI_COMPLETE | Generate a response from an instruction and input. | Grounding, format validation, and unsafe or invented content. |
Use Categories That Match the Work Queue
Classification is most useful when the options are mutually understandable to the people who will act on them. If a team cannot explain the difference between “billing” and “payment,” a model will not repair that taxonomy. Begin with a small label set, include short definitions where supported by the current function syntax, and evaluate against examples labeled by a human reviewer.
SELECT
TICKET_ID,
AI_CLASSIFY(
MESSAGE_TEXT,
['delivery', 'payment', 'account access', 'other']
) AS CLASSIFICATION_RESULT
FROM TICKET_SAMPLE
LIMIT 20;AI_CLASSIFY returns a serialized object whose labels field contains the classification labels. Inspect the complete result before selecting a field or casting it. Do not assume it includes a calibrated confidence score. If confidence thresholds are important, confirm the supported response format and define a fallback for uncertain or malformed outputs.
Separate Machine-Generated Signals from Facts
Sentiment is a rough signal, not a verified fact about a person's intent or state of mind. A short “fine” can mean satisfaction, frustration, or neutral acknowledgment depending on context. Use sentiment to help prioritize a human queue, not to make a sensitive decision about an individual. Summaries are also lossy: a compact version can omit qualifiers or details that change meaning.
SELECT
TICKET_ID,
AI_SENTIMENT(MESSAGE_TEXT) AS SENTIMENT_RESULT,
AI_SUMMARIZE(MESSAGE_TEXT) AS SHORT_SUMMARY
FROM TICKET_SAMPLE
WHERE LENGTH(MESSAGE_TEXT) > 80
LIMIT 25;AI_SENTIMENT returns an object with a categories field, including an overall sentiment record; when category arguments are supplied, the result also reports sentiment for those aspects. Validate the exact output types and function behavior for the account and version in use. Keep the original message available to authorized reviewers, render model-generated summaries as suggestions, and make it easy to open the full source text before a support agent acts. Never treat a summary as a replacement for the source record in an audit or dispute.
Generate Draft Text with Explicit Boundaries
Completion is broader than classification: it generates text from an instruction and input. For support triage, a bounded use is drafting an internal summary from a ticket, not sending a customer response automatically. Keep model selection configurable so deployment can choose a model supported by the account and region, and verify current arguments in official docs rather than hard-coding an obsolete model name.
-- The deployment runner sets the Snowflake session variable CORTEX_MODEL
-- from reviewed environment configuration and approved account options.
SELECT
TICKET_ID,
AI_COMPLETE(
$CORTEX_MODEL,
CONCAT(
'Write a neutral one-sentence internal summary. ',
'Do not invent facts. If details are unclear, say so. ',
'Ticket: ', MESSAGE_TEXT
)
) AS DRAFT_SUMMARY
FROM TICKET_SAMPLE
LIMIT 10;Treat model output as untrusted text. If downstream software expects JSON, parse it with a real parser, validate required fields and allowed values, and route failures to review. Avoid asking a model to perform authorization or SQL construction. Parameterize application SQL and grant tools only the privileges their code needs.
| Control | Implementation idea | Failure response |
|---|---|---|
| Grounding | Prompt only with approved source text and clear instructions. | Show “needs review” when output adds unsupported claims. |
| Format | Validate JSON or required fields after generation. | Reject invalid output; do not silently coerce it. |
| Access | Use a role with access only to approved source columns. | Stop the task on privilege or policy errors. |
| Human review | Keep a reviewer queue for consequential actions. | Do not auto-send or auto-decide on low-quality output. |
Measure the Workflow Before Scaling It
Create a held-out evaluation set labeled by people who understand the task. Measure category-level errors, false positives and false negatives, language or channel differences, and the percentage routed to human review. For summaries, review factual coverage and omissions. Track feature use, latency, failures, retries, and estimated spend using the currently supported account telemetry and usage views. Set a budget or row-volume limit before scheduling a large backfill.
Common Mistakes
- ✓Assuming generated output is a verified fact or calibrated confidence score; inspect and validate the actual return value.
- ✓Sending unnecessary identifiers or sensitive columns because the query runs inside Snowflake; minimize and approve input data.
- ✓Promoting a prompt or taxonomy change without a fixed human-labeled regression set.
- ✓Scaling a backfill without checking feature availability, model options, usage controls, and a bounded cost plan.
- ✓Minimize input columns and redact direct identifiers unless the task demonstrably requires them.
- ✓For Snowflake-hosted models, confirm the required SNOWFLAKE.CORTEX_USER database role and review feature-specific grants, availability, region, and data-processing requirements.
- ✓Separate model-generated output from source facts and retain provenance for each result.
- ✓Test prompt and taxonomy changes against a fixed regression set before promoting them.
- ✓Use bounded batches, retries with limits, and a human fallback for ambiguous or failed records.
- ✓Check current function limits, model options, and usage reporting in the official Snowflake docs before implementation.
Practice: Support Ticket Triage Pilot
Use a synthetic or explicitly approved dataset. Begin with a small batch and ask a human reviewer to label the same records independently.
- ✓Define four ticket labels and write a one-sentence description for each label.
- ✓Run a small classification query, inspect the full result type, and compare it with human labels.
- ✓Find examples where sentiment or a summary loses context; add them to a regression test set.
- ✓Draft an internal summary with AI_COMPLETE using a configured model value; validate and review every output.
- ✓Write a production checklist covering data minimization, access, regional availability, evaluation, cost, and human escalation.
🎯 Key Takeaways
- ✓Cortex AI Functions bring selected AI capabilities into Snowflake SQL workflows; they still need product and operations design.
- ✓Choose a bounded task and approved dataset before calling a model.
- ✓Inspect actual return values and validate them instead of assuming a schema or confidence score.
- ✓Treat sentiment, summaries, and generated text as fallible signals that need evaluation and, where appropriate, human review.
- ✓Availability, model choices, syntax, access, and usage controls can change; verify the current account documentation.
- ✓Privacy, provenance, least privilege, cost limits, and quality monitoring are part of the implementation.
HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
