Module 5 · Evaluation & production

Observability and continuous improvement loop

6 min read

A RAG put into production is not finished: the corpus changes, questions evolve, a model gets updated, an article gets deleted. Without observability, regressions are discovered through user complaints. This lesson, which closes the course, shows how to trace every query, what to review every week, and how to close the loop: from traces to the evaluation set, from the evaluation set to improvements.

Tracing every query

A trace must make it possible to answer, after the fact, the three diagnostic questions of lesson 1.2: did the answer exist, was it retrieved, was it used well?

@dataclass
class Trace:
    question: str
    retrieved: list[tuple[str, float]] = field(default_factory=list)
    answer: str = ""
    cited: list[str] = field(default_factory=list)
    timings_ms: dict[str, float] = field(default_factory=dict)
    feedback: int | None = None  # +1 / -1
    cache_hit: bool = False
FieldWhat it helps diagnose
retrieved (identifiers and scores)Did retrieval find the right passage? With what confidence?
answer and citedIs the answer based on sources? Which ones?
timings_msWhich step is slow?
feedbackIs the user satisfied?
cache_hitDoes the answer come from the cache (and therefore from another question)?

Durations are measured per step with a context manager:

@contextmanager
def step(self, name: str):
    start = time.perf_counter()
    try:
        yield
    finally:
        self.timings_ms[name] = round((time.perf_counter() - start) * 1000, 1)
trace = telemetry.Trace(q)
with trace.step("cache"):
    hit = semantic.lookup(q)
...
with trace.step("retrieval"):
    found = hybrid.search(q, 5)
with trace.step("generation"):
    trace.answer, trace.cited = answer_with_citations(q, found)

Prism writes traces as JSON Lines, the simplest format to work with. In production, you would rather use an LLM observability platform (Langfuse, LangSmith, Arize Phoenix…) or OpenTelemetry: the principles stay the same. Add the pipeline version (models, prompts, parameters) to every trace, to link a regression to a change.

Traces contain users' questions, hence potentially personal data: define a retention period, mask what must be masked, and restrict access.

The weekly review

Traces are useless if nobody reads them. Prism produces a report to review every week:

def weekly_review(traces: list[Trace]) -> dict:
    """Le rapport à relire chaque semaine."""
    answered = [t for t in traces if not t.unanswered]
    rated = [t for t in traces if t.feedback is not None]
    latencies = sorted(sum(t.timings_ms.values()) for t in traces) or [0.0]
    return {
        "requetes": len(traces),
        "sans_reponse": [t.question for t in traces if t.unanswered],
        "mal_notees": [t.question for t in rated if t.feedback < 0],
        "taux_satisfaction": sum(t.feedback > 0 for t in rated) / len(rated) if rated else None,
        "taux_cache": sum(t.cache_hit for t in traces) / len(traces) if traces else 0.0,
        "latence_p95_ms": latencies[int(0.95 * (len(latencies) - 1))],
        "articles_les_plus_cites": Counter(a for t in answered for a in t.cited).most_common(5),
    }

(The keys: queries, unanswered, badly rated, satisfaction rate, cache rate, p95 latency, most cited articles.) Each section triggers a specific action:

SectionAction
Unanswered questionsGaps in the corpus: write the missing articles (failure 1), or check that retrieval did not miss them
Badly rated answersReplay each case, locate the failing step with the trace, add it to the evaluation set
Satisfaction rateFollow the trend, especially after a change
p95 latencyThe 95th percentile, not the average: what the slowest users experience
Most cited articlesThe critical articles: to be kept perfectly up to date

An "unanswered question" here is an answer without a citation. In Prism's offline example, it is « Vendez-vous des imprimantes 3D ? » (do you sell 3D printers?) — a real gap in the corpus, or an out-of-scope request that deserves a clear answer.

The improvement loop

production ─► traces ─► weekly review ─► failing cases ─► evaluation set
     ▲                                                          │
     └──── deployment ◄── before/after comparison ◄── fix ◄─────┘
  1. Collect: every failing case (badly rated, unjustified non-answer, reported error) becomes a question of the evaluation set, with its reference answer.
  2. Fix: depending on the failure point — corpus, chunking, retrieval, re-ranking, prompt.
  3. Compare: rerun the full test bench on the enriched set, before and after the fix. A fix that repairs one case and breaks three others shows up here, not in production.
  4. Deploy, then monitor the same indicators.

The evaluation set thus grows with real failures: it is the best investment in a RAG project, and it is what lets you change model, index or technique with confidence.

What to monitor continuously

Beyond the weekly review, a few automatic alerts:

  • a sudden rise in the share of answers without citations (an empty index, a broken filter, a deleted source);
  • a drop in the distribution of re-ranker scores (new questions the corpus does not cover);
  • p95 latency exceeding the budget;
  • cost per query drifting (an agent that loops, a prompt cache that no longer works).

Final word

This course went through a complete RAG pipeline, and every technique was judged on measurements. On Prism, hybrid search and re-ranking did more than all the other techniques combined; LLM-based techniques (rewriting, context, graph, agent, self-correction) have specific use cases, and a cost that must be justified. On your corpus, the ranking may be different. The method, however, does not change: an evaluation set, a test bench, traces, and the discipline of measuring before adding complexity.

Key takeaways

  • Trace every query: passages and scores, answer and citations, per-step durations, user feedback, pipeline version.
  • Review every week the unanswered questions, badly rated answers, p95 latency, critical articles.
  • Every failure joins the evaluation set; every fix is compared before/after on the whole set.
  • Alert on drift: answers without citations, re-ranker scores, latency, cost.
  • Measure before adding complexity: that method makes a reliable RAG, more than any technique.

Practice

  1. Run examples/06_evaluation_and_ops.py and read the trace file it produces: find the query served from the cache.
  2. Add the pipeline version (embedding model, re-ranker, prompt) to Trace, and a breakdown by version to the report.
  3. Write an alert that fires if more than 20% of the answers in the last hour have no citation.

Resources