AI_FUNCTION_OPTIMIZATION

functiondeferredAI & ML

docs.snowflake.com ↗

String & binary functions

AI_FUNCTION_OPTIMIZATION searches for improved implementations of an AI function using a labeled dataset and a scoring metric. You specify the AI function to optimize, the dataset used to evaluate candidate outputs, the metric to optimize, and the models available to the optimizer. Snowflake generates candidate implementations by modifying the function’s prompt and model configuration, evaluates each candidate against the dataset, and records its quality and cost. The optimization produces candidate implementations, including candidates on a quality & cost frontier. The original AI function is not modified. To use an optimized implementation, create a new AI function from the selected optimization run. AI_FUNCTION_OPTIMIZATION uses the same evaluation model as AI function evaluation: candidate implementations are repeatedly evaluated against the specified dataset and metric.

Syntax

Optimization runs in two statements: create the experiment with an optimization specification, then execute it.

CREATE [ OR REPLACE ] EXPERIMENT <experiment_name>
  TYPE = 'AI_FUNCTION_OPTIMIZATION'
  FROM SPECIFICATION $$
    <optimization_specification>
  $$;

EXECUTE EXPERIMENT <experiment_name>;

The optimization specification is a YAML document with this shape

function:
  function_name: "<db.schema.fn(ARG_TYPES)>"   # one of function_name /
  # function_body: "<sql_expression>"          #   function_body
metrics:
  - name: <metric_name>
    # judge_model: <model>    # llm_judge only
    # custom_udf: <udf>       # custom only (required)
dataset:
  name: <dataset_name>
  version: <dataset_version>
  column_mapping:                      # optional
    argument_mapping:
      <function_arg>: <dataset_column>
    ground_truth: <label_column>
optimization:
  models:
    - <candidate_model>          # one or more
  reflection_model: <model>
  algorithm: <gepa|cocoevolve>                    # optional
  strategy: <quality_first|balanced|cost_first>   # optional
  budget: <auto|ultra-light|light|medium|heavy>   # optional

Parameters

NameRequiredDescription
functionrequiredThe AI function to optimize. Supply exactly one of: function_name — a fully-qualified name with its argument-type signature, for example "my_db.my_schema.redact(VARCHAR)". The named function must be created with CREATE AI FUNCTION — a plain SQL UDF is rejected. function_body — an inline SQL expression to optimize directly (the same form you would put in an AI function body).
metricsrequiredExactly one scoring metric — a single mapping or a one-element list. Same metric set as evaluation: Metric Requires exact_match — fuzzy_match — contains_match — redaction_match — llm_judge optional judge_model custom custom_udf: <udf_name> (required)ExpandShow lessSee more See Evaluate an AI function for what each metric scores.
datasetrequiredThe labeled data used to score candidates. Must be a versioned Snowflake dataset (SNOWFLAKE.ML.DATASET); plain tables/views are not accepted. name — fully-qualified dataset name. Required. version — dataset version (for example v1). Required.
optimizationrequiredHow to search. Requires at least: models — a list of one or more candidate models to try. Each model is optimized in parallel; the best implementations across all models form the reported frontier. reflection_model — the model used to generate candidate improvements. Use the most capable supported model available, because this model drives the optimization process. For example, use a Claude Opus model or a latest-generation GPT model.
dataset.column_mappingrequiredMaps the function’s inputs and label to dataset columns. Needed when the function argument names differ from the dataset column names. argument_mapping — a mapping of <function_argument>: <dataset_column> (keys are the AI function’s parameter names, or positional markers $1, $2, …; values are dataset column names). ground_truth — the label column candidates are scored against.
optimization.algorithmrequiredThe evolutionary optimization algorithm (default gepa). Values are case-insensitive. Algorithm Proposes candidates by gepa Showing the reflection model one candidate’s failures through AI_COMPLETE and asking for an improved version. cocoevolve Running an agent that applies meta-reflection across the search history, and self-tests drafts before evaluation.ExpandShow lessSee more With cocoevolve, reflection_model must be a model supported by Cortex Agents; otherwise CREATE EXPERIMENT fails. For details, see Select an optimization algorithm.
optimization.strategyrequiredThe optimization objective (default quality_first): Strategy Optimizes for quality_first Maximize the metric score (default). balanced Trade quality against cost. cost_first Minimize cost while holding quality.ExpandShow lessSee more
optimization.budgetrequiredControls how much search effort Snowflake uses to optimize the function. The budget is applied independently to each candidate model. For example, if you specify three candidate models with light, each model receives its own light optimization budget. Higher budgets generally generate and evaluate more candidate implementations, which can improve optimization results but also increase runtime and credit consumption. Use: ultra-light for a very small search. For optimizers that use iterative search, this corresponds to approximately 4–6 proposal iterations per candidate model. light for most optimization runs. This corresponds to approximately 10–15 proposal iterations per candidate model. medium for a broader search. This corresponds to approximately 18–27 proposal iterations per candidate model. heavy for the broadest search. This corresponds to approximately 27–40 proposal iterations per candidate model. auto to let Snowflake choose the budget. Currently, auto resolves to light.

Returns

CREATE EXPERIMENT and EXECUTE EXPERIMENT return status rows. EXECUTE EXPERIMENT runs asynchronously on serverless compute and returns immediately. The experiment produces a tree of runs: - A single SEED run — the evaluation of the input function as-is (the baseline). - One or more ITER_<N> runs — candidate implementations the optimizer proposed and evaluated. Accepted, rejected, and frontier candidates are all recorded (distinguished by run metadata, not by name). Read the runs and their metrics: Key run metrics and parameters: A healthy result: SEED completes with a baseline val_score, and one or more ITER_<N> runs reach a higher score with is_frontier = 1.

Usage notes

Access control requirements

Privilege / role | Object | Notes CREATE EXPERIMENT | Schema | To create the experiment. Database role SNOWFLAKE.CORTEX_USER (or SNOWFLAKE.AI_FUNCTIONS_USER) | — | Required to EXECUTE EXPERIMENT. USAGE | Function + dataset database/schema | To read the function and labeled data.

Limitations

- Target must be an AI function or an inline body. function_name must reference a CREATE AI FUNCTION object; a plain UDF is rejected. - Exactly one AI_COMPLETE call. Optimization tunes a single AI_COMPLETE call: the function (or function_body) must call AI_COMPLETE exactly once, and every optimized candidate contains exactly one AI_COMPLETE call. Functions with zero or multiple AI_COMPLETE calls are not supported for optimization. - Dataset objects only. The dataset must be a versioned SNOWFLAKE.ML.DATASET object. - Dataset size limit. The dataset can contain at most 1,000 rows (about 50–200 recommended). - One metric per optimization. - Model allowlist. Candidate and reflection models must be authorized/priced for optimization in your account and served in your region. A rejected model fails the experiment. - mode / optimize_mode / validation_fraction are not supported and are rejected by spec validation. - Execution time limit. An experiment run is limited to 20 hours; a longer run is terminated. Reduce runtime with a smaller dataset, fewer candidate models, or a lighter budget.

Legal

The data classification of inputs and outputs are as set forth in the following table. Input data classification | Output data classification | Designation Usage Data | Customer Data | Generally available functions are Covered AI Features. Preview functions are Preview AI Features. [1] [1] Represents the defined term used in the AI Terms and Acceptable Use Policy. For additional information, refer to Snowflake AI and ML.

Known gaps

Parts not fully supported, and recorded divergences from Snowflake.