Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 68/ 160
04. Evals

Context Precision

How much of the retrieved context was actually useful for the answer.

IntermediateUseful Context · Noise3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

Context precision asks how many of the documents the system fetched were actually helpful, and whether the helpful ones came first.

Think of it like…

Pulling books for a research question: precision is how many of the books on your trolley are useful (and whether the best ones are on top); recall is whether you found all the books you needed.

Where you’ve seen it

For "What's the refund window for electronics?", the retriever returns five chunks. Relevant ones are at rank 1 and rank 3. Precision@5 = 2/5 = 0.40. The rank-aware version (RAGAS-style context precision) averages precision at each relevant position: (1/1 + 2/3) / 2 = 0.83, a high score because the relevant chunks are near the top. The three irrelevant chunks average 400 tokens each, wasting 1,200 tokens per query: at 50,000 queries a day, 60M tokens, roughly $150 a day at $2.50 per million input tokens. Adding a reranker puts both relevant chunks at ranks 1 and 2, so sending only the top 2 lifts precision to 1.0 (from 0.40 at k = 5) while recall stays complete.

Words you’ll hear

Precision@k
Fraction of the top k retrieved chunks that are relevant.
Context precision (rank-aware)
Average of precision@k taken at each relevant chunk's rank, rewarding relevant chunks ranked early.
Context recall
Fraction of the facts needed for the answer that appear in the retrieved context.
Distractor
A retrieved but irrelevant (or misleading) chunk that can derail the model.

Visual diagram

Loading interactive diagram

Theory & deep dive

Computing the metrics

You need relevance labels for retrieved chunks. Sources: labelled golden data (the passages known to contain the answer), a reference answer the judge compares chunks against, or an LLM judge asking "is this chunk useful for answering the question?" (validate it on human labels). Precision@k = relevant retrieved in top k / k. The rank-aware variant, as in RAGAS, computes precision@j at every rank j where a relevant chunk appears and averages them, so [relevant, irrelevant, relevant, irrelevant, irrelevant] scores (1/1 + 2/3)/2 = 0.83, while the same chunks in the order [irrelevant, irrelevant, irrelevant, relevant, relevant] score (1/4 + 2/5)/2 = 0.325.

Context recall = answer-relevant facts found in the context / total facts needed (take the facts from the reference answer, split into claims). It tells you whether the retrieval could have supported a correct answer. Related ranking metrics (recall@k, MRR, nDCG) cover chunk-level retrieval relative to labelled passages; context precision and recall are the answer-oriented versions that work well when the exact gold chunks are unknown.

Order changes the score

Same five chunks, two relevant. Relevant first: (1/1 + 2/2) / 2 = 1.0. Relevant at ranks 4 and 5: (1/4 + 2/5) / 2 = 0.325. Precision@5 is 0.4 in both cases, which is why rank-aware scoring exists.

Key takeaway: Label chunk relevance, compute precision@k and the rank-aware score, and pair with context recall.

Improving them

Low precision with high recall means you're retrieving the right material plus noise. Remedies: add a cross-encoder reranker and cut k, filter with metadata (date, product, language), use hybrid search so exact terms rank properly, tune chunk size (large chunks dilute relevance, tiny chunks lose context), and rewrite vague queries. Low recall means the evidence isn't coming back: improve chunking and parsing, query rewriting or HyDE, increase k before reranking, add keyword search for rare identifiers, or fix indexing gaps (documents never ingested).

Trade-offs: a larger k raises recall and lowers precision, so the usual pattern is retrieve broadly (k = 30 to 50), rerank, and send only the top 3 to 8 chunks to the model. Measure the downstream effect, since a less precise context can still give a good answer with a strong model; look at faithfulness and correctness alongside. Slice results by query type (lookup versus multi-hop), because averages hide the questions where recall is zero. Re-run when you change embeddings, chunking or the index.

Broad then narrow

Retrieve k = 40 with hybrid search (recall 0.96), rerank with a cross-encoder, keep the top 5. Precision@5 rises from 0.38 to 0.71 with recall at 0.93, and prompt input drops from 16,000 to 2,000 context tokens.

Key takeaway: Retrieve broadly, rerank, send few; tune precision and recall separately.

How it works, step by step

Step 1 of 4
Eval design

1. Label relevance

Mark which retrieved chunks are useful using golden passages, a reference answer or a validated judge.

ranks 1 and 3 relevant
Key numbers at this step
Validate
judge on humans

When to use it & trade-offs

What it is

Context precision measures how much of the retrieved context is actually useful for answering the question, and whether the useful chunks are ranked at the top. If you retrieve five chunks and only two are relevant, precision@5 is 0.4. Its counterpart, context recall, asks whether the retrieved context contains all the information needed. Together they diagnose retrieval: low precision means noise, wasted tokens and distraction; low recall means the model can't answer correctly however good it is.

Why it matters: Retrieval quality caps RAG quality, and in error analyses it is usually the biggest single cause of failures. Irrelevant chunks cost money (every extra chunk is tokens), add latency and actively mislead models (the "lost in the middle" and distractor effects). Context precision tells you whether to tighten top-k, add a reranker or fix chunking, independently of how the generator behaves.

When to reach for it

  • Deciding top-k, reranking and chunking changes
  • Diagnosing whether failures are retrieval or generation
  • Estimating wasted context tokens and cost

When to avoid or be careful

  • Raising k to chase recall without reranking
  • Using precision@k alone when order matters
  • Judging relevance without human validation

Retrieval-quality metrics

Context precision
Best for: Noise and ordering of retrieved context

Strengths: Shows wasted tokens and distraction risk.

Watch out: Needs relevance labels.

Context recall
Best for: Whether the needed facts were retrieved

Strengths: Identifies hard retrieval failures.

Watch out: Needs reference answers or facts.

Answer-level metrics (faithfulness, correctness)
Best for: End-to-end outcomes

Strengths: Reflect user experience.

Watch out: Don't say where the failure originated.

Interview signals & numbers to know

When this topic comes up

  • “The right document is retrieved but buried”
  • “We send 10 chunks to the model”
  • “Context precision and recall in RAGAS”
  • Distractor chunks
  • Token cost of context

Benchmarks & rules of thumb

0.40 / 0.83 -
precision@5 vs rank-aware score in the example
1,200 tokens -
wasted per query by 3 irrelevant chunks
40 → 5 -
retrieve broadly, then rerank and keep few

Common mistakes & follow-ups

Common interview mistakes

  • Treating all retrieved chunks as equally useful regardless of rank.
  • Increasing k until recall looks good and flooding the prompt with noise.
  • Ignoring recall because precision looks fine.

Likely interviewer follow-ups

  • How do context precision and context recall differ?
  • How would you improve low context precision?
  • Why does ranking matter inside the prompt?
Interview tip: Convert the noise into tokens and dollars (1,200 wasted tokens per query). It ties a retrieval metric to a business cost.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Context Precision with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Answer Relevance
Next topic
Tool Call Accuracy