Skip to content
Ask Sera Search
Ask SeraAnswers from our guides, research and news Search
Learn
Guides
Research
Sectors
Platforms
News
Tools
About

What is top-k in retrieval and text generation?

Top-k is the number of highest-ranked results a retrieval step keeps. In text generation, top-k sampling picks only from the k most likely next tokens.

In short. Top-k is the number of highest-ranked results that a retrieval step keeps, such as the top 20 passages. Only those results go on to the language model, so a passage ranked below the cutoff is not read. In text generation, top-k has a second meaning: the model picks the next token only from the k most likely tokens.

On this page 6 sections

The evidence

  • Observed:

    Anthropic found that passing the top 20 chunks to the model was more effective than passing the top 10 or the top 5.1Anthropic, Sep 19, 2024

  • Tested:

    In Anthropic's tests, reranking the top 150 retrieved chunks down to 20, on top of contextual embeddings and contextual BM25, cut the top-20 retrieval failure rate by 67%, from 5.7% to 1.9%.1Anthropic, Sep 19, 2024

  • Confirmed:

    OpenAI's Retrieval API returns at most 10 results by default. Developers can raise the limit to 50 with max_num_results.2OpenAI API documentation. Checked Dec 1, 2026

  • Confirmed:

    Google's Gemini API uses topK in both senses: the number of chunks that its file search tool retrieves, and the number of most probable tokens that sampling considers.3Gemini API reference. Checked Dec 1, 2026

Confirmed by the platformTestedObserved in data

Top-k is a cutoff. In retrieval, it sets how many results move on to the next step. In text generation, it sets how many candidate tokens the model can choose from.

#What does top-k mean in retrieval?

In retrieval, top-k is the number of highest-ranked results that a search step keeps. A retrieval system scores many candidates, sorts them, and passes only the first k on.

Retrieval products expose k as a setting. Microsoft's page on relevance in Azure AI Search vector search says the query parameter k sets how many nearest neighbors a vector search returns.5 OpenAI's retrieval guide returns at most 10 results by default and up to 50 with max_num_results.2 The Gemini API reference describes topK for its file search tool as the number of semantic retrieval chunks to retrieve.3

#How does top-k affect which pages AI answers use?

Only the top k results reach the model, so the cutoff decides which passages can shape an answer. Anthropic's post on Contextual Retrieval shows a two-stage setup. The first retrieval took the top 150 chunks. A reranking model then scored those chunks and kept the top 20 for the prompt.1

Anthropic also measured the effect of k. It found that passing the top 20 chunks to the model was more effective than passing the top 10 or the top 5. Its main metric counted the relevant documents that failed to appear within the top 20 chunks.1 A passage at rank 21 in that setup is not in the prompt, however relevant it is. What is retrieval-augmented generation (RAG)? covers the rest of the pipeline.

#What is top-k sampling in text generation?

In text generation, top-k sampling limits the model's choice of the next token to the k most probable tokens. The paper The Curious Case of Neural Text Degeneration describes it as a popular sampling method. At each step, the model samples from the top k tokens according to their relative probabilities. The paper found that a constant k is not ideal across contexts, and it proposed nucleus sampling, which cuts off by total probability instead.4

The Gemini API reference describes the generation setting topK as the maximum number of tokens to consider when sampling. It says this field is no longer supported and is silently ignored on Gemini 3.6 Flash and newer models.3 Top-k works alongside temperature, which controls how random the choice among those tokens is.

Read next

Related terms

Questions

What is a good top-k value for RAG?

There is no single right value. Anthropic's tests found that the top 20 chunks worked better than the top 10 or the top 5. OpenAI's Retrieval API defaults to 10 results.

What is the difference between top-k and top-p sampling?

Top-k sampling keeps a fixed number of the most likely tokens. Top-p, or nucleus, sampling keeps the smallest set of tokens whose probabilities add up to p, so the number of tokens changes from step to step.

Is top-k the same as the k in reciprocal rank fusion?

No. Microsoft notes that the constant k in reciprocal rank fusion is separate from the k that sets how many nearest neighbors a vector query returns.

About this page

Researched and reviewed by Promptly Found. Reviewed Dec 1, 2026. Next review due Mar 2027.

Notes

  1. 1
    Introducing Contextual RetrievalAnthropic · Sep 19, 2024 · Checked Dec 1, 2026 · Primary source
  2. 2
    RetrievalOpenAI API documentation · Checked Dec 1, 2026 · Primary source
  3. 3
    Generating contentGemini API reference · Checked Dec 1, 2026 · Primary source
  4. 4
    The Curious Case of Neural Text DegenerationarXiv · Apr 22, 2019 · Checked Dec 1, 2026 · Paper
  5. 5
    Relevance in vector searchMicrosoft Learn · Checked Dec 1, 2026 · Primary source
Cite this page
Citation style

Promptly Found. (2026, December 1). What is top-k in retrieval and text generation? https://promptlyfound.com/glossary/top-k

Revisions 1
First published.