Sub topic 10
Judging Context Relevance When You Have No Labels
What this chapter assumes
You should know how a RAG pipeline retrieves passages and passes them to a generator. You should also know precision@k and recall@k from earlier chapters, and you should be able to read short Python functions. This chapter also uses the idea of an LLM judge, which is a language model prompted to grade another model's output.
The problem: labels are expensive
Precision@k and recall@k need a labelled set that says which documents are relevant to each question. Building that set takes human time. In production you also see new questions every day, and none of them have labels.
Context relevance is a judgment that works without labels. For each question, you ask a judge whether each retrieved passage helps answer it. The judge's verdicts stand in for the labels. The result is a label-free approximation of retrieval precision.
What context relevance measures
Three scores are easy to confuse, so keep their inputs apart.
| Score | Compares | Question it answers |
|---|---|---|
| Context relevance | question and retrieved passages | Did retrieval bring back useful material? |
| Faithfulness | answer and retrieved passages | Is the answer supported by that material? |
| Answer relevance | question and answer | Does the answer address what was asked? |
Context relevance never looks at the generated answer. This is useful for diagnosis. If context relevance is low, the retriever is the first suspect, whatever the generator did.
"Relevant" needs a precise definition, or the judge will drift. A passage that is on the same topic as the question is not necessarily relevant. A passage is relevant if it contains information that helps answer the question. A passage about warranty coverage does not help with "How long does the warranty last?" unless it states a duration.
Passage-level judging
The most common method scores each retrieved passage on its own.
- Take the question and the top k retrieved passages.
- For each passage, ask the judge for a verdict: relevant or not (binary), or a grade such as 0 to 3.
- Aggregate the verdicts into a score for the question, then average across questions.
The aggregate can be the share of passages judged relevant, which is a label-free precision@k. It can also be rank-aware. A rank-weighted version rewards the system for putting relevant passages early, in the same spirit as average precision.
The code below separates the judge from the aggregation. The judge is a plain function you can replace with a real LLM call. The toy judge checks word overlap and exists only so the example runs without a model.
import re
STOPWORDS = {"the", "a", "an", "is", "of", "how", "long", "does",
"what", "do", "for", "to", "in", "and", "it"}
def content_words(text):
return {w for w in re.findall(r"[a-z0-9]+", text.lower())
if w not in STOPWORDS}
def toy_judge(question, passage):
"""Return 1 if the passage shares at least half of the question's content words."""
q_words = content_words(question)
if not q_words:
return 0
overlap = len(q_words & content_words(passage)) / len(q_words)
return 1 if overlap >= 0.5 else 0
def judge_contexts(question, passages, judge):
return [judge(question, p) for p in passages]
def context_precision(judgements):
if not judgements:
return None # no passages were retrieved; do not report 0 or 1
return sum(judgements) / len(judgements)
def rank_weighted_precision(judgements):
"""Average of precision@i over the ranks i where the judge said 'relevant'."""
precisions = []
hits = 0
for i, verdict in enumerate(judgements, start=1):
if verdict:
hits += 1
precisions.append(hits / i)
return sum(precisions) / len(precisions) if precisions else 0.0
question = "How long does the warranty last?"
passages = [
"Shipping takes 3 days.",
"The warranty lasts 24 months.",
"Our warranty covers manufacturing defects.",
]
verdicts = judge_contexts(question, passages, toy_judge)
print(verdicts) # [0, 1, 1]
print(round(context_precision(verdicts), 3)) # 0.667
print(round(rank_weighted_precision(verdicts), 3)) # 0.583The toy judge marks the third passage relevant because it shares the word "warranty". The passage never says how long the warranty lasts, so a careful human would mark it not relevant. This is the same failure you will see from a weak LLM judge: it rewards topical overlap and not usefulness.
Passage-level versus sentence-level
A retrieved chunk often has one useful sentence among several irrelevant ones. A passage-level verdict hides this. Two common refinements help.
- Sentence-level judging. Split each passage into sentences and judge each one. The share of useful sentences across all retrieved text measures how much of the prompt is noise. Low values suggest your chunks are too large.
- Extraction. Ask the judge to quote the sentences that help answer the question, and score the fraction of context tokens that were quoted. The quotes are also useful when you debug.
Sentence-level judging costs more calls. Use it on a sample when you tune chunk size, and use passage-level judging for routine runs.
How the pieces fit
What this method cannot see
Label-free context relevance measures precision only. It tells you whether what you retrieved was useful. It cannot tell you whether something useful was missed. Recall needs to know what the corpus contains, and the judge only sees the passages you give it.
For example, a retriever may return one excellent passage and four off-topic ones, giving a precision of 0.2. A second retriever may return only that excellent passage, giving 1.0. Neither score shows that a second needed passage exists in the corpus and was never retrieved.
Two partial workarounds exist.
- Retrieve a much larger set, such as the top 50, and have the judge grade all of it. Treat the relevant passages found there as a pseudo-label set, then compute recall for smaller k against it. This only finds relevant passages that a broad retrieval surfaces.
- Keep a small labelled set for recall, and use judging for the unlabelled bulk of traffic.
Common judge errors
An LLM judge is a model with its own biases. Several matter specifically for context relevance.
| Error | What happens | Mitigation |
|---|---|---|
| Topical leniency | On-topic passages that lack the answer are marked relevant | Define relevance as "helps answer", add negative examples |
| Position bias | Passages shown first, or in a long list, are judged differently | Judge each passage separately, or shuffle the order |
| Length bias | Long, detailed passages get higher grades | Compare scores across passage lengths, and prefer binary verdicts |
| Self-preference | A judge from the same model family as the system agrees with it too readily | Use a different model for judging than for generating |
| Instability | The same input gets different verdicts across runs | Use temperature 0, and repeat a sample of calls to measure agreement |
Judging each passage separately removes most position effects and makes the calls easy to parallelize. The cost is that the judge cannot see redundancy. Three passages that repeat the same fact will all be marked relevant.
Practical setup
- Write the judge prompt with a precise definition of relevant, two or three examples, and an output format that is easy to parse, such as a single word.
- Use binary verdicts at first. Graded scales (0 to 3) are noisier and need more validation.
- Log the question, passage, verdict, and the judge's short reason. The reasons make disagreements easy to inspect.
- Check a sample of verdicts by hand before you trust the numbers. The next chapters cover this in detail: a judge is validated by comparing its verdicts with human labels and computing agreement.
- Report the score with the retriever settings that produced it, such as k, chunk size, and embedding model. Changes in context relevance only mean something when you know what changed.
Exercises
Exercise
A retriever returns five passages for a question. A judge marks passages at ranks 2 and 4 as relevant and the others as not relevant. Compute the context precision and the rank-weighted precision using the definitions in this chapter.
Show answer
Context precision is 2/5 = 0.4. For the rank-weighted score, precision@2 is 1/2 = 0.5 and precision@4 is 2/4 = 0.5. The average of these is 0.5.
Exercise
Your context relevance score is 0.9, but users report that answers are often incomplete. Name the likely cause and one way to check it.
Show answer
The likely cause is low recall. The judge only grades passages that were retrieved, so it cannot detect relevant passages that were missed. To check, build a small labelled set for the incomplete questions and compute recall@k, or retrieve a larger top-k and see whether additional relevant passages appear that were not in the original top k.