Recommended resources
The freely available resources this lesson will direct you to.
Introduction
Welcome to the third lesson in our module on interacting with LLMs. In our last two lessons, you learned how to make API calls and how to use prompt engineering techniques like zero-shot, few-shot, and chain-of-thought to guide the content of an LLM's response.
Today, we move from controlling what the model says to controlling how it says it. This lesson focuses on the various parameters that influence the text generation process itself. At its heart, an LLM is a probabilistic machine that predicts the next word (or token) based on the input it has seen. By adjusting certain parameters, you can steer this prediction process to make the model's output more deterministic and factual, or more creative and surprising.
Our goal is to explain the effect of temperature and other sampling parameters on model output. By the end of this lesson, you'll understand the trade-offs involved and be able to tune these parameters to suit your specific application, whether you're building a fact-checking bot or a creative story generator.
1. The Probabilistic Nature of Text Generation
Before we dive into the parameters, let's briefly recap how text generation works. When an LLM needs to generate the next token in a sequence, it doesn't just pick one "correct" word. Instead, it performs two main steps:
- Prediction: It calculates a probability score for every single token in its vocabulary. These raw scores are called logits.
- Sampling: It uses these probabilities to select the next token.
The parameters we'll discuss today are all about manipulating this sampling step.
The simplest sampling method is greedy sampling, where the model always chooses the token with the highest probability. This makes the output completely deterministic—the same input will always produce the same output. However, it often leads to repetitive and uninteresting text. To get more varied and natural-sounding responses, we use more nuanced sampling strategies.
2. The Core Sampling Trio: Temperature, Top-K, and Top-P
Three parameters are fundamental to controlling the randomness and diversity of an LLM's output: temperature, top_k, and top_p.
To build a strong conceptual foundation, let's watch a video from the 'Under The Hood' channel. It provides excellent visual explanations of how these three parameters work together to shape the model's output.
Watch the entire video (up to 09:10). It will walk you through: How LLMs use a probability distribution to sample the next token. Greedy sampling as a baseline. Temperature: How it 'sharpens' or 'flattens' the probability distribution. Top-K Sampling: How it limits the pool of candidate tokens to a fixed number. Top-P (Nucleus) Sampling: How it creates a dynamic pool of candidates based on a cumulative probability threshold. How all three are typically applied in sequence during generation.
Let's break down and reinforce the key concepts from the video.
Temperature
Temperature is perhaps the most common parameter for controlling creativity. It modifies the probability distribution by scaling the logits before the softmax function is applied.
- Low Temperature (e.g., < 1.0, approaching 0): This "sharpens" the distribution, making high-probability tokens even more likely and low-probability tokens effectively impossible to select. A temperature of
0.0is equivalent to greedy sampling. This is ideal for tasks requiring factual accuracy and consistency, like summarization or question-answering. - High Temperature (e.g., > 1.0): This "flattens" the distribution, increasing the chance that less likely tokens are chosen. This boosts randomness and creativity, which is useful for brainstorming, story writing, or creating diverse options.

Top-K and Top-P (Nucleus) Sampling
While temperature adjusts the shape of the entire probability distribution, top_k and top_p control which tokens are even considered for sampling by filtering them.
- Top-K: This is a simple but effective method. You set an integer
k(e.g., 50), and the model will only sample from the topkmost probable tokens. This prevents the model from picking a completely nonsensical token that might have a tiny but non-zero probability. - Top-P (Nucleus Sampling): This is a more dynamic approach. You set a cumulative probability
p(e.g., 0.9), and the model selects the smallest set of most probable tokens whose cumulative probability is greater than or equal top. The size of this set can change at each step, making it adaptive. If a few tokens are highly probable, the set will be small. If many tokens have similar probabilities, the set will be larger.
Practical Guideline: Temperature vs. Top-P
As you might have gathered, temperature and top_p both serve a similar purpose: controlling the trade-off between determinism and randomness. A common best practice, recommended by API providers like OpenAI, is to alter either temperature or top-p, but not both simultaneously. For most applications, tuning one of these is sufficient to achieve the desired output behavior.
3. Other Important Generation Parameters
Beyond the core trio, several other parameters give you finer-grained control. As a software engineer, you'll find these essential for managing costs, latency, and output structure.
LLM Parameters Explained: A Practical Guide with Examples for OpenAI API in Python
The article 'LLM Parameters Explained' from Learn Prompting provides a fantastic, practical guide with Python examples using the OpenAI API. We'll use it to explore some additional key parameters.
Read the sections titled: 'What Is the Max Tokens Parameter in LLMs?' 'What Is the Frequency Penalty Parameter in LLMs?' 'What Is the Presence Penalty Parameter in LLMs?' 'What Is the Stop Sequence Parameter in LLMs?' Focus on understanding what each parameter does and when you might use it.
Let's summarize these additional parameters:
max_tokens: An integer that sets the maximum number of tokens the model can generate in its response. This is a critical parameter for controlling API costs and ensuring your application receives a response of a manageable length. Be aware that if the model's natural response is longer thanmax_tokens, the output will be abruptly cut off.frequency_penalty: A number between -2.0 and 2.0. Positive values penalize tokens based on how frequently they've already appeared in the generated text. This helps reduce word-level repetition (e.g., "The plan is a good plan because the plan is well-planned.").presence_penalty: Similar tofrequency_penalty, but it applies a one-time penalty to any token that has appeared at all, regardless of frequency. This encourages the model to introduce new topics and concepts rather than circling back to ones it has already mentioned.stop: A string or list of strings that, when generated by the model, will cause the generation to halt immediately. This is extremely useful for creating structured conversations or ensuring the model doesn't generate extra, unwanted text after completing its task. For example, in a dialogue system, you could use"User:"as a stop sequence.
4. Putting It All Together: A Code Example
Let's modify the code from our previous lesson to see how these parameters are used in an actual API call. We'll ask for a creative, short story and tune the parameters accordingly.
import os
from openai import OpenAI
client = OpenAI() # Assumes OPENAI_API_KEY is set as an environment variable
# A creative prompt for a short story
prompt = "Write a very short story (2-3 sentences) about a programmer who discovers their cat has been secretly debugging their code at night."
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": prompt}
],
temperature=0.8, # Higher for creativity
top_p=1.0, # Not modifying top_p since we are using temperature
max_tokens=100, # Limit the length to keep it brief
frequency_penalty=0.5, # Slightly discourage repetitive phrases
presence_penalty=0.2, # Slightly encourage new concepts
stop=None # No specific stop sequence needed here
)
print(response.choices[0].message.content)
Experiment on Your Own:
Try running the code above with different parameter settings:
- Set
temperatureto0.1. How does the story change? It will likely become more generic and less imaginative. - Set
frequency_penaltyto2.0. Does the vocabulary become more diverse? - Set
max_tokensto15. Observe how the story is cut off mid-sentence.
5. Heuristics for Parameter Tuning
Choosing the right parameters can feel like a dark art, but there are some excellent starting points based on your use case.
The Galileo AI blog provides a helpful decision tree and baseline values.
Parameter Optimization Decision Tree
Excerpt from "Essential LLM Parameters Every AI Team Needs | Galileo" (Section: Parameter optimization decision tree)
Here is a summary of their recommendations for different tasks:
| Use Case | Temperature | Top-P | Purpose |
|---|---|---|---|
| Factual Q&A / Data Extraction | 0.0 - 0.3 | 0.3 - 0.7 | Prioritize accuracy and consistency. Low randomness is key. |
| Creative Content / Brainstorming | 0.7 - 1.0 | 0.9 - 1.0 | Encourage diverse and novel ideas. High randomness is beneficial. |
| Summarization / Dialogue | 0.5 - 0.8 | 0.8 - 0.9 | A balance between factual grounding and natural, engaging language. |
These are just starting points. The best way to find the optimal settings for your application is to identify your goal, set initial values based on these heuristics, and iterate by testing and reviewing the output.
Conclusion
You have now gained a crucial understanding of how to control the LLM generation process. While prompt engineering shapes the model's task and context, sampling parameters fine-tune its "personality"—from a rigid fact-teller to a freewheeling creative.
Key Takeaways:
- LLM text generation is a probabilistic sampling process. We can influence this process with parameters.
- Temperature and Top-P are the primary levers for controlling the balance between deterministic, factual output and creative, random output. It's best to tune only one of them at a time.
- Top-K offers a simpler way to filter out low-probability tokens.
- Parameters like
max_tokens,frequency_penalty,presence_penalty, andstopprovide practical controls for managing output length, repetition, and structure. - The optimal parameter settings are highly dependent on the specific task. Start with established heuristics and iterate based on the results.
Preview of the Next Lesson:
We've explored how to structure the user prompt and how to tune the generation parameters. In the next lesson, we will focus on the system prompt. You will learn how to design system prompts to control an LLM's overall behavior, personality, and rules of engagement for a specific task, effectively giving your AI a persistent character and instruction set.