AI Model Wiki

    Skip to main content
    Back to Glossary

    Top-p (Nucleus Sampling)

    Top-p (nucleus sampling) is a decoding parameter that restricts token selection to the smallest set of candidates whose cumulative Softmax probability sums to at least p. At each generation step, tokens are ranked by likelihood, and only the "nucleus"—those tokens filling the top-p probability mass—are eligible for sampling. Setting p=0.9 means the model draws exclusively from high-confidence tokens while still allowing some diversity; setting p=1.0 disables the filter entirely.

    Think of it like a restaurant's daily specials board: rather than listing every item on a hundred-page menu, the kitchen writes up only the dishes the chef considers worth ordering today. The more decisive the chef, the shorter the list—but the customer still gets to choose from what's written up.

    import anthropic
    client = anthropic.Anthropic()
    message = client.messages.create(
        model="claude-opus-4-5",
        max_tokens=256,
        top_p=0.95,
        messages=[{"role": "user", "content": "Write a one-sentence story."}]
    )
    print(message.content[0].text)
    

    Lowering p toward 0.8 tightens the nucleus and produces safer, more conservative text; raising it toward 1.0 unlocks rare tokens and increases creative risk.

    Top-p is typically set alongside Temperature—which reshapes the distribution before the nucleus is computed—and compared with Top-k Sampling, which uses a fixed candidate count rather than a probability threshold. Together they are the primary sampling controls exposed by LLM APIs.

    You'll encounter top-p in every major model API (OpenAI, Anthropic, Google) and in Prompt Engineering guides whenever the goal is to balance output diversity against factual reliability.

    AI Terminology
    Glossary