Search⌘ K
AI Features

Evaluating Context Pipelines with Traces, RAGAS, and DeepEval

Learn how to record OpenInference traces for each pipeline step, define retrieval and agent evaluation criteria, and score context precision, recall, faithfulness, and tool trajectories. Compare results with a baseline, enforce CI quality gates, and use model-based judging with LiteLLM.

We'll cover the following...

Over this chapter, we added defenses to the assistant’s memory. Each one runs when the code reaches it. But nothing tells us when an ordinary code change makes the assistant’s answers worse. Suppose a refactor removes the freshness filter from chapter 4. The stale 2024 runbook, note-4, comes back into the search results, and the assistant starts suggesting a Redis restart again. Nobody would know until an engineer noticed during an incident.

In this lesson, we’ll close that gap in three steps. First, we’ll record each run of the pipeline as a trace: a saved record of every step, in the OpenInference format that Arize Phoenix reads. Then we’ll score the traces with metrics modeled on RAGAS and DeepEval. Finally, we’ll compare the scores with a stored baseline and fail the check when one drops, so it can block a change in continuous integration (CI).

Record each step as a span

A trace is made of spans. Each span records one step: its kind, its input, and its output. OpenInference defines span kinds such as RETRIEVER, TOOL, LLM, and CHAIN, and attribute names such as input.value, output.value, and retrieval.documents. Because the names are standard, tools like Phoenix can display any pipeline that uses them.

Here’s how the pieces of this lesson fit together.

Each run becomes a saved trace, the traces are scored, and the gate compares the averages with a baseline
Each run becomes a saved trace, the traces are scored, and the gate compares the averages with a baseline

First, let’s write a small span recorder that uses those names.

tracing.py · Python 3.14.0
import json
from contextlib import contextmanager
SPANS = [] # attribute names follow OpenInference, the format Arize Phoenix reads
@contextmanager
def span(trace_id, kind, name, **attributes):
record = {"trace_id": trace_id, "name": name, "openinference.span.kind": kind, **attributes}
yield record # the code inside the block adds its output
SPANS.append(record)
def save(path, spans):
with open(path, "w") as f:
f.writelines(json.dumps(s, default=list) + "\n" for s in spans)
def load(path):
return [json.loads(line) for line in open(path)]
def summary(s): # one short line per span, for printing
docs = s.get("retrieval.documents")
detail = [d["document.id"] for d in docs] if docs else str(s.get("output.value"))[:70]
return f"{s['openinference.span.kind']:<9} {s['name']:<18} {detail}"

In the code above:

  • Line 4: We keep every finished span in SPANS.

  • Lines 7–11: We define span(). It creates a record with the trace ID, the span’s kind and name, and any attributes. The code inside the with block adds its output, and the record is saved when the block ends.

  • Lines 14–20: We define save() and load(), which write spans to a JSON Lines file and read them back.

  • Lines 23–26: We define summary(), which prints one short line per span.

Next, we need a pipeline to trace. It retrieves notes with the chapter 4 search, applies the freshness and authority filters, and drafts an answer. The draft step stands in for the model with a fixed rule, so the whole evaluation runs the same way every time. We’ll use a real model as the judge at the end of the lesson.

eval_pipeline.py · Python 3.14.0
import re
from authority_filter import authority_filter
from freshness_filter import freshness_filter
from hybrid_search import hybrid_search
def retrieve(indexes, question, use_freshness=True, top_k=4):
found = [doc_id for doc_id, _ in hybrid_search(indexes, question, top_k=10)["documents"]]
if use_freshness:
found, _ = freshness_filter(found)
return authority_filter(found)[0][:top_k]
def draft_answer(doc_ids, texts): # a fixed-rule stand-in for the model, so the eval runs offline
claims = []
for doc_id in doc_ids[:2]:
sentence = max((s for s in re.split(r"(?<=\.) ", texts[doc_id]) if "[" not in s), key=len)
claims.append(f"{sentence} [{doc_id}]")
return " ".join(claims)

Here’s ...