Sviluppo Software

LLM Non-Determinism: Probability, Creativity, and the Control Parameters (Temperature, Top-K, Top-P)

Published on 11 October 2025

LLM Non-Determinism: Probability, Creativity, and the Control Parameters (Temperature, Top-K, Top-P)

When I started delving into the first concepts of Artificial Intelligence, one of the discoveries that fascinated and, in a way, bewildered me the most was their intrinsically non-deterministic process. By asking the same question at two different times to a Large Language Model (LLM), you get conceptually similar answers, but expressed with different words and nuances. For an engineer accustomed to systems where one input corresponds to a single, predictable output, this behavior might seem almost an imperfection. Instead, it is the key to their extraordinary effectiveness.

The Limits of Pure Logic

This immersion in the world of probability awakened university memories in me, related to signal theory. At the time, those complex formulas seemed like abstract exercises, almost a rite of passage in the engineering curriculum. I never thought I would find them, conceptually, in the analysis of such an advanced tool as an LLM.

The question naturally arises: why not build an AI based exclusively on strict logic, the kind that governs the game of chess or the writing of computer code? The answer is simple: because the reality we try to describe and interact with is only minimally a purely logical system. Logic can perfectly model the rules of a game, but it fails in describing the complexities of economics, the dynamics of a relationship, the nuances of psychology, or even the entire structure of quantum physics. Human language itself is a vehicle for ambiguity, creativity, and emotions, elements that a deterministic system would struggle to manage.

The Wisdom Control Room

This is where LLMs come into play. Their operation is not based on rigid rules, but on an extremely sophisticated probabilistic calculation. In simple terms, their primary task is to predict the most probable next word in a given sequence of text.

Relying on this mechanism, however, is not like relying on rolling two dice. It is more similar to listening to the advice of an old sage. The enormous training process an LLM is based on is the equivalent of a life experience that condenses entire libraries and countless human conversations. Therefore, its prediction is not a random gamble, but a considered suggestion.

To manage the 'tone' of this wisdom, a real control room comes into play with three fundamental knobs: Temperature, Top-K, and Top-P.

  • Temperature: Imagine it as the regulator of 'creative risk'. A low temperature (close to 0) makes the model more conservative and deterministic, making it choose the words with the highest probability. A high temperature encourages the model to consider even less obvious options, increasing the creativity and 'surprise' in the generated text.
  • Top-K: This knob defines the number of options the model can choose from. With K=1, the model is forced to take the single most probable word. With K=50, the model creates a basket with the 50 most probable words and chooses from there (also influenced by the temperature), increasing variety.
  • Top-P (Nucleus Sampling): This is a more dynamic and intelligent approach. Instead of a fixed number of words (as in Top-K), the model selects a group of words whose summed probability reaches a certain threshold P (e.g., 0.95). This means the number of options adapts to the context: if the model is very confident about the next word, the group will be small; if there are many valid options, the group will be larger.

These parameters work synergistically. For a technical analysis, a low temperature with restrictive Top-K/Top-P will be used. For poetry, all sliders will be raised to give the 'sage' maximum expressive freedom.

Experiment with AI Creativity

Understanding how these parameters can transform an LLM from a rigorous analyst to a whimsical poet is crucial. To make this concept even more tangible, I have developed an interactive console that allows you to see in real-time how an AI's response changes as these three parameters vary.

The invitation is to experiment, to see with your own eyes how probability becomes the brush with which AI paints its responses.

Try the Interactive Console →

Embracing this calculated 'imperfection' is the first step toward fully understanding and exploiting the potential of these extraordinary tools.

#Intelligenza Artificiale #LLM #Generative AI #Non Determinismo #Temperatura LLM #Nucleus Sampling #Ingegneria Software
Ing. Marco Maretto

Written by

Ing. Marco Maretto

Systems Engineer. Designing architectures that not only use AI, but survive its errors.