CertSafari
    Snowflake SnowPro Specialty: Gen AI (GES-C02)· Lessons

    Domain 1 · Lesson 2/15

    Cortex AI functions, tokens and Cortex Search

    Outline Gen AI capabilities in Snowflake.

    13 min read
    9% of exam
    6 sources
    Published 21 Oct 2026
    Docs as of 4 Oct 2026

    What you will be able to do

    • Write an AI_COMPLETE prompt that pairs a clear instruction with an explicit output format
    • Explain what a token is, how embedding context windows limit input, and what Cortex Search does with text that exceeds them
    • Describe how a Cortex Search service combines vector search, keyword search and reranking, and how its source query, target lag and attributes shape it
    • List the privileges needed to create, query and operate a Cortex Search service, and explain why consumers need no SELECT on base tables
    • Choose between SQL, Python and REST access to Cortex Search, and explain how a Cortex Knowledge Extension shares a search service

    Key concept

    Structured versus unstructured retrieval in Cortex — Snowflake's Gen AI capabilities divide the work by data shape. Cortex Analyst turns questions into SQL over structured data described by semantic views, and Cortex Search retrieves passages from unstructured text. Agents, chat interfaces and external clients are built by combining these two.

    1.Prompting Cortex AI functions for usable output

    Snowflake Cortex is the suite of AI features that use large language models to understand unstructured data and answer freeform questions. The simplest entry point is the AI functions, which call a model from SQL. With these functions the prompt is your whole interface to the model. Whatever you leave out of the prompt, the model has to guess.

    For an extraction task, a good prompt has two parts. The first is a direct instruction that names the task. In the AI_COMPLETE structured-outputs example, the prompt opens with Extract structured data from this customer interaction note: and then gives the note. The second is an explicit constraint on the shape of the answer. That example passes a response_format of type json with a schema that lists the fields it expects back, such as price, address and product. What comes back is text a pipeline can parse, not a paragraph somebody has to read.

    The common bad habits all fail for the same reason: they remove information the model needs. If you send raw column values with no instruction, the model has to infer what you want. If you repeat the same question several times, you spend more tokens but add no constraint. If you drop examples and format guidance to give the model "freedom", you get free-form prose, and the downstream SQL that expects fields breaks on the first row it can't parse.

    Checkpoint 1 of 5· Check yourself

    A nightly job feeds each support note to AI_COMPLETE and loads the answer into typed columns. Which prompt design is most likely to keep that load from breaking?

    Sources12

    2.Tokens, embeddings and context windows

    Models do not read characters or words. They read tokens, the smallest unit of text a large language model processes. As a rough guide, one token is about three quarters of an English word, or around four characters. Model cost is quoted in credits per million tokens, so token counts drive both spend and fit. COUNT_TOKENS tells you how many tokens a string uses for a given model.

    Counting the tokens a string uses for an embedding modelsql
    SELECT SNOWFLAKE.CORTEX.COUNT_TOKENS('snowflake-arctic-embed-m', '<input_text>') as token_count

    A vector embedding turns text into a fixed-length array of numbers, so that texts with similar meaning end up close together. Each embedding model accepts a fixed-size context window of input tokens. The models available to Cortex Search differ widely in window size, in output dimensions and in which languages they handle:

    Embedding models hosted for Cortex Search
    ModelOutput dimensionsContext window (tokens)Languages
    snowflake-arctic-embed-m-v1.5 (default)768512English-only
    snowflake-arctic-embed-l-v2.01024512Multilingual
    snowflake-arctic-embed-l-v2.0-8k10248192Multilingual
    voyage-multilingual-2102432,000Multilingual

    Going past the window does not cause an error. It truncates the text, at indexing time and at query time alike. Cortex Search cuts the string to the window size before it embeds it, so anything after the cut point plays no part in semantic matching. Keyword retrieval still uses the full text. In practice, a key sentence near the end of a long document can match on its exact words but never on its meaning.

    The fix is to chunk the text before it reaches the service, and a larger model is not the answer. Snowflake recommends chunks of no more than 512 tokens, about 385 English words, even though longer-context models exist. Smaller chunks make retrieval more precise, and in a RAG flow the LLM receives passages that are more relevant to the question. SPLIT_TEXT_RECURSIVE_CHARACTER is the built-in helper for splitting the text.

    Checkpoint 2 of 5· Fill the gap

    Complete the query that reports how many tokens a string uses for an embedding model.

    SELECT SNOWFLAKE.CORTEX. ? ('snowflake-arctic-embed-m', '<input_text>') as token_count

    Sources3

    3.Cortex Search: managed hybrid retrieval

    Cortex Search is a managed search engine for fuzzy, low-latency search over text stored in Snowflake. You do not manage embeddings, infrastructure, quality tuning or index refreshes yourself. It has three main uses: the retrieval engine for RAG chatbots, the backend for an enterprise search bar, and the retrieval layer for Cortex Agents that work over unstructured data.

    Every query goes through three stages:

    • vector search, which finds semantically similar documents
    • keyword search, which finds lexically similar documents
    • semantic reranking, which reorders the most relevant results

    Vector search catches paraphrases that keyword search misses. Keyword search catches exact product codes and names that embeddings blur. You can adjust scoring with numeric boosts, time decays and component weights, or turn reranking off.

    Cortex Search and Cortex Analyst solve different problems. Cortex Analyst turns natural-language business questions into SQL over structured data. If the corpus is support articles or call transcripts and the task is to find the right passage, the tool is Cortex Search. A home-built nightly job that recomputes similarity over a VECTOR column gives you back exactly the embedding, refresh and ranking work that the managed service exists to remove.

    A search service over support transcripts: one search column, one filterable attribute, a daily target lagsql
    CREATE OR REPLACE CORTEX SEARCH SERVICE transcript_search_service
      ON transcript_text
      ATTRIBUTES region
      WAREHOUSE = cortex_search_wh
      TARGET_LAG = '1 day'
      EMBEDDING_MODEL = 'snowflake-arctic-embed-l-v2.0'
      AS (
        SELECT
            transcript_text,
            region,
            agent_id
        FROM support_transcripts
    );

    Each clause in that statement does a separate job:

    • ON names the column that is searched.
    • ATTRIBUTES names columns you can filter on at query time. Here that means region.
    • The source query decides what the service indexes. Here region and agent_id are indexed so they can come back alongside each match.
    • TARGET_LAG sets how far the service may fall behind its base table. With '1 day', it checks for updates about once a day.
    • The warehouse materialises the source query, both at creation and on each refresh. Snowflake recommends a dedicated warehouse no larger than MEDIUM.

    The index is built during CREATE, so on large tables the statement itself can take hours.

    Refresh works the same way as it does for dynamic tables. For that reason, the source query must qualify for incremental refresh, a rule that exists to prevent runaway embedding costs. If you declare an optional PRIMARY KEY of TEXT columns, each refresh processes only the rows that changed and does not re-embed everything.

    Checkpoint 3 of 5· Match them up

    Match each part of a Cortex Search service definition to the job it does.

    Tap a term, then the definition that fits it.

    Sources34

    4.Access control for Cortex Search

    Creating a service and querying one need different privileges. The creator's role needs four things:

    • the SNOWFLAKE.CORTEX_USER or SNOWFLAKE.CORTEX_EMBED_USER database role, because building the index calls the embedding functions
    • CREATE CORTEX SEARCH SERVICE or OWNERSHIP on the target schema
    • SELECT on the source tables or views
    • USAGE on the warehouse that refreshes the service

    Change tracking must also be enabled on every underlying object, or the service cannot detect changes.

    Consumers need only USAGE on the service and on its database and schema. They do not need SELECT on the base tables, because a Cortex Search service runs searches with owner's rights, like other owner's-rights objects in Snowflake. This lets you expose search over a table without granting read access to the table itself. It also means a consumer's own privileges do not narrow the results. To suspend or resume a service with ALTER, a role needs OPERATE on it.

    Grants that let a consumer role query an existing servicesql
    GRANT USAGE ON DATABASE cortex_search_db TO ROLE customer_support;
    GRANT USAGE ON SCHEMA services TO ROLE customer_support;
    
    GRANT USAGE ON CORTEX SEARCH SERVICE transcript_search_service TO ROLE customer_support;

    Checkpoint 4 of 5· Check yourself

    The customer_support role must run searches against transcript_search_service but must not be able to read the support_transcripts table directly. Which set of privileges is sufficient?

    Sources3

    5.Ways to query Cortex Search, and sharing it as a CKE

    A service is queried through a JSON-style request with the same fields however you call it: query, the columns to return, an optional filter on attributes, and a limit. Only the client changes. Each response returns every column listed in columns.

    Ways to reach a Cortex Search service
    InterfaceTypical use
    SQL: SNOWFLAKE.CORTEX.SEARCH_PREVIEWChecking from a worksheet that a new service is populated and returns sensible results
    SQL: CORTEX_SEARCH_DATA_SCANInspecting the indexed rows, including the generated embeddings column
    Python API (snowflake.core)Querying from application code through a Snowpark session
    REST APIQuerying from any application over HTTP
    Python API query, filtered to one regionpython
    resp = transcript_search_service.search(
      query="internet issues",
      columns=["transcript_text", "region"],
      filter={"@eq": {"region": "North America"} },
      limit=1
    )
    print(resp.to_json())

    A Cortex Knowledge Extension (CKE) is a Cortex Search service that has been shared on the Snowflake Marketplace or through a private or organizational listing. The provider loads its text into a table, builds a search service over it, and publishes the service. The consumer then builds a chatbot or agent with Cortex AI Functions or the Cortex Agent API and points it at the CKE. When a user asks a question, it goes to the CKE for a semantic search. The LLM reasons over the passages that come back and answers with citations and attribution. This lets a consumer ground an application in licensed articles or market research it doesn't hold itself. On the provider side, CKEs support content protection, management, trials and monetization.

    Checkpoint 5 of 5· Put it in order

    Put the life of a Cortex Knowledge Extension in order, from the provider's first step to the consumer's application.

    1. 1.The consumer builds an application that sends prompts to the CKE for semantic search
    2. 2.The service is shared on the Snowflake Marketplace or through a private or organizational listing
    3. 3.The provider creates a Cortex Search service on that table
    4. 4.The provider uploads its text data into a table

    Sources35

    Sources

    (6)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “Extract structured data from this customer interaction note”
      ↩︎ Prompting Cortex AI functions for usable output
      “it ignores any text the model provides unrelated to the supplied schema”
      ↩︎ Checkpoint
    2. 2.
      “a suite of AI features that use large language models (LLMs) to understand unstructured data, answer freeform questions”
      ↩︎ Prompting Cortex AI functions for usable output
    3. 3.
      “one token is equivalent to about 3/4 of an English word, or around 4 characters”
      ↩︎ Tokens, embeddings and context windows
      “Each vector embedding model supports a fixed size context window for text inputs”
      ↩︎ Tokens, embeddings and context windows
      “without having to worry about embedding, infrastructure maintenance, search quality parameter tuning, or ongoing index refreshes”
      ↩︎ Cortex Search: managed hybrid retrieval
      “Semantic reranking for reranking the most relevant documents in the result set.”
      ↩︎ Cortex Search: managed hybrid retrieval
      “The column region will be available as a filter column when querying the transcript_text column.”
      ↩︎ Cortex Search: managed hybrid retrieval
      “Cortex Search Services have the same refresh properties as Dynamic Tables.”
      ↩︎ Cortex Search: managed hybrid retrieval
      “granting the SNOWFLAKE.CORTEX_USER database role or the SNOWFLAKE.CORTEX_EMBED_USER database role to the service creator role”
      ↩︎ Access control for Cortex Search
      “must have USAGE privileges on the service itself, as well as on the database and schema”
      ↩︎ Access control for Cortex Search
      “Change tracking must be enabled on all underlying objects used by a Cortex Search Service.”
      ↩︎ Access control for Cortex Search
      “Cortex Search Services return all columns specified in the columns field in your query.”
      ↩︎ Ways to query Cortex Search, and sharing it as a CKE
      “Cortex Search Services perform searches with owner’s rights”
      ↩︎ Exam trap 1
      “Cortex Search truncates the string to the size of the context window before embedding it into vector space for semantic search”
      ↩︎ Exam trap 2
      “splitting the text in your search column into chunks of no more than 512 tokens (about 385 English words)”
      ↩︎ Exam trap 3
      “However, Cortex Search uses the full body of text for keyword-based retrieval.”
      ↩︎ Prediction
      “The TARGET_LAG parameter dictates that the Cortex Search Service will check for updates to the base table support_transcripts approximately once per day.”
      ↩︎ Checkpoint
    4. 4.
      “business users can ask questions in natural language and receive direct answers without writing SQL”
      ↩︎ Cortex Search: managed hybrid retrieval
    5. 5.
      “Cortex Knowledge Extensions (CKEs) are Cortex Search Services that can be shared on the Snowflake Marketplace or via private listings or organizational listings.”
      ↩︎ Ways to query Cortex Search, and sharing it as a CKE
      “A Consumer builds an application leveraging Cortex AI, such as a chatbot, using Cortex AI Functions or the Cortex Agent API with the CKE.”
      ↩︎ Ways to query Cortex Search, and sharing it as a CKE
      “A Provider uploads their text data into a table in their account and creates a Cortex Search Service on the table.”
      ↩︎ Checkpoint

    Also cited

    Continue to part 2 of 2

    Cortex Analyst, Cortex Agents and external access

    Spotted a mistake, or was something unclear? Tell us.