# 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.

Canonical: https://promptlyfound.com/glossary/top-k
Updated: 2026-12-01

## Short answer

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.

## Key findings

- Anthropic found that passing the top 20 chunks to the model was more effective than passing the top 10 or the top 5. [1]
- 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%. [1]
- OpenAI's Retrieval API returns at most 10 results by default. Developers can raise the limit to 50 with max_num_results. [2]
- 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. [3]

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](https://promptlyfound.com/glossary/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](https://learn.microsoft.com/en-us/azure/search/vector-search-ranking) says the query parameter k sets how many nearest neighbors a vector search returns.[5] [OpenAI's retrieval guide](https://developers.openai.com/api/docs/guides/retrieval) returns at most 10 results by default and up to 50 with max_num_results.[2] The [Gemini API reference](https://ai.google.dev/api/generate-content) 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](https://www.anthropic.com/news/contextual-retrieval) shows a two-stage setup. The first retrieval took the top 150 chunks. A [reranking](https://promptlyfound.com/glossary/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)?](https://promptlyfound.com/ai-search/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](https://arxiv.org/abs/1904.09751) 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](https://promptlyfound.com/glossary/temperature), which controls how random the choice among those tokens is.

## Sources

1. Introducing Contextual Retrieval. Anthropic, 2024-09-19. https://www.anthropic.com/news/contextual-retrieval
2. Retrieval. OpenAI API documentation. https://developers.openai.com/api/docs/guides/retrieval
3. Generating content. Gemini API reference. https://ai.google.dev/api/generate-content
4. The Curious Case of Neural Text Degeneration. arXiv, 2019-04-22. https://arxiv.org/abs/1904.09751
5. Relevance in vector search. Microsoft Learn. https://learn.microsoft.com/en-us/azure/search/vector-search-ranking

---

This is a plain-text copy of https://promptlyfound.com/glossary/top-k from Promptly Found. The web page is the canonical version and includes the charts.
