LESSON 45 Β· RETRIEVAL

Top-K

Learn how Top-K controls how many retrieval results a RAG system returns, why the value matters, and how to choose it without adding unnecessary noise.

25 min readβ€’Intermediateβ€’Generative AI

By the end of this lesson, you will be able to:

  • Explain what Top-K means in retrieval.
  • Understand how K changes recall, precision, noise, and context size.
  • Compare Top-1, Top-3, Top-5, and larger values.
  • Use metadata filters and Top-K together.
  • Build a simple Python Top-K ranking example.
  • Choose and evaluate K for a real RAG workload.
1

What Does Top-K Mean?

Top-K means keeping the best K results after the retriever ranks candidate chunks.

If a search produces 100 possible matches and K = 5, the system keeps the five highest-ranked candidates for the next stage.

Key idea:K controls the number of candidates returned, not the quality of those candidates. A larger K gives the system more chances to find useful evidence, but it can also add irrelevant context.
EXAMPLE 1Top-3

A support chatbot retrieves the three most relevant policy chunks before asking the LLM to answer.

EXAMPLE 2Top-8

A research assistant keeps eight candidates because one answer may require evidence spread across several sections.

2

Where Top-K Fits in Retrieval

Top-K is applied after candidates have been scored or ranked by the retrieval method.

User queryβ€œWhat is the refund period?”
β†’
Candidate searchCompare the query with stored chunks
β†’
RankOrder candidates by relevance
β†’
Top-KKeep the best K results

The selected results can then be filtered, reranked, compressed, or passed into the generation prompt depending on the system design.

3

Top-1, Top-3, Top-5, and Top-10

Different K values create different retrieval trade-offs.

TOP-1

One result

Very focused, but a single missed chunk can make the answer impossible to ground.

TOP-3

Three results

A common starting point for focused questions with limited supporting evidence.

TOP-5

Five results

More coverage for questions that may need evidence from several chunks.

TOP-10

Ten results

Higher candidate coverage, but more context can increase noise and cost.

There is no universal best K. The right value depends on the question type, chunk quality, corpus, retriever, context budget, and evaluation results.
4

Why Not Always Use a Large K?

It is tempting to retrieve many chunks so the correct answer is more likely to be present. However, retrieval is not simply β€œmore is better.”

Small K
  • Less noise
  • Smaller context
  • Lower latency and cost
  • Higher risk of missing evidence
Large K
  • More evidence coverage
  • More chances to find related facts
  • More context to process
  • Higher noise and token cost

A strong RAG system aims for enough evidenceβ€”not the maximum possible number of chunks.

5

Top-K and Recall

Recall asks whether the retrieval set contains the evidence needed for the query. Increasing K often improves recall because more candidates are included.

LOW KRelevant chunk at rank 7

If K is 3, the useful chunk is never passed to the next stage.

HIGHER KRelevant chunk at rank 7

If K is 10, the useful chunk becomes available, although some lower-ranked noise comes with it.

Important:Higher recall does not automatically mean a better final answer. The extra results still need to be relevant and manageable.
6

Top-K and Precision

Precision considers how much of the retrieved set is actually useful. As K grows, the system may include increasingly weak matches.

1Rank 1Highly relevant
2Rank 2Highly relevant
3Rank 3Useful context
10Rank 10May be weak or unrelated

Top-K therefore creates a practical balance between retrieving enough evidence and keeping the context focused.

7

Top-K with Metadata Filtering

Metadata filtering can reduce the candidate set before Top-K is selected.

WITHOUT FILTER

Search everything

Top-5 may contain chunks from several products, regions, or years.

β†’
WITH FILTER

Search allowed data

Filter first, then select Top-5 from the remaining candidates.

Example: For a 2026 policy question, filter year = 2026 and then retrieve the top five matching chunks. This can improve relevance without simply increasing K.
8

Simple Python Top-K Example

You can understand Top-K without a vector database by ranking already-scored candidates.

</> Python
results = [
    {"chunk": "Refunds are allowed within 30 days.", "score": 0.91},
    {"chunk": "Damaged items can be returned within 60 days.", "score": 0.88},
    {"chunk": "Shipping is free for orders over $100.", "score": 0.52},
    {"chunk": "Gift cards are valid for 12 months.", "score": 0.41},
    {"chunk": "Contact support for account changes.", "score": 0.34},
]

k = 3

top_k = sorted(
    results,
    key=lambda item: item["score"],
    reverse=True
)[:k]

for item in top_k:
    print(item["score"], item["chunk"])

Here, sorted(..., reverse=True) puts the highest scores first and [:k] keeps only the first K results.

9

Top-K Is Not the Same as a Threshold

Top-K always asks for a fixed number of results. A similarity threshold asks whether a result is good enough to keep.

TOP-K

β€œGive me the best five results.” Even if the fifth result is weak, it can still be returned.

THRESHOLD

β€œKeep results with a score of at least 0.75.” The number of results can vary.

Many production systems use both ideas: retrieve a manageable candidate set, then remove results that fail a relevance threshold or another quality rule.

10

Top-K and RAG Context

Every retrieved chunk consumes context. If chunks are large, even a modest K can produce a large prompt.

QueryUser question
β†’
Top-KSelect evidence
β†’
ContextCombine chunks
β†’
LLMGenerate answer
Practical rule:Think about K together with chunk size. Five 300-token chunks and five 2,000-token chunks are very different context budgets.
11

Choosing a Starting K

Start with a small, explainable value and measure it. Do not choose K only because another RAG tutorial uses it.

STARTTry K = 3 or 5

Use a practical baseline for focused retrieval tasks.

TESTCompare K values

Evaluate K = 1, 3, 5, 8, or another range that fits your workload.

MEASURECheck evidence recall

Ask whether the expected supporting chunks appear in the retrieved set.

CONTROLWatch context size

Track token usage, latency, and answer quality as K changes.

12

When K Should Be Larger

01Multi-part questions

The answer may require evidence from several different chunks.

02Distributed facts

Important information may be spread across separate sections of a document.

03Weak initial ranking

A larger candidate set can protect against imperfect retrieval ranking.

04High-recall workflows

Some downstream stages can rerank or filter a larger candidate pool.

13

When K Should Be Smaller

01Simple questions

A focused question may need only one or two strong pieces of evidence.

02Large chunks

Large chunks quickly consume the available context budget.

03High noise

If lower-ranked results are frequently unrelated, reducing K can improve focus.

04Strict latency

Smaller retrieval sets can reduce downstream processing and generation cost.

14

Common Top-K Mistakes

01Using a huge K

More results can overwhelm the context with weak matches.

02Always using K = 1

A single result can miss supporting evidence required for a complete answer.

03Ignoring chunk size

K and chunk size jointly determine how much evidence enters the prompt.

04Skipping evaluation

The right K must be tested against representative questions.

05Confusing score with truth

The highest similarity result is not automatically factually correct.

06Ignoring filters

Better candidate constraints can be more useful than simply increasing K.

15

Evaluate Top-K in a Real RAG System

Create a test set of realistic questions and define which chunks contain the expected evidence. Compare several K values using the same questions.

RECALL@KWas the expected evidence present within the top K?
RANKHow early did useful evidence appear?
CONTEXTHow much irrelevant content entered the prompt?
ANSWERDid changing K improve grounded answer quality?
Production mindset: choose K from measured retrieval and answer quality, while also respecting latency and token-cost constraints.
16

Practice: Compare Different K Values

Use the same ranked result list and test K = 1, 3, and 5. Identify which value retrieves all evidence needed for the answer while avoiding unnecessary chunks.

TASK

Find the useful cutoff

Write down the expected evidence for a question. Then compare the retrieved sets for different K values and explain your choice.

Quick Quiz

1. What does Top-K mean?

It means keeping the best K candidates after retrieval ranking.

2. What can happen when K is too small?

The system may miss relevant evidence needed to answer the question.

3. What can happen when K is too large?

More irrelevant context can be added, increasing noise, latency, and token cost.

4. Is Top-K the same as a similarity threshold?

No. Top-K fixes the number of candidates, while a threshold keeps results that meet a minimum relevance score.

30-Second Recap

1. Rank

Score and order candidate chunks.

2. Select

Keep the best K results.

3. Balance

Trade evidence coverage against noise and context cost.

4. Evaluate

Choose K using real retrieval and answer-quality tests.

Key Takeaways

  • Top-K controls how many ranked retrieval candidates are returned.
  • Increasing K can improve evidence recall but may also increase noise and context cost.
  • Top-K should be considered together with chunk size, filtering, ranking, and context limits.
  • A threshold and Top-K solve different problems and can be combined.
  • There is no universal best K; evaluate realistic questions and compare candidate values.
  • The best K is usually the smallest value that reliably provides sufficient evidence for the task.