Trace and monitor applications with TruLens¶
Use this topic when you build a custom AI application (for example, an agent, an AI workflow, or a RAG pipeline) that is not a native Cortex feature, and you want tracing and monitoring on Snowflake with the TruLens SDK. You can run the app on Snowflake compute (such as Snowpark Container Services or a hosted Python process) or on other infrastructure.
TruLens exports OpenTelemetry-style traces to your account. You debug executions in Snowsight and can query the same events with SQL. For batch evaluation runs and LLM-judge metrics (context relevance, groundedness, and others), see Evaluate applications with TruLens.
Note
If you built a Cortex Agent in Snowflake, use Monitor Cortex Agent requests instead. You don’t need TruLens for native agent monitoring.
TruLens is an open source observability SDK that Snowflake integrates with AI Observability. Snowflake registers each TruLens application as an External Agent object. That object stores metadata only (application name, version, run name). It does not store application code, prompts, traces, or scores. Traces and evaluation results are stored in AI_OBSERVABILITY_EVENTS.
TruLens is a good fit when you own the application end to end, including:
- Agent, RAG, or workflow apps on Snowflake compute or on another cloud or on-premises
- Custom tool chains and retrieval logic that are not native Cortex Agent deployments
- Batch LLM-as-a-judge evaluations you run from Python rather than Snowsight
Get started¶
- Install TruLens packages (version 2.1.2 or later):
trulens-core,trulens-connectors-snowflake,trulens-providers-cortex. See Required privileges in the reference topic. - Instrument and register your application using the steps in this topic.
- Run batch evaluations with Evaluate applications with TruLens.
- Take the quickstart tutorial or read the reference for datasets, metrics, runs, and privileges.
View External Agent traces and evaluation runs in Snowsight under AI & ML » Evaluations. For more information about TruLens itself, see the TruLens documentation.
Important
External Agent objects share a namespace with model objects in the same schema.
Tracing vs evaluation¶
Use the following table to choose the right TruLens guide:
| Task | Documentation |
|---|---|
| Capture spans while the app runs; debug latency and tool use in Snowsight | This topic |
| Run a dataset through the app, compute metrics, compare versions | Evaluate applications with TruLens |
Prerequisites¶
Complete the Required privileges steps before you instrument an application.
Instrument the app¶
After you create your application in Python, use the TruLens @instrument() decorator to capture function inputs, outputs, and latency.
Scenario 1: Trace a function¶
Add @instrument() on functions you want to trace:
Scenario 2: Trace a function with a specific span type¶
Span types improve trace readability in Snowsight:
RETRIEVAL: Retrieval or search functionsGENERATION: LLM inference callsRECORD_ROOT: Main entry point for the application
To map parameters to span attributes for evaluation metrics, see Evaluate applications with TruLens.
Scenario 3: Capture discrete function attributes in a trace¶
Span attributes help you surface particular values in a trace, such as a key-value pair passed to a custom agent.
The @instrument() decorator accepts lambda functions in the attributes parameter. Instead of static mappings, you can compute attributes from the function’s execution context. A lambda receives:
ret: The function’s return valueexception: Any exception raised during execution (Noneif the call succeeded)*args: Positional arguments passed to the function**kwargs: Keyword arguments (positional args are also available here by name)
Auto-instrument framework applications¶
TruLens provides wrappers for popular frameworks:
TruChain: LangChain and LCEL chainsTruGraph: LangGraph applicationsTruLlama: LlamaIndex query engines and retrievers
For framework examples and selectors, see the TruLens instrumentation documentation.
Register the app in Snowflake¶
Register the application so TruLens can write traces to AI_OBSERVABILITY_EVENTS and create an External Agent object for governance:
If you use TruChain, TruGraph, or TruLlama, registration is included when you wrap the application. See Auto-instrument framework applications.
Parameters:
app: Instance of your application classapp_name: Name stored in Snowflake for this applicationapp_version: Version label for experiments and comparisonsconnector:SnowflakeConnectorfor Snowpark session managementmain_method(optional): Entry point for tracing (for example,app.answer_query). Not required if the entry point usesRECORD_ROOTinstrumentation.
Invoke the application while the TruApp (or framework wrapper) recorder is active so traces are exported on each run.
View traces in Snowsight¶
- Sign in to Snowsight.
- In the navigation menu, select AI & ML » Evaluations.
- Select your application (External Agent).
- Open a run or record to inspect traces: inputs and outputs per span, latency, and intermediate steps.
Compare traces across application versions to debug regressions and tune prompts or tools. For aggregated evaluation scores on a dataset, see Evaluate applications with TruLens.
Observability data and SQL access¶
Traces and evaluation results for TruLens applications are stored in SNOWFLAKE.LOCAL.AI_OBSERVABILITY_EVENTS. Rows can’t be modified after ingestion.
The SNOWFLAKE.AI_OBSERVABILITY_READER application role grants read-only access to the table. The SNOWFLAKE.AI_OBSERVABILITY_ADMIN role can delete rows. For details, see AI_OBSERVABILITY_EVENTS table.
Note
Unredacted fields in Snowsight and UDTF results
The account privilege READ UNREDACTED AI OBSERVABILITY EVENTS TABLE controls whether roles see unredacted tool inputs, outputs, and conversation text when viewing traces in Snowsight or calling SNOWFLAKE.LOCAL observability UDTFs. Without the grant, metadata (tool names, latency, token usage, and similar fields) is still available. This does not change TruLens evaluation run execution or scores shown in the Evaluations experience. See Account Privilege READ UNREDACTED AI OBSERVABILITY EVENTS TABLE.
Query traces with SQL¶
Use GET_AI_OBSERVABILITY_EVENTS with agent_type EXTERNAL AGENT. Pass the database, schema, and External Agent object name. Results use the event table column layout.
When agent_type is EXTERNAL AGENT, USAGE on the External Agent is sufficient to call the function; MONITOR does not apply. OWNERSHIP is required to modify or drop the External Agent with SQL.
Filter with WHERE on RECORD, RECORD_ATTRIBUTES, time columns, or other fields to narrow results. For full arguments, see the function reference.
For evaluation-run logs and warnings, use GET_AI_OBSERVABILITY_LOGS. For evaluation result tables, see Evaluate applications with TruLens.
Access control¶
To view traces in Snowsight or query events for an External Agent, the role needs:
- USAGE (or OWNERSHIP) on the External Agent object
- SNOWFLAKE.CORTEX_USER database role (for
SNOWFLAKE.LOCALfunctions)
To register applications and run evaluations, see Required privileges.