Module 3 of 10
Chapter 03 • Foundations & Mechanics

Decoding Hyperparameters: Temperature, Top-P & Penalties

A prompt is only half of the generation equation. How the model translates raw logits into actual output tokens is dictated by sampling hyperparameters. In this chapter, understand the probability mathematics behind Temperature,Top-P (Nucleus Sampling), and repetition penalties to achieve rock-solid reproducibility in production.

1. Temperature Scaling: The Softmax Modifier

Before generating a token, the neural network computes unbounded real scores (logits $z_i$) for every word in its vocabulary. To convert logits into a probability distribution, PyTorch and inference engines apply Softmax scaled by Temperature (T):

P(x_i) = exp( z_i / T ) / Σ_j exp( z_j / T )
T = 0.0 (Greedy / Argmax)

Collapses the distribution so the single token with the highest logit receives probability 1.0. Completely deterministic. Mandatory for SQL generation, code, math, and JSON extraction.

T = 0.7 - 0.9 (Balanced)

Preserves the natural shape of the learned distribution while allowing slight variation between runs. Ideal for general conversational agents, copywriting, and customer support.

T > 1.3 (High Entropy)

Flattens the distribution toward a uniform random pick. Low-probability tokens become frequent, resulting in hallucinations, syntactical breakdown, and incoherent output.

2. Top-P (Nucleus Sampling) vs. Top-K Truncation

Even at low temperatures, a long tail of thousands of rare tokens can cause erratic slips. Sampling cutoff algorithms dynamically prune this tail:

Top-P (Nucleus Sampling)

Sorts tokens by probability and keeps only the top candidates whose cumulative sum reaches threshold p (e.g. 0.90).

Dynamic Horizon: When the model is confident (one token has 85% probability), only 2 tokens are considered. When ambiguous, the candidate pool dynamically expands to 40 tokens.
Top-K Truncation

Strictly limits sampling to the top k highest-probability tokens regardless of cumulative mass (e.g. k = 40).

Static Horizon: Less adaptive than Top-P, but highly effective on open-source edge models (Ollama, llama.cpp) to prevent runaway gibberish.
OpenAI Golden Rule

OpenAI recommends altering either temperature OR top_p, but never tweaking both simultaneously. For deterministic business workflows, keep top_p = 1.0 and set temperature = 0.0.

3. Frequency Penalty vs. Presence Penalty

When generating long documents, models can fall into repetitive feedback loops where reciting a phrase increases the probability of reciting it again:

ParameterFormula ModificationPrimary EffectRecommended Setting
frequency_penaltylogit -= count * penaltyPenalizes words proportionally to how many times they already appeared in the text. Prevents repetitive lists.0.2 - 0.5 (Max 2.0)
presence_penaltylogit -= (1 if count > 0 else 0) * penaltyOne-time flat penalty if the word has appeared at least once. Encourages introducing fresh topics and diverse vocabulary.0.1 - 0.4 (Max 2.0)

4. Production Hyperparameter Recipes

Application DomainTemperatureTop-PFrequency PenaltySeed
JSON Extraction / Pydantic0.01.00.042 (Strictly reproducible)
Code Generation & Bug Fixing0.10.950.01337
RAG Enterprise Q&A0.21.00.1None
Creative Storytelling / Brainstorming0.850.900.4None
Next Chapter

Chain-of-Thought & Tree of Thoughts

Continue to the next hands-on prompt engineering guide in this series.

Continue Learning →

Explore Related Tools

Hand-picked utilities and calculators related to this tool.

Related Tools