Lesson 26.7 · 7 of 7 · 6 min read · Reviewed 24 Nov 2026
RAG eval frameworks: RAGAS, TruLens and rolling your own
By the end, you can
- Describe what RAG eval frameworks such as RAGAS and TruLens provide.
- Decide when to adopt a framework and when to write your own small harness.
What a framework gives you
By now you have met the parts of a RAGRetrieval-augmented generation (RAG): Retrieving relevant documents at request time and adding them to the prompt so the answer is grounded in them. evalEval: A repeatable test that measures how well an AI system performs on a set of cases with defined success criteria.: a test set (26.3, 26.6), retrieval metrics (26.1, 26.4, 26.5) and answer metrics (26.2). Eval frameworks bundle these so you don’t start from a blank file. Typical features:
- Ready-made metric functions, most of them model judges with tuned prompts.
- Test set generation from your documents.
- Integrations with common RAG libraries and tracingTrace: A record of everything that happened during one request or agent run, as a tree of timed steps. tools.
- Reports and dashboards that compare runs.
Two common shapes
| RAGAS | TruLens | |
|---|---|---|
| Main idea | Score a dataset of test cases with metric functions | Instrument a running app, record each call, attach feedback functions |
| Typical input | Rows of question, retrieved contexts, answer, reference | Traces from your app as it runs |
| Headline metrics | Faithfulness, answer relevancy, context precision, context recall | The RAG triad: context relevance, groundedness, answer relevance |
| Feels like | An offline test suite | Observability with scores attached |
Other tools in the same space include DeepEval, Arize Phoenix and the eval features in LangSmith and similar platforms. They overlap heavily. The concepts in this chapter carry across all of them, even when names differ: “groundedness” in one tool is close to “faithfulnessFaithfulness: Whether every claim in an answer is supported by the retrieved context.” in another.
What frameworks don’t solve
- Your test set. No library knows which questions your users ask or what the right answers are.
- Judge trust. A framework metric is a prompt plus a model. Upgrading the library or switching the judge model can move scores with no change to your app. Pin versions, and check each judge against 30 to 50 hand-labelled cases.
- Thresholds. Deciding that faithfulness must be at least 0.9 for release is a product decision.
Rolling your own
A small harness is often enough, and you understand every number in it:
for case in cases:
hits = retrieve(case["question"], k=5)
ids = [h["id"] for h in hits]
row = {
"recall@5": any(i in case["relevant"] for i in ids),
"rr": next((1 / (r + 1) for r, i in enumerate(ids) if i in case["relevant"]), 0),
"faithful": judge_faithfulness(answer(case, hits), hits), # one prompt you own
"tags": case["tags"],
}
results.append(row)
report_by_tag(results)
Choosing
| If you… | Lean towards |
|---|---|
| Need a first eval this week and use a common RAG stack | A framework, with pinned versions |
| Want scores on live traffic and traces | A tracing-first tool |
| Have unusual metrics or strict audit needs | Your own harness |
| Already have an eval platform for other features | Its RAG metrics, plus your own retrieval checks |
Many teams mix: code metrics they own for retrieval, a framework judge for faithfulness, and the same CIContinuous integration (CI): Automatically building and testing code on every change, usually in a hosted pipeline. gate for both (chapter 20).
Common mistakes
- Reporting a framework’s single combined score without the parts.
- Comparing scores produced by different judge models or library versions.
- Adopting a framework before writing down what “good” means for your product.
Key points
- Frameworks package common metrics (faithfulness, relevance, context precision and recall), judge prompts, test set generation and dashboards.
- RAGAS centres on metric functions over test cases; TruLens centres on tracing a running app and scoring each step, framed as the RAG triad.
- Every framework metric is a model judge with its own prompt: pin versions and check it against your own labels.
- A small harness of your own is often enough: labelled cases, a few code metrics, one judge prompt, results per tag.
Roll a tiny RAG eval harness
Build a minimal harness that retrieves, answers and scores a few cases, then reports results per tag, so you understand what a framework does for you.
You will have: A runnable harness that prints recall@k, reciprocal rank and a faithfulness flag per case, plus a per-tag summary.
Steps
- Read the small corpus and the four test cases.
- Write retrieve: rank chunks by word overlap with the question and return the top k ids.
- Write a stand-in answer function that copies the top chunk, and a faithfulness check that the answer is in a retrieved chunk.
- Score each case for recall@k, reciprocal rank and faithfulness.
- Print a per-tag summary, then note which tags have too few cases to trust.
Done when
- Each case prints recall@2, RR and faithful on one line.
- The per-tag summary includes single-fact and multi-hop.
- The unanswerable case is reported as a correct refusal.
Open in the Playground No API key needed.
Starter code (Python)
CORPUS = {
"pol-4.2": "Footwear may be returned within 30 days if unworn.",
"pol-4.3": "Sale items are final sale unless faulty.",
"pol-2.5": "Gift cards expire 24 months after purchase.",
"pol-7.1": "Standard delivery takes three to five working days.",
}
CASES = [
{"q": "How many days do I have to return unworn footwear?", "relevant": {"pol-4.2"}, "tags": ["single-fact"]},
{"q": "When do gift cards expire?", "relevant": {"pol-2.5"}, "tags": ["single-fact"]},
{"q": "Can I return worn sale footwear that is faulty?", "relevant": {"pol-4.2", "pol-4.3"}, "tags": ["multi-hop"]},
{"q": "Do you price match other shops?", "relevant": set(), "tags": ["unanswerable"]},
]
def words(t):
return {w.strip(".,?").lower() for w in t.split()}
def retrieve(question, k=2):
# TODO: rank CORPUS ids by shared words with the question; return the top k ids with score > 1
return []
def answer(question, ids):
return CORPUS[ids[0]] if ids else "Not in the sources."
for c in CASES:
ids = retrieve(c["q"])
print(c["tags"][0], ids, answer(c["q"], ids))
# TODO: score recall@2, reciprocal rank and faithfulness; print a per-tag summary
Show a solution
CORPUS = {
"pol-4.2": "Footwear may be returned within 30 days if unworn.",
"pol-4.3": "Sale items are final sale unless faulty.",
"pol-2.5": "Gift cards expire 24 months after purchase.",
"pol-7.1": "Standard delivery takes three to five working days.",
}
CASES = [
{"q": "How many days do I have to return unworn footwear?", "relevant": {"pol-4.2"}, "tags": ["single-fact"]},
{"q": "When do gift cards expire?", "relevant": {"pol-2.5"}, "tags": ["single-fact"]},
{"q": "Can I return worn sale footwear that is faulty?", "relevant": {"pol-4.2", "pol-4.3"}, "tags": ["multi-hop"]},
{"q": "Do you price match other shops?", "relevant": set(), "tags": ["unanswerable"]},
]
def words(t):
return {w.strip(".,?").lower() for w in t.split()}
def retrieve(question, k=2):
scored = sorted(((len(words(question) & words(t)), cid) for cid, t in CORPUS.items()), reverse=True)
return [cid for s, cid in scored[:k] if s > 1]
def answer(question, ids):
return CORPUS[ids[0]] if ids else "Not in the sources."
summary = {}
for c in CASES:
ids = retrieve(c["q"])
ans = answer(c["q"], ids)
if c["relevant"]:
rec = any(i in c["relevant"] for i in ids)
rr = next((1 / (r + 1) for r, i in enumerate(ids) if i in c["relevant"]), 0.0)
faithful = any(ans == CORPUS[i] for i in ids)
ok = rec and faithful
print(f"{c['tags'][0]:12} recall@2={'hit ' if rec else 'miss'} RR={rr:.2f} faithful={faithful} {ids}")
else:
ok = ans == "Not in the sources."
print(f"{c['tags'][0]:12} correct refusal={ok}")
for t in c["tags"]:
p, n = summary.get(t, (0, 0))
summary[t] = (p + ok, n + 1)
print("by tag:")
for t, (p, n) in summary.items():
print(f" {t:12} {p}/{n}")
Finished the chapter? Take the chapter check.