liveBook · Manning

Top-p filtering

As explained in Build Python Web Apps with Streamlit

top_p is a language-model sampling parameter that limits token selection to the smallest set whose cumulative probability reaches a specified threshold.

A trivia game needs fresh questions, but an LLM may give similar answers to similar prompts. Top-p filtering is one way to adjust how predictable its responses are. The model writes one token at a time, assigning probabilities to possible next tokens. The top_p setting, from 0 to 1, limits selection using a cutoff based on their cumulative probability. section 9.6.1 section 9.6.2

Top_p and temperature both affect response variability, but they do different jobs: top_p limits the token options considered, while temperature changes the shape of their probability distribution. Neither setting by itself promises a unique question. section 9.6.1 section 9.6.2

Make room for different trivia questions

A question-generating call uses temperature=0.7 and top_p=0.9. It also supplies a changing seed and a list of questions already asked. The sampling settings adjust variability; the prompt gives the model more specific reasons to produce a different question. section 9.6.2

from openai import OpenAI

class Llm:
  ...

  def ask(self, user_message, sys_message=None, schema=None,
          temperature=None, top_p=None):
    messages = self.construct_messages(user_message, sys_message)

    llm_args = {'model': 'gpt-5.1', 'messages': messages}
    if temperature:
      llm_args['temperature'] = temperature
    if top_p:
      llm_args['top_p'] = top_p

    if schema:
      completion = self.client.beta.chat.completions.parse(
        response_format=schema,
        **llm_args
      )
      return completion.choices[0].message.parsed
    else:
      completion = self.client.chat.completions.create(**llm_args)
      return completion.choices[0].message.content

The Python wrapper accepts an optional top_p argument and passes it to the OpenAI call when supplied, so callers can choose a setting for each request. section 9.6.2

Check model support before exposing the setting. OpenAI reasoning models such as gpt-5-mini do not support explicitly configured top_p; gpt-5.1 also disallows it when reasoning mode is enabled. Keep the wider reliability problem separate: changing sampling cannot guarantee that a generated question is factual. section 9.6.2 section 9.6.1

How Build Python Web Apps with Streamlit covers it

How deep it goes
Defines the cumulative-probability cutoff and demonstrates an API argument, without developing sampling mathematics.
Examples
Shows a Python LLM wrapper and a trivia-game call using temperature=0.7 and top_p=0.9.
What it assumes
Introduces tokens and their probabilities in plain language before using top_p in a Python API call.
What you can do after
Apply it via OpenAI chat completions to adjust question variability when the selected model supports explicit top_p settings.
How it connects
Connects top-p filtering to token probabilities, temperature, prompt randomness, and hallucinations.

Also explained in

Compare how 12 books explain top-p filtering →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.