How do temperature, top-p and other sampling parameters affect output?
722 views
34 of 83
1
The problem
Question presented to candidate:
"When you call an LLM API you can set temperature, top-p and a few other sampling knobs. What does each one actually do to the output, and how would you set them for, say, data extraction versus brainstorming?"
What a strong answer should cover:
The model outputs logits for every token; sampling parameters only change how the next token is chosen from that distribution, not what the model knows
Temperature divides the logits before softmax: below 1 sharpens toward the top token, above 1 flattens and gives rare (often junk) tokens more mass; 0 means greedy
Truncation methods: top-k keeps a fixed number of candidates, top-p (nucleus) keeps the smallest set whose probability adds up to p, min-p keeps tokens above a fraction of the top token probability; the order of truncation and temperature matters
Frequency and presence penalties (and logit bias) edit logits based on what was already generated or on explicit token IDs, to reduce repetition or ban tokens
Practical settings: low or zero temperature for extraction, classification and code; moderate for chat; higher plus truncation for creative work; change one knob at a time; temperature 0 is not fully deterministic; newer reasoning-style models may reject these parameters
Code / implementation expected: Optional โ implementing temperature, top-p and penalties on a small logits vector and showing their effect is a strong signal.
temperature
top-p
sampling
inference
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Developers who call LLM APIs and want to set sampling parameters on purpose rather than by copying defaults.
Difficulty: Medium
Verified with: Node 22.22.0 and Python 3.12.3 (the sampling pipeline in the example runs on a fixed logits vector; no real model is called). API parameter rules are cited inline with the source and the date checked (October 2026).
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the words an interviewer expects. All program output shown was produced by actually running the example; the Verified section quotes it in full.
1. Why This Even Matters โ A Story First
Picture a quiz show contestant who, for every question, privately ranks every possible answer by how likely it is to be right. The ranking is the contestant knowledge; nothing you say
The solution is hidden so you can try first
14 min read ยท 12 sections
4
Read the code
One logits vector through temperature, top-k, top-p, min-p and repetition penalties