Enable Guardrails Metrics
Use this minimal setup to enable metrics from IORails with the OpenTelemetry SDK and console output. LLMRails does not support OpenTelemetry metrics. Use this to verify metric emission locally before wiring up a production exporter.
Experimental Feature
Metrics currently require the opt-in IORails engine.
Enable it either by constructing Guardrails(config, use_iorails=True) (the form used in this quickstart) or by setting NEMO_GUARDRAILS_IORAILS_ENGINE=1, which aliases the top-level LLMRails import to Guardrails.
IORails is an early-release feature, and metric names can change as the OpenTelemetry GenAI semantic conventions evolve.
-
Install the NeMo Guardrails library and the OpenTelemetry SDK.
The
[tracing]extra installsopentelemetry-api, which is the only OpenTelemetry dependency the library itself takes. -
Save the following to
metrics_example.py.The script issues a single request and exports metrics once per second to both stdout and
metrics.json. Theprovider.shutdown()call flushes the metrics to disk. Long-running services typically do not need this call. -
Run the script.
-
Post-process the metrics JSON file.
metrics.jsoncontains one JSON document per export interval, concatenated. The following command reads every interval into an array and returns the final object, which is the cumulative state after the request completed.Example output (exact output token counts can vary):
Each object is one aggregation of the
gen_ai.client.token.usagehistogram. The fields are:type: Value of the requiredgen_ai.token.typelabel. Values areinputoroutput.count: Number of observations recorded for this token type. Each LLM call records oneinputobservation and oneoutputobservation, givingcount: 1per type.sum: Total tokens across those observations.
The host application is responsible for configuring a MeterProvider.
If you construct LLMRails(config) with metrics.enabled: true but no MeterProvider is set, the OpenTelemetry API returns a no-op meter and silently discards every metric emission.
The library does not log a warning.
Always set the MeterProvider before constructing LLMRails.
Next Steps
- For production exporters (OTLP, Prometheus), refer to OpenTelemetry Metrics Integration.
- For the full list of emitted metrics, refer to Metric Reference.