Evaluate applications with TruLens¶
Use this topic when you run batch evaluations on a custom AI application (agent, RAG pipeline, AI workflow, and similar) instrumented with TruLens. Evaluations invoke your app against a dataset, store traces, and compute LLM-as-a-judge metrics (such as context relevance, groundedness, and correctness).
Before you start, instrument and register the application as described in Trace and monitor applications with TruLens.
Note
For Cortex Agent evaluations (Snowsight Evaluations tab, GPA metrics, EXECUTE_AI_EVALUATION), see Cortex Agent evaluations.
Evaluation workflow¶
- Build and instrument the app with TruLens.
- Register the app in Snowflake (
TruAppor a framework wrapper). - Create a run with a dataset and metric list.
- Invoke the run to generate outputs and traces.
- Compute metrics and review results in Snowsight (or query with SQL).
Map span attributes for metrics¶
To compute metrics during a run, assign function parameters to span attributes with @instrument(). Required attributes per metric are listed in Evaluation metrics.
Supported attributes by span type:
RECORD_ROOT:INPUT,OUTPUT,GROUND_TRUTH_OUTPUTRETRIEVAL:QUERY_TEXT,RETRIEVED_CONTEXTSGENERATION: None
In this example, query is the input parameter and return is the return value mapped to RETRIEVAL.QUERY_TEXT and RETRIEVAL.RETRIEVED_CONTEXTS for context relevance.
For framework auto-instrumentation (TruChain, TruGraph, TruLlama), see Auto-instrument framework applications on Trace and monitor applications with TruLens.
Create a run¶
Create a run with RunConfig and tru_app.add_run():
run_name: Unique name under thisTruAppdescription(optional): Run descriptionlabel(optional): Groups comparable runssource_type:DATAFRAMEorTABLEdataset_name: Dataframe label, or Snowflake table name (qualified if needed)dataset_spec: Maps span attributes to column names; see Dataset and attributesllm_judge_name(optional): Cortex model for judging; defaultllama3.1-70b
Retrieve, describe, invoke, and manage runs:
run.start() is blocking until invocation and ingestion complete or time out.
Live tracing¶
For online tracing (production monitoring), create a live run with the @trace_with_run decorator on the function that calls your app (commonly in a web frontend or API handler). When you invoke that function, @instrument() on app methods creates OpenTelemetry spans inside the TruLens recording context that @trace_with_run enables.
Trace and retrieve live runs:
Compute metrics¶
AI Observability metrics can be computed server-side (by metric name) or client-side (with the TruLens Metric API and custom metrics you define).
To compute server-side metrics after invocation status is INVOCATION_COMPLETED or INVOCATION_PARTIALLY_COMPLETED:
To compute custom client-side metrics, instantiate a Metric object and include it in the same list. Client-side metrics can be any TruLens feedback function or any Python function.
Simple Python function:
TruLens feedback functions can be customized for your domain with custom rubrics, guiding examples, or a different score scale. The following example uses a Cortex provider feedback function (provider is a trulens.providers.cortex.Cortex instance):
run.compute_metrics() is asynchronous. You can call it multiple times with different metric lists; a metric can’t be recomputed for the same run.
Check status, cancel, delete, or list runs:
Run statuses are listed in Runs.
View evaluation results in Snowsight¶
- Sign in to Snowsight.
- In the navigation menu, select AI & ML » Evaluations.
- Select the application (External Agent).
- Select a run to view aggregated and per-record scores.
- Select a record to view traces, latency, span detail, and LLM judge explanations.
To compare runs that share a dataset, select multiple runs and choose Compare.
Query evaluation data with SQL¶
Evaluation traces and scores for External Agent applications are stored in SNOWFLAKE.LOCAL.AI_OBSERVABILITY_EVENTS. Use agent_type EXTERNAL AGENT and the database, schema, and External Agent name.
Observability events¶
Filter on RECORD_ATTRIBUTES (for example run name) to scope results to one evaluation run. For column details, see GET_AI_OBSERVABILITY_EVENTS (SNOWFLAKE.LOCAL) and AI_OBSERVABILITY_EVENTS table.
Evaluation logs¶
For warnings and errors during a TruLens run:
Note
The fields record:"severity_text" and record_attributes:"snow.ai.observability.run.name" are guaranteed in AI Observability logs. Other fields may change.
When agent_type is EXTERNAL AGENT, USAGE on the External Agent is sufficient; MONITOR does not apply.
Note
Redaction and TruLens evaluations
The READ UNREDACTED AI OBSERVABILITY EVENTS TABLE privilege affects unredacted fields in Snowsight and observability UDTFs on the monitoring path. It does not change TruLens evaluation run execution or how scores appear in the Evaluations experience. See Account Privilege READ UNREDACTED AI OBSERVABILITY EVENTS TABLE and Trace and monitor applications with TruLens.
Access control¶
Roles need privileges described in Access control and storage and Runs, including USAGE on the External Agent, CREATE TASK, EXECUTE TASK, and SNOWFLAKE.CORTEX_USER.