Skip to content
AI and LLM SQL recipe

Evaluate RAG retrieval quality by version

Compare relevant-document retrieval, grounded answers, retrieval latency, and evaluation cost across RAG pipeline versions.

Intermediaterag_evaluation_eventsReviewed 2026-10-02Tested with Apache DataFusion 45.2.0

Reviewed by the Telemetry product team on . We checked the SQL syntax, required event fields, sample results, and limits on using the query. Who reviews this page

Question answered

Did the new RAG pipeline improve retrieval and grounded-answer rates?

Compare retrieval relevance, answer grounding, latency, and evaluation cost for the same versioned test set. Check whether a quality gain also costs more or takes longer.

Event schema

Fields the query expects

FieldTypeWhy it exists
timestamp_utcTimestampEvaluation completion time in UTC.
pipeline_versionUtf8Versioned retriever, index, and prompt configuration.
test_case_idUtf8Stable evaluation-case identifier.
evaluation_run_idUtf8One comparison run shared across the candidate pipelines.
dataset_versionUtf8Frozen evaluation dataset version.
evaluator_versionUtf8Versioned grader, rubric, and threshold configuration.
relevant_document_retrievedBooleanReviewed retrieval relevance outcome.
answer_groundedBooleanReviewed grounded-answer outcome.
retrieval_latency_msInt64Retrieval-stage latency in milliseconds.
cost_usdFloat64Versioned estimated evaluation cost in USD.
environmentUtf8Evaluation or production environment.
DataFusion SQL

Copy the query

sql
WITH cases AS (
  SELECT
    pipeline_version,
    test_case_id,
    COUNT(*) AS records,
    MAX(CASE WHEN relevant_document_retrieved THEN 1
      WHEN relevant_document_retrieved = false THEN 0 END) AS retrieved,
    MAX(CASE WHEN answer_grounded THEN 1
      WHEN answer_grounded = false THEN 0 END) AS grounded,
    MAX(retrieval_latency_ms) AS retrieval_latency_ms,
    MAX(cost_usd) AS cost_usd
  FROM rag_evaluation_events
  WHERE timestamp_utc >= now() - INTERVAL '30 days'
    AND environment = 'evaluation'
    AND evaluation_run_id = 'release-check-001'
    AND dataset_version = 'faq-v1'
    AND evaluator_version = 'grounding-rubric-v1'
    AND pipeline_version IS NOT NULL AND pipeline_version <> ''
    AND test_case_id IS NOT NULL AND test_case_id <> ''
  GROUP BY pipeline_version, test_case_id
)
SELECT
  pipeline_version,
  SUM(CASE WHEN records = 1 THEN 1 ELSE 0 END) AS test_cases,
  SUM(CASE WHEN records > 1 THEN 1 ELSE 0 END) AS conflicting_test_cases,
  COUNT(CASE WHEN records = 1 THEN retrieved END) AS retrieval_reviewed_cases,
  COUNT(CASE WHEN records = 1 THEN grounded END) AS grounding_reviewed_cases,
  100.0 * SUM(CASE WHEN records = 1 THEN retrieved END)
    / NULLIF(COUNT(CASE WHEN records = 1 THEN retrieved END), 0) AS retrieval_relevance_pct,
  100.0 * SUM(CASE WHEN records = 1 THEN grounded END)
    / NULLIF(COUNT(CASE WHEN records = 1 THEN grounded END), 0) AS grounded_answer_pct,
  AVG(CASE WHEN records = 1 THEN retrieval_latency_ms END) AS avg_retrieval_latency_ms,
  SUM(CASE WHEN records = 1 THEN cost_usd END) AS evaluation_cost_usd
FROM cases
GROUP BY pipeline_version
ORDER BY grounded_answer_pct DESC NULLS LAST,
  retrieval_relevance_pct DESC NULLS LAST, pipeline_version;

This read-only query is planned and executed against an empty typed table with Apache DataFusion 45.2.0. We review the synthetic sample output separately. Check field types, thresholds, and counting rules against your own data. Read the testing methodology.

Query result

Grounded-answer rate by pipeline

Bars show grounded-answer rates only. The table reports retrieval relevance and each reviewed-case denominator separately.

pipeline_versiontest_casesconflicting_test_casesretrieval_reviewed_casesgrounding_reviewed_casesretrieval_relevance_pctgrounded_answer_pctavg_retrieval_latency_msevaluation_cost_usd
rag-v240447575950.05
rag-v1404450501200.04

Synthetic example output. Run the query against your own event schema and thresholds before using it for operational decisions.

Grounded-answer rate by pipeline: static chart of synthetic grounded_answer_pct values from the Evaluate RAG retrieval quality by version example result
Download this SVG chart of the sample results for an article, runbook, or design review. Please credit Telemetry.

Reproduce the example

Download the sample data

The JSON bundle includes the event schema with field types, reproducible input rows, exact SQL, expected output, review notes, and engine version. The CSV contains the displayed result.

How the SQL works

  1. 1Replace the run, dataset, and evaluator filters with one frozen comparison. Keep prompts and answers in the evaluation system.
  2. 2Only one terminal record per pipeline and case is counted. Conflicting cases are reported and excluded from rates, latency, and cost.
  3. 3Null reviews remain unknown. Each quality rate uses its own reviewed-case denominator; missing cases require a separate comparison with the planned test set.
  4. 4Pipeline version should identify the retriever, index snapshot, reranker, and answer prompt used for the test.
  5. 5Retrieval relevance and answer grounding remain separate because a good document can still produce an unsupported answer.
  6. 6Latency and cost keep the quality comparison connected to production constraints.

Edge cases to check

  • A run must use the same frozen case list and grader configuration across pipelines. Different grader versions measure different things.
  • The linked rag_retrieval_evaluated schema is a different contract. Add the fields listed above before using this query. Its duration_ms is end-to-end time, not retrieval_latency_ms; retrieval_score needs an explicit relevance rubric before becoming a Boolean review.
  • Investigate conflicting_test_cases before a release decision. Missing identifiers are excluded; no result row does not mean a zero failure rate.
  • Latency and cost cover only nonconflicting records with those values present, not necessarily the complete run.
  • Use the same versioned test cases across pipelines and report newly added or removed cases.
  • Human and model-based graders need documented rubrics, calibration, and disagreement review.
  • Do not store private source passages or raw answers in general telemetry by default.

Recommended dashboard

  • Bar chart: grounded_answer_pct by pipeline, with retrieval relevance and reviewed counts in the table
  • Trend: evaluation outcomes as the test set changes
  • Table: latency, cost, test count, and grader version

Alert guidance

Use an evaluation release gate with minimum test count and reviewed regression tolerance rather than a production paging alert.

Read alert setup

Set up the events this query needs

Related instrumentation and guides

Define the source data

Event schemas for this analysis

Continue the analysis

Run it on your events

Create a table, adapt the fields, and save the result

Start free, send structured events, and use the query result as a chart, shared dashboard widget, or alert input.

Get an API key