How do you choose Top-K in RAG?
Choose Top-K in RAG by testing how many retrieved chunks are needed to find the right evidence without adding unnecessary latency, tokens, or irrelevant context. There is no universal best K.
What does K control?
Top-K is the number of highest-ranked results requested from a retriever. For example, K = 5 requests up to five candidate chunks. The number ultimately sent to the LLM can be different after filtering, deduplication, or reranking.
How to choose K
- Start with a small baseline: Try values such as K = 3, 5, and 10 on representative user questions. These are test settings, not fixed recommendations.
- Measure evidence coverage: Use labeled query–document pairs to calculate Recall@K or Hit Rate. Check whether the answer-bearing chunks are included.
- Check final answer quality: Measure groundedness and answer relevance. More retrieved chunks do not necessarily produce better responses.
- Track latency and cost: Larger K can increase reranking work, prompt tokens, and generation cost. Also observe p95 latency rather than just averages.
- Account for chunk size: Five short chunks and five long chunks may consume very different context budgets.
- Tune by query type: A straightforward factual question may need one or two supporting passages; comparison or multi-hop questions may need evidence from multiple sources.
- Keep access filters consistent: Only authorized chunks should be eligible, regardless of K.
Practical example
Suppose an HR RAG assistant answers questions using policy documents.
- With K = 3, the leave-policy definition is retrieved but an exception clause is often missed.
- With K = 8, both definition and exception usually appear.
- With K = 20, coverage may improve little while irrelevant policies add noise and context cost.
You would compare those settings on a held-out evaluation set and choose the smallest K that meets the required evidence-retrieval quality and latency budget. The numbers above are illustrative; actual results require measurement.
Initial retrieval K versus final context K
A common two-stage pattern is:
- Retrieve Top-20 candidates cheaply.
- Rerank those candidates with a stronger model.
- Pass only the best Top-5 chunks, subject to a token budget.
This separates candidate coverage from the amount of context given to the LLM. The appropriate values depend on measured quality and cost.
Common mistakes
- Assuming larger K always improves answers.
- Evaluating only vector similarity instead of answer-bearing evidence.
- Ignoring near-duplicate chunks, which can crowd out different relevant sources.
- Setting K without considering chunk length and context-window limits.
- Choosing K from one example rather than a representative dataset.
Interview takeaway
Pick Top-K empirically. Compare several values using evidence Recall@K, answer quality, p95 latency, and token cost. Use a larger candidate pool when reranking is available, but send only the strongest, nonredundant evidence to the generator.
Learning Sequence
Retrieval-Augmented Generation Curriculum
Explore Related Questions
Deepen your understanding across related Retrieval-Augmented Generation topics.