Blog/AI & ML/The AI Functions Optimization Playbook: Higher Quality at 89% Lower Cost
Nov 18, 2026/8 min readAI & ML

The AI Functions Optimization Playbook: Higher Quality at 89% Lower Cost

Reducing the cost of an AI-powered application should not mean compromising the quality a business depends on. But with multiple models and prompts to choose from, how can teams tell which configuration will meet their quality requirements at a lower cost?

Consider a messaging application, such as a dating platform or a customer service app. The application can use Cortex AI Functions to summarize conversations and identify recurring themes, helping teams improve the service or deliver more relevant recommendations to users. As adoption grows and the number of messages increases from thousands to tens of millions, the team needs to keep those insights useful while managing the cost of generating them. While a smaller model or a more efficient prompt might reduce cost, teams need to evaluate whether those changes will still deliver the quality their application requires.

To help AI data engineers scale workloads more cost effectively without sacrificing the quality their business depends on, we built a native evaluation and optimization harness for Cortex AI Functions. With AI_FUNCTION_EVALUATION, teams can measure output quality for their use case. AI_FUNCTION_OPTIMIZATION reduces manual tuning, helping teams identify configurations that lower inference costs while meeting quality requirements or that improve quality when initial results fall short. 

Connect AI capabilities to the customer experience

Imagine a friend-connection, customer service or shopping app that uses conversations to make more relevant recommendations. With Cortex AI Functions, teams can transform multimodal data into summaries, recurring themes, fraud-risk flags and users’ stated preferences that guide those recommendations. For example, in the conversation below, a friend-connection platform would want to flag this as a scam risk:

source

Figure 1. Example conversation on a friend-connection platform that Cortex AI Functions would flag as a scam risk.

 

To build applications that deliver the right insights, teams need a repeatable way to evaluate AI outputs against business expectations and compare prompts and models without manually testing every change.

This helps teams answer two business questions: 

  • Are our AI functions producing outputs of high enough quality to support recommendations that deliver value to users? 

  • Can we deliver that quality more cost effectively?

The workflow below connects four stages — Build, Evaluate, Optimize and Deploy — which take teams from selecting AI functions, models and prompts to validating quality, improving cost efficiency and putting the selected improvements into production.

build-update

Figure 2. The AI function lifecycle: Build, Evaluate, Optimize and Deploy.

 

Below, we follow this lifecycle for a connection application that recommends a next action for various user conversations.

Build: Turn conversations into useful recommendations

Many customers use AI_COMPLETE to turn unstructured data into insights, from summarizing conversations to identifying potential risks and recommending the next best action. 

Customers can now also define custom AI functions (generally available) to make this logic easily reusable across their organization. Teams can package their instructions and model choice into a function that applications call through SQL, with access managed through Snowflake’s existing controls. That means teams can define custom AI functions centrally rather than re-create them in every application. 

Our first function, RECOMMEND_NEXT_ACTION, uses AI_COMPLETE to recommend an action for each conversation:

CREATE OR REPLACE AI FUNCTION RECOMMEND_NEXT_ACTION(CONVERSATION VARCHAR)
RETURNS VARCHAR
COMMENT = 'Baseline: recommend next action for a dating-app chat'
AS
$$
SELECT AI_COMPLETE(
    'claude-opus-5',
    'You review a chat between two users on a dating platform and recommend the next action for the platform. Return exactly one label in lowercase: suggest_date (strong mutual interest, a meetup is being proposed), send_icebreaker (low-effort chat that is stalling), recommend_similar_profiles (polite incompatibility or dealbreaker), safety_review (scams, money requests, harassment, explicit pressure, underage, threats, phishing links), keep_chatting (healthy rapport, no plan yet). Return only the label. Chat: ' || CONVERSATION
)::VARCHAR
$$;

Next, customers need to understand whether the outputs of their custom AI function meet their quality and cost expectations.

Evaluate: Measure the quality behind the recommendations

AI_FUNCTION_EVALUATION (now generally available) makes quality measurable directly in Snowflake. It tests AI functions against labeled examples and reports quality, token usage and cost, helping teams assess whether their implementation meets quality requirements and estimate the cost per conversation before scaling to larger workloads. For example, the FRIENDS_PLATFORM_CHAT_EVAL table below pairs each user conversation with an expected_action, providing a reference for measuring how well the function identifies the appropriate action.

chat

Figure 3. The FRIENDS_PLATFORM_CHATS_EVAL table pairs each user conversation with an expected_action label, which is the reference for scoring the function's output.

 

Teams can choose a scoring method that fits their AI transformation task. Below, we use llm_judge to compare the function’s recommendations with the expected actions. For fixed labels such as these, exact_match is another option. AI_FUNCTION_EVALUATION supports various evaluation metrics for different AI transformation tasks. See the documentation.

CREATE OR REPLACE EXPERIMENT DATING_CHAT_ACTION_EVAL
  TYPE = 'AI_FUNCTION_EVALUATION'
  FROM SPECIFICATION $$
    query_text: MY_DATABASE.PUBLIC.RECOMMEND_NEXT_ACTION(conversation)
    metrics:
      - name: llm_judge
    dataset:
      name: MY_DATABASE.PUBLIC.FRIENDS_PLATFORM_CHATS_EVAL
      version: v1
      ground_truth: expected_action
    evaluation:
      num_eval_runs: 1
  $$;

EXECUTE EXPERIMENT DATING_CHAT_ACTION_EVAL;

The results establish a baseline for comparing alternatives. They also help teams distinguish changes that sound promising from changes that produce better outputs.

Optimize: Identify the highest-quality option at the lowest cost for a use case

Once we understand the baseline, we can explore whether another model or better instructions can deliver the quality we need more economically.

AI_FUNCTION_OPTIMIZATION (now generally available) automates that search. The product brings a selection of agentic optimization algorithms, such as Snowflake’s CoCoEvolve and GEPA, into your production workload within a few clicks. These agentic optimization algorithms use evaluation feedback to generate and test alternative implementations, reducing the need for manual prompt tuning.

Using the models, evaluation data, scoring criteria and budget provided, it explores prompt-model-and-function-implementation combinations and records each candidate’s quality and cost. These results help teams identify configurations that lower inference costs while meeting quality requirements or that improve quality when the baseline falls short.

For our connection app, we include both the premium model and lower-cost alternatives. Instead of manually tuning each option, we let AI_FUNCTION_OPTIMIZATION explore what each can achieve with better instructions. Teams can configure an optimization through a guided workflow in the Snowsight interface, with CoCo or directly in SQL. For the SQL approach, the example below configures an optimization experiment for our connection app’s RECOMMEND_NEXT_ACTION function:

CREATE OR REPLACE EXPERIMENT DATING_CHAT_ACTION_OPT
  TYPE = 'AI_FUNCTION_OPTIMIZATION'
  FROM SPECIFICATION $$
    function:
      function_name: MY_DATABASE.PUBLIC.RECOMMEND_NEXT_ACTION
    dataset:
      name: MY_DATABASE.PUBLIC.FRIENDS_PLATFORM_CHATS_EVAL
      version: v1
      column_mapping:
        argument_mapping:
          CONVERSATION: conversation
        ground_truth: expected_action
    metrics:
      - name: llm_judge
    optimization:
      budget: medium
      models:
        - claude-opus-5
        - claude-sonnet-5
        - claude-haiku-4-5
      reflection_model: gpt-5-5
      algorithm: CoCoEvolve
  $$;

The results show how optimization can improve quality while reducing the cost of running an application at scale. In this example, an optimized Haiku candidate achieves a validation score of 0.98, compared with 0.95 for the Opus baseline, at 89% lower inference cost. Other candidates score higher, but this option improves on the baseline while substantially reducing cost.

cost

Figure 4. AI_FUNCTION_OPTIMIZATION results compare candidate configurations by quality score and inference cost. An optimized Haiku candidate scores 0.98 against 0.95 for the Opus baseline, at 89% lower cost.

 

Note: This material contains hypothetical scenarios created for informational purposes only. The scenario and data are synthetic. Scenarios are not intended to predict or guarantee any specific results or cost savings. The potential accuracy of these scenarios is contingent upon numerous factors and Snowflake makes no guarantee that you will achieve any stated results.

Deploy: Put the best AI function to work

Once we’ve selected and validated an implementation, we can register it as a custom AI function directly from the experiment (seen in the figure below using Snowsight). This brings the optimized prompt and model configuration into a reusable function, so teams can put the improvement to work without re-creating the logic.

runs

Figure 5. Registering the selected candidate as a custom AI function directly from the experiment in Snowsight.

 

For our connection app, using the optimized function is a simple SQL call just like any Cortex AI Function. We apply it to production conversations to recommend the next action:

 

SELECT
    chat_id,
    user_id,
    conversation,
    RECOMMEND_NEXT_ACTION(conversation) AS predicted_action
FROM FRIENDS_PLATFORM_CHATS

The application continues to receive the same action labels but now uses the implementation we selected for its quality and cost. For our example, that means putting the lower-cost model and optimized prompt to work in the live pipeline, turning an experiment result into a more economical way to serve users.

Conclusion

Custom AI Functions make capabilities reusable. Evaluation makes quality measurable. Optimization helps teams identify more economical ways to deliver it. Together, these building blocks give teams a repeatable path from an initial implementation to a better-informed production decision.

Importantly, improving an AI workflow does not have to mean moving to a more expensive model. It can mean getting more from the models available and knowing when additional capability is worth paying for.

Get started

  • Follow the AI function optimization quickstart to build an AI function, evaluate it against labeled examples, compare optimized candidates and deploy your selected implementation. Then apply the same workflow to your own use case, starting with one function and a representative set of labeled examples.

  • Already have an AI function to evaluate? Ask Snowflake CoCo to help you get started. Enter /cortex-ai-function-studio in CoCo and select Evaluate to set up an evaluation using your function and labeled data. You can also use a guided UI flow directly from the Snowflake UI “AI > AI Functions > Evaluation | Optimization.”

  • For a deeper look at the technology behind optimization, read about how CoCoEvolve works.

 

Disclaimer: This material contains hypothetical scenarios created for informational purposes only. The scenario and data are synthetic. Scenarios are not intended to predict or guarantee any specific results or cost savings. The potential accuracy of these scenarios is contingent upon numerous factors and Snowflake makes no guarantee that you will achieve any stated results.

Share this post

Subscribe to our blog newsletter

Get the best, coolest and latest delivered to your inbox each week

Where Data Does More