Autoregressive generation
When you ask an LLM a question, it does not "look up" an answer. It converts your prompt into tokens, runs those tokens through a neural network, and produces a probability distribution over possible next tokens. It samples one token, appends it to the sequence, and repeats.
This loop is called autoregressive generation. The prefix auto means "self": the model uses its own previous outputs as inputs for the next step. A long answer is really a sequence of many small next-token decisions.
Key idea: a question-answering chatbot is a text-completion engine trained on dialogue-like data. The prompt steers it, but the mechanism is always "predict the next token."
Sampling strategies
At each step the model outputs logits: raw scores for every token. Those logits are converted to probabilities using the softmax function. The highest-probability token is called the greedy choice.
Greedy decoding often gives safe but repetitive text. To get variety, LLMs use sampling. Two common methods are:
- Top-k sampling: restrict choices to the k highest-probability tokens, then sample from those.
- Top-p (nucleus) sampling: choose the smallest set of tokens whose probabilities add up to p, then sample from that set.
Both methods trade off between coherence and creativity by deciding how many unlikely tokens are allowed into the pool.
Temperature and randomness
Temperature is a parameter that scales the logits before softmax. A low temperature makes the distribution sharper, so the highest-probability token almost always wins. A high temperature flattens the distribution, making unlikely tokens more likely.
At temperature 0, an LLM becomes deterministic and usually repeats itself. At very high temperatures, it becomes incoherent. A value around 0.7–1.0 is common for creative writing; 0.2–0.5 is common for tasks that need accuracy.
Prompts and context
The prompt is the only thing the model sees. It has no external memory, no common-sense engine, and no internet connection unless given a tool. That means the wording, examples, and context in the prompt strongly shape the output.
Prompt engineering is the practice of writing prompts that make the model more useful. This can include giving examples, specifying the format, asking for step-by-step reasoning, or instructing the model to say "I don't know" when uncertain.
Remember: the context window is finite. Long prompts and long outputs eventually push earlier context out of the model's view. That is why conversations can "forget" earlier instructions.
Sampling simulator
Below is a tiny example of how temperature changes token probabilities. Adjust the temperature and click Sample next token to see which word the model picks. Lower temperature is more predictable; higher temperature is more surprising.
Sampling strategies
Modern LLMs use tricks like top-k and nucleus (top-p) sampling to stay creative without producing nonsense. This Python example shows both.
Check yourself
Why does increasing temperature make an LLM more creative but also more likely to say something weird?
Your answer:
Sample answer: Higher temperature flattens the probability distribution over tokens, giving low-probability words more chance to be selected. That introduces variety and surprise, but if the temperature is too high the model may pick unlikely, incoherent tokens.
Quick quiz
1. What does "autoregressive" mean in an LLM?
2. What does temperature control?
3. Why is the prompt important?
Keep learning
You understand how LLMs answer questions one token at a time. Next, see how they can be turned into agents that plan and act.
Part of the advanced remake of the “Are LLMs Smarter Than a 5th Grader?” series.