Introduction
Large Language Models (LLMs) like GPT, LLaMA, and Ollama generate text by predicting the next token from probabilities. Controlling these probabilities allows developers to influence creativity, randomness, length, and coherence of outputs. This article explains the key parameters and provides example code to experiment with them.
How LLMs Generate Text
An LLM takes a prompt and produces a probability distribution over the next token:
P(token ∣ previous tokens)
The model then samples from this distribution to generate the next word. Parameters such as temperature, top-k, and top-p shape this distribution, controlling the output style.
1. Temperature – "Randomness / Creativity"
What it does: Controls how likely the model is to pick fewer probable tokens.
Low temperature: model picks the safest, most likely word.
High temperature: model may choose creative or unusual words.
temperature = 0Deterministic (greedy)temperature = 1Creativetemperature > 1Very creative but risky
Example:
Prompt: "The cat sat on the _______"
| Temperature | Output Example |
|---|---|
| 0.0 | "mat." |
| 0.5 | "sofa." |
| 1.0 | "keyboard." |
| 1.5 | "spaceship." |
2. Top-k – "Limit Choices"
What it does: Only allows the model to consider the top k most probable tokens at each step.
Effect: Reduces the chance of weird, very low-probability words.
It Provent unlikely words.
More control than temperature alone.
Top-K
5- very safe
40- Balanced
100- More creative
Example:
Prompt: "I went to the __________"
| Top-k | Output Example |
|---|---|
| 5 | "store" |
| 50 | "store", "park", "library" |
| 100 | "store", "park", "library", "zoo", "museum" |
3. Top-p / Nucleus Sampling– "Cumulative Probability"
What it does: Instead of fixed
k, consider only tokens whose cumulative probability ≤ p.Effect: More adaptive than top-k.
Top-P
0.5- Very focused
0.9- Balanced
0.95- Creative
Example:
Prompt: "She loves eating _____________"
| Top-p | Output Example |
|---|---|
| 0.5 | "pizza" |
| 0.8 | "pizza", "ice cream" |
| 0.95 | "pizza", "ice cream", "sushi", "cake" |
4. Max Tokens / Max Length
What it does: Limits the number of tokens generated.
Effect: Controls output length.
Prevents long running output.
Example:
Prompt: "Explain gravity in one sentence"
| Max Tokens | Output Example |
|---|---|
| 5 | "Gravity pulls things." |
| 15 | "Gravity is the force that pulls objects toward Earth." |
| 30 | "Gravity is the universal force that attracts all objects with mass towards each other, keeping planets in orbit." |


Join the conversation! Your thoughts help the community grow.