The Settings Nobody Explains
When I first started poking around the OpenAI API, I noticed these sliders — temperature, top-p, top-k — and completely ignored them. They sounded like audio engineering terms. I figured the defaults were fine and moved on.
Then I started noticing things. Sometimes the AI would give me nearly identical answers no matter how I rephrased my prompt. Other times it would go completely off-script and get weird and creative when I just wanted a straight answer. Eventually I realized these sampling parameters were at the root of both problems — and once I understood what they actually did, I could fix both situations in about ten seconds.
This isn't going to be a statistics lecture. I'll explain what each parameter does in plain English, show you when to touch them, and tell you honestly when the default is already the right answer.
What "Sampling" Even Means
Before we get to the parameters, a quick mental model. When an AI generates text, it doesn't just pick the single "most likely" next word every time. It builds a probability distribution — basically a ranked list of candidates for the next token, each with a score. Then it samples from that list.
Sampling means it rolls weighted dice. The top candidate might have a 40% chance of being picked, the second might have a 25% chance, and so on. The parameters we're about to cover all control how that weighted dice roll works — how wide the pool of candidates is, and how extreme the weighting gets.
That's it. Everything else is details.
Temperature: The Most Important Knob
Temperature is the big one. It controls how "confident" the sampling feels. A low temperature makes the model more decisive — it heavily favors the top candidates. A high temperature flattens the probabilities out so even lower-ranked tokens get a real shot.
Think of it like this: at temperature 0, the model always picks the most probable next token. It becomes deterministic — you'll get the same output every time for the same input. At temperature 2.0, it's throwing darts at a much wider board. The output gets creative, surprising, and sometimes incoherent.
// Low temperature — precise, consistent, predictable
{
"model": "gpt-4o",
"temperature": 0.2,
"messages": [{ "role": "user", "content": "Extract the dates from this invoice" }]
}
// High temperature — creative, varied, sometimes surprising
{
"model": "gpt-4o",
"temperature": 1.2,
"messages": [{ "role": "user", "content": "Write me five tagline ideas for a dog grooming startup" }]
}The practical rule I use: lower temperature for tasks where there's a right answer, higher for tasks where variety and creativity are the point. Extracting structured data from a document? Go low — maybe 0.0 to 0.3. Brainstorming five completely different marketing angles? Go higher — 0.9 to 1.2.
The Default Is Usually 1.0
Most APIs default temperature to 1.0, which is a solid middle ground. ChatGPT in the browser uses something similar. If you're just prompting normally without API access, the temperature is already tuned reasonably well — you mostly only need to think about this when you're building something or calling the API directly.
Top-P: Trimming the Long Tail
Top-P (also called nucleus sampling) works differently from temperature. Instead of reshaping the probability curve, it limits how many candidates are even considered for sampling.
Here's how it works: the model ranks all possible next tokens by probability. Top-P tells it to only consider the smallest group of candidates whose probabilities add up to P. So if top-p is 0.9, the model takes the top tokens until their combined probability reaches 90%, then ignores everything below that cutoff.
The effect: at top-p 1.0, every token is on the table (no filtering). At top-p 0.1, only the very top handful of candidates survive — very focused output. At top-p 0.9, you're cutting off the weird long-tail stuff while still allowing reasonable variety.
// Top-P 0.1 — very focused, only near-certain tokens survive
{
"temperature": 1.0,
"top_p": 0.1
}
// Top-P 0.95 — wide pool, only extreme outliers cut
{
"temperature": 1.0,
"top_p": 0.95
}I find top-p most useful when I want creative output that doesn't go completely off the rails. Setting top-p to around 0.85–0.92 while keeping temperature up gives you variety without the model inventing words or going logically sideways.
Top-K: The Simpler Cousin
Top-K is the blunter version of top-p. Instead of a probability threshold, it just hard-caps the candidate pool at K tokens. Top-K of 40 means only the top 40 candidates are ever considered, regardless of their actual probabilities.
You'll see top-k more in local model interfaces like Ollama and LM Studio than in OpenAI's API (which doesn't expose it directly). It's a useful knob when you want predictable behavior without fussing over probability math.
# Ollama Modelfile example — lower top_k for focused responses
FROM llama3
PARAMETER temperature 0.7
PARAMETER top_k 20
PARAMETER top_p 0.9A good rule of thumb: top-k between 20–50 is reasonable for most use cases. Below 10 and you risk very repetitive output. Above 100 and it's basically not doing anything.
Temperature vs Top-P: Use One, Not Both
Here's something that tripped me up for a while: temperature and top-p both control randomness, but they do it in different ways, and using both at extreme values simultaneously creates unpredictable interactions.
The OpenAI docs actually say this directly: alter temperature or top-p, not both. Most practitioners I've seen follow this rule in practice.
My Personal Default Setup
For most API work I do, I fix top-p at 1.0 and only touch temperature. It's simpler to reason about and easier to tune. I only reach for top-p when I'm specifically trying to filter out weird long-tail outputs while keeping temperature elevated for creativity.
When to Actually Change These Settings
Let me give you concrete scenarios rather than abstract advice:
Leave defaults alone when: You're using the chat interface (ChatGPT, Claude.ai, Gemini). The platforms already tune this reasonably. You're not building anything — you're just prompting. The defaults are a good starting point and honestly cover 80% of use cases.
Turn temperature down (0.0–0.3) when: You're extracting structured data. You're generating code that needs to be syntactically correct. You're doing classification tasks. You want consistent output across repeated runs. You're building something where reliability matters more than creativity.
Turn temperature up (0.9–1.3) when: You're brainstorming and want genuinely different options. You're generating creative writing and the first draft feels too flat. You're running the same prompt multiple times and getting outputs that feel too samey.
Adjust top-p (down to 0.85–0.92) when: High temperature is giving you incoherent output. The model is going off-script but you still want some variety. You're working with a model that tends toward verbose or wandering outputs.
// Preset: Data extraction / code generation
{"temperature": 0.1, "top_p": 1.0}
// Preset: Balanced Q&A / summarization
{"temperature": 0.7, "top_p": 1.0}
// Preset: Creative brainstorming
{"temperature": 1.1, "top_p": 1.0}
// Preset: Creative but coherent
{"temperature": 1.1, "top_p": 0.9}One Thing That Surprised Me
When I first learned about temperature 0 making the model deterministic, I assumed that meant it would give exactly the same answer every single time. Not quite. Infrastructure randomness (batching, floating point differences across GPUs) means you might still see small variations even at temperature 0.0. It's more consistent, not perfectly reproducible across different server calls.
If you truly need identical outputs for testing purposes, you also need to set a fixed seed parameter (available in OpenAI's API). Temperature 0 plus a fixed seed gets you much closer to true reproducibility.
The Short Version
Temperature controls how bold the sampling is — low is precise, high is creative. Top-p limits the candidate pool by probability mass — lower cuts off weird long-tail tokens. Top-k does the same thing but with a hard count instead of a probability threshold.
For most prompt work through a chat interface, you never need to touch any of these. For API work, keep a few presets handy based on task type and adjust from there. And when in doubt, nudge temperature first — it has the most noticeable effect and is the easiest to reason about.
The defaults exist for a reason. They're not laziness — they're calibrated for the most common use cases. Learning when to override them is the skill. And now you've got enough to do that without just guessing.
More tutorials in this category, or explore the full field guide.