Search⌘ K
AI Features

Measuring Retrieval Precision, Recall, and Context Utilization

Explore how to build a labeled evaluation set and measure retrieval precision, recall, and context utilization. Compare top-k settings, identify missed evidence and unnecessary documents, choose a defensible retrieval cutoff, and track how much context each answer actually uses.

We'll cover the following...

Over this chapter, we’ve built a full retrieval pipeline for the on-call assistant. hybrid_search() combines keyword and meaning-based ranking, the filters drop stale and unapproved notes, edge ordering places the strongest notes at the start and end, and verify_quotes() checks every quote against what the model was given.

All of that makes one answer more reliable. It doesn’t tell us whether the retrieval is good in general. Does it find the right notes for most questions? How many useless notes come along? And how much of what we send does the answer actually use? Until now, we’ve judged this by looking at one question at a time.

In this lesson, we’ll turn those questions into numbers. We’ll write a small set of labeled questions, measure precision and recall on it, compare different values of top_k, and measure context utilization, the share of sent tokens the answer actually uses.

Label a set of questions

To measure retrieval, we first need to know the right answer. We’ll write a few real questions and, for each one, list the documents a person would call relevant. This is a labeled evaluation set. We write it once, before looking at any results, and reuse it every time the pipeline changes.

Our set covers five kinds of questions the assistant gets:

  • The running question: The current runbook, the tax-service postmortem, and the flags reference are relevant, since a good answer uses all three.

  • The cache question: The cache guideline and the current runbook both say not to restart Redis.

  • A paging question: Only the payments on-call rotation is relevant.

  • A search question: Only the search-latency runbook is relevant.

  • A code question: Only the fetch_tax_breakdown function in the checkout service is relevant.

Let’s write these labels down in code.

eval_set.py · Python 3.14.0
EVAL_QUERIES = [ # each question with the documents a person marked as relevant, written down once
("Checkout latency spiked at 14:05. What changed, and what should we roll back?",
{"note-1", "note-2", "note-3"}),
("Is it safe to clear the cache right now?", {"note-8", "note-1"}),
("Who do I page if the payments engineer does not answer?", {"note-6"}),
("Search latency is spiking. What do we do?", {"note-5"}),
("Where does checkout call the tax service?", {"checkout_service.py::fetch_tax_breakdown"}),
]
if __name__ == "__main__":
for question, relevant in EVAL_QUERIES:
print(f"{len(relevant)} relevant | {question}")

In the code above:

  • Lines 1–8: We define EVAL_QUERIES, a list of questions, each paired with the set of relevant document IDs.

  • Lines 10–12: We ...