# TruLens

> Trace and evaluate LLM apps and agents with feedback functions on OpenTelemetry

- Category: Evaluation & Production Ops / Evaluation & Red-Teaming
- Language: Python
- Licence: MIT
- Repository: https://github.com/truera/trulens
- Website: https://www.trulens.org
- Canonical page: https://ai-tldr.dev/tools/trulens/

## In short

TruLens is an open-source Python library for evaluating and tracking LLM applications and agents. It sits in the Evaluation & Red-Teaming category of the Evaluation & Production Ops tools and is open-source, written in Python and licensed MIT. To get started: install the core package.

## Overview

TruLens is an open-source Python library for evaluating and tracking LLM applications and agents. Instead of manually checking outputs by hand, you attach feedback functions that score each run, so you can measure how a prompt, model, or retriever change affects quality and iterate from there.

Its instrumentation is built on OpenTelemetry, so every function call, LLM generation, retrieval, and tool invocation is captured as a structured span. Those traces are interoperable with existing observability backends like Jaeger, Grafana Tempo, Datadog, or any OTLP-compatible system.

It fits the evaluation and testing category as an eval framework for teams building RAG pipelines and agentic systems. It ships agent-specific evaluators, batch and inline evaluation modes, MCP tracing, and connectors for providers such as OpenAI, LiteLLM, Google, Bedrock, Snowflake Cortex, and HuggingFace.

## What it does

- OpenTelemetry-based tracing: each retrieval, generation, and tool call is captured as a structured OTEL span you can export to OTLP backends
- Feedback functions to systematically score app runs instead of manually vibe-checking outputs
- Seven agent evaluators including LogicalConsistency, PlanAdherence, ToolSelection, and ToolCalling
- Inline, on existing data, or offline batch evaluation via the Run API
- MCP span type to instrument Model Context Protocol tool calls (name, arguments, output, latency)
- Provider packages for OpenAI/Azure, LiteLLM, Google Gemini, AWS Bedrock, Snowflake Cortex, HuggingFace, and LangChain

## Getting started

Install the core package from PyPI, add a provider for feedback evaluation, then instrument your app with OpenTelemetry spans.

1. Install the core package

   Install TruLens core from PyPI.

```bash
pip install trulens-core
```

2. Add a provider and framework integration

   Install a provider for feedback evaluation and, if needed, an app framework integration.

```bash
pip install trulens trulens-providers-openai   # OpenAI / Azure OpenAI
pip install trulens trulens-apps-langchain     # LangChain / LangGraph
```

3. Instrument a retrieval span

   Decorate your app methods with the OpenTelemetry instrument decorator so each call is captured as a typed span.

```python
from trulens.core.otel.instrument import instrument
from trulens.otel.semconv.trace import SpanAttributes

class MyRAG:
    @instrument(
        span_type=SpanAttributes.SpanType.RETRIEVAL,
        attributes={
            SpanAttributes.RETRIEVAL.QUERY_TEXT: "query",
            SpanAttributes.RETRIEVAL.RETRIEVED_CONTEXTS: "return",
        },
    )
    def retrieve(self, query: str) -> list:
        ...
```

4. Target attributes with the Selector API

   Define a metric and point it at the span attributes you want to evaluate.

```python
from trulens.core import Metric, Selector

f_context_relevance = Metric(
    name="Context Relevance",
    implementation=provider.context_relevance,
    selectors={
        "input": Selector.select_record_input(),
        "context": Selector.select_context(),
    },
)
```

## When to use it

- Measure RAG quality with feedback functions for context relevance and groundedness while iterating on prompts, models, and retrievers
- Debug agent behavior with evaluators for plan adherence, tool selection, and tool calling
- Export LLM traces as OpenTelemetry spans to Jaeger, Grafana Tempo, Datadog, or another OTLP backend
- Run offline batch evaluations over a pre-collected dataset using the Run API
