RAG Top-K Tuning: Finding the K That Fits Your Corpus

RAG Top-K Tuning: Finding the K That Fits Your Corpus

RAG top-k tuning is the practice of choosing k — the number of passages your retriever returns for each query — and adjusting it until answer quality, cost and latency balance out. The central question is what value of k to use, and the short answer is that the right k is the smallest one that still retrieves the passages your answers need, measured on your own questions rather than copied from a tutorial.

Top-k is the most visible parameter in a RAG system. It appears in every example, every framework default and every getting-started guide, which makes it tempting to leave at its default forever. Defaults are chosen for demos, though, not for your corpus. A k that works for a small set of short documents can fail badly on a large collection of long ones — and the failure is silent, because the generator simply answers from whatever it was given.

Key takeaways

  • RAG top-k tuning sets how many passages retrieval returns per query; it is the most direct lever on coverage, cost and latency.
  • Too small a k starves the answer; too large a k floods the generator with noise.
  • There are usually two k values to tune: the retrieval k and the smaller k that reaches the generator after reranking.
  • Score thresholds and reranking change what any given k is worth.
  • Tune k with a fixed test set and a quality metric — never by feel.

What k actually controls

In vector search, k is the number of nearest neighbours returned for a query embedding. In keyword search, it is the number of top-ranked documents. In a hybrid system, it is the size of the merged shortlist. Whichever retriever you use, k sets the size of the pool that every later step works from. Nothing downstream can use a passage that k excluded.

That makes k a coverage control first and a quality control second. Raising k increases the chance that the right passage is in the pool — the property usually called recall. But it also increases the share of irrelevant passages, which drags down precision. Every value of k is a point on that trade-off, and the best point depends on how good your retriever is. A strong retriever ranks the right passages at the top, so a small k suffices. A weak retriever buries them, so only a large k finds them.

It helps to think of k as a budget rather than a target. You are buying coverage with latency, tokens and noise. Spend the budget where it returns value, and stop spending when the returns flatten. That framing also explains why k should be revisited: as the retriever, the corpus or the reranker improves, the same coverage costs less k.

The two k values in a modern pipeline

Most production pipelines have two k values, and confusing them is the most common tuning mistake. The first is the retrieval k: how many candidates come back from the index. The second is the context k: how many passages survive reranking and reach the generator. The first is often 20, 50 or 100. The second is often 3 to 10.

The two values serve different purposes. Retrieval k exists to give the reranker enough material to choose from. It should be generous, because the cost of a missed passage at this stage is final. Context k exists to give the generator a clean, focused set of evidence. It should be restrained, because every extra passage is noise the generator must sift through.

Tune them separately. First, find the retrieval k at which the reranker stops finding new relevant passages — beyond that point, more candidates add nothing. Then, find the context k at which answer quality stops improving. Tuning them together muddles the results: you will not know whether a change helped because the pool grew or because the context shrank. Our guide to RAG reranking models explains how the second stage uses the pool the first stage provides.

How k interacts with reranking and thresholds

Reranking changes the economics of k. Without a reranker, the generator reads passages in retrieval order, so a large k means a long, noisy context. With a reranker, a large retrieval k is much safer: the reranker filters the pool before the generator ever sees it. Teams that add a reranker can often raise retrieval k substantially at little cost to answer quality — and should, because the reranker can only choose from what retrieval returned.

Score thresholds are the other interaction worth understanding. A threshold discards passages whose similarity score falls below a cutoff, regardless of k. Used well, a threshold makes k an upper bound rather than a fixed count: easy queries return few passages, hard queries return many. Used badly, a threshold silently discards the only relevant passage because its score was low in absolute terms. Similarity scores are not comparable across queries, so a threshold that works for one question type may fail for another. If you use thresholds, validate them per query type and monitor how often they empty the result set.

Hybrid search adds a third interaction. When keyword and vector results are merged, k applies to the merged list, and the merge method decides the balance between the two. A system that takes the top 10 from each side and merges them behaves differently from one that takes the top 20 combined. If your hybrid results look skewed toward one method, the merge and the k are the first places to look. Our guide to hybrid search for RAG covers the merge options.

Hands turning a single large dial on a minimalist control board with five small indicator lights
Top-k is a budget, not a target: spend it where it buys coverage, and stop when the returns flatten.

A practical method for tuning k

Tuning k is straightforward once you have a test set. Start with questions drawn from real usage, covering your main query types, and include questions whose answers need several passages. For each question, record which documents contain the answer. That gives you a ground truth for recall: at a given k, did retrieval return the documents the answer needed?

Run the pipeline at several values of k — for example, retrieval k of 5, 10, 20, 50 and 100, with a fixed context k — and record recall and answer quality at each step. The recall curve will rise quickly and then flatten. The quality curve will rise, flatten, and sometimes fall as noise grows. The right retrieval k sits near the start of the recall plateau: the smallest value that captures nearly all the available recall.

Then repeat the exercise for context k, holding retrieval k fixed. Try 3, 5, 8 and 12 passages to the generator and score the answers for groundedness and completeness. Pick the smallest context k that reaches near-maximum quality. The two plateaus together define your tuned configuration. The evaluation approach in our RAG evaluation metrics guide gives you the scoring methods, and the optimisation habits in RAG retrieval optimisation show how k fits alongside the other dials.

Retrieval k What usually happens Watch for Best for
3–5 Fast, cheap, narrow Missed facts, brittle to chunking Simple factual questions
10–20 Good balance for most systems Some multi-hop answers incomplete General-purpose RAG
20–50 Strong coverage before reranking Higher latency, reranker load Systems with a reranker
50–100 Maximum recall from the index Noise, cost, slow reranking Large corpora, weak first-pass retrieval

Best for most teams: a retrieval k of 20 to 50 feeding a reranker, with a context k of 5 to 8 reaching the generator — then tune both on your own data.

When k is not the problem

It is tempting to treat k as the fix for every retrieval failure, but some failures do not respond to it at all. If the right document is not in the corpus, no k will find it. If the chunking split the answer across a boundary, a larger k may return both halves — or it may return neither, if both halves rank poorly. If the query is vague, the retriever cannot know what to fetch, and the fix is query handling, not a bigger k.

The diagnostic is simple. For a failing question, check whether the needed passage appears anywhere in the top 100 results. If it does, k or reranking is the problem. If it does not, the problem is upstream: the embedding model, the chunking, the query itself or a missing document. Raising k past 100 rarely helps in that case, and it costs latency on every query. Our guide to RAG context recall walks through this diagnosis in more detail.

Latency deserves a final note. Retrieval time grows with k, reranking time grows with k, and generation time grows with context k. A system that answers in under a second at k of 10 may take several seconds at k of 100, and users notice. The latency breakdown in RAG retrieval latency shows where the milliseconds go, and it is worth reading before you raise k in a user-facing system.

Top-k tuning checklist

  • Build a test set with ground truth. Know which documents each answer needs.
  • Separate the two k values. Tune retrieval k and context k independently.
  • Test at least five values. Compare retrieval k of 5, 10, 20, 50 and 100.
  • Plot recall and quality. Find where each curve flattens.
  • Pick the plateau, not the peak. Choose the smallest k that reaches near-maximum quality.
  • Check the reranker first. A bigger k only helps if the reranker can use the extra candidates.
  • Validate thresholds per query type. Scores are not comparable across question types.
  • Price and time the choice. Measure latency and cost per query at real volume.
  • Re-tune after changes. New chunks, models or corpora move the optimum.

Frequently asked questions

What is RAG top-k tuning?

RAG top-k tuning is choosing k, the number of passages retrieval returns per query, by testing several values on your own questions and picking the smallest one that still retrieves the passages your answers need.

What is a good starting value for k?

A retrieval k of 20 to 50 with a reranker, narrowed to 5 to 8 passages for the generator, is a sensible starting point for most systems. Treat it as a starting point, not a final answer, and tune from there.

Does a larger k always improve retrieval?

It improves recall — the chance the right passage is in the pool — but it also adds irrelevant passages that can confuse the generator and slow the system down. Past a point, the extra coverage is not worth the noise.

What is the difference between top-k and retrieval depth?

They describe the same idea from different angles. Top-k is the parameter you set; retrieval depth is the resulting behaviour. In practice, retrieval k sets candidate depth and context k sets the depth the generator actually reads.

Should I use a similarity score threshold as well as k?

You can, but carefully. A threshold makes k an upper bound rather than a fixed count, which suits easy queries. Validate thresholds per query type, because similarity scores are not comparable across different questions.

How often should k be re-tuned?

Whenever the corpus, chunking, embedding model or reranker changes, and at least twice a year otherwise. A k tuned for last year’s system is a guess about this year’s.

Top-k is a small number with a large effect. Tune it like any other engineering parameter: with a test set, a metric and a willingness to accept that the optimum is smaller than you expected. The smallest k that retrieves what your answers need is the right k — and the money and milliseconds you save are yours to spend elsewhere.

Latest articles

Scroll to Top