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:
- Tested:
- 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
AI search
What is retrieval-augmented generation (RAG)?
RAG retrieves documents at query time and gives them to a language model to write a cited answer. How ChatGPT, Claude, Google, Bing and Perplexity use it.
AI search
How does AI search choose which sources to cite?
AI search retrieves pages from an index, reads passages and cites what it used. What OpenAI, Anthropic, Google, Microsoft and Perplexity document.
LLMs
How do large language models work?
LLMs predict text one token at a time from what fits in their context window. Tokens, context window sizes and the lost-in-the-middle effect.
AI search
What is semantic search and how does it differ from keyword search?
Semantic search matches the meaning of a query, often with vector embeddings. Keyword search matches its words. Hybrid search runs both and merges the results.
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
- 2
- 3
- 4
- 5
Cite this page
Promptly Found. (2026, December 1). What is top-k in retrieval and text generation? https://promptlyfound.com/glossary/top-k
Revisions 1
- First published.