What This Article Tries to Answer
While studying LLMs, the word entropy appears repeatedly: cross-entropy loss, perplexity, temperature, hallucination detection, and more. These may look like separate concepts, but they all point to the same mathematical intuition.
This article connects those scattered ideas into one line of reasoning: entropy is a unifying language for understanding LLM behavior.
Entropy in One Sentence
Entropy is a measure of uncertainty.
Flip a fair coin and the outcome is completely uncertain, so entropy is maximal at 1 bit. If both sides of the coin are heads, the outcome is certain and entropy is 0.
Shannon’s 1948 formula is:
$$ H(X) = -\sum p(x) \log p(x) $$The more uniform, and therefore uncertain, a probability distribution is, the higher its entropy. The more concentrated and certain it is, the lower its entropy.
A note about the Chinese character 熵: Chinese physicists created this phono-semantic character during the Republican era to translate thermodynamic entropy: 火 refers to thermodynamics, while 商 provides the pronunciation shāng. Information theory borrows the same mathematical form, but information entropy is not a physical metaphor. The two concepts share a formula, not a physical mechanism.
Training: Cross-Entropy Loss
The fundamental objective of LLM training is simple: given a context, predict the probability distribution of the next token and make that prediction as close as possible to the true distribution.
The measure of that closeness is cross-entropy.
An Intuitive Example
Suppose the true next token is “cat” and the model assigns the following probabilities:
| Token | Model probability | Cross-entropy contribution |
|---|---|---|
| cat | 0.7 | $-\log(0.7) \approx 0.36$ |
| cat | 0.01 | $-\log(0.01) \approx 6.64$ |
The important properties are:
- The more accurate the prediction, and the higher the correct token’s probability, the lower the cross-entropy.
- The more unreasonable the prediction, the higher the cross-entropy, with a logarithmic penalty.
- Assigning the correct answer a probability of 0.01 is punished far more severely than assigning it 0.1.
That is exactly the training signal we want: strongly penalize predictions that are completely wrong.
Loss on a Training Curve
When you see an LLM training-loss curve, its vertical axis usually represents cross-entropy loss. A falling curve means that the model is becoming more accurate at predicting the next token and that uncertainty in its predicted distribution is decreasing.
Evaluation: Perplexity
Perplexity is the exponential form of cross-entropy:
$$ PPL = 2^{H} $$The intuition is that perplexity measures how many tokens the model is, on average, “hesitating” among.
- In the GPT-2 era, perplexity was roughly 20–30, meaning the model was uncertain among about 20–30 candidate tokens at each step.
- Modern large models have significantly lower perplexity, reflecting stronger predictive ability.
Perplexity is a core measure of a language model’s foundational capability. It measures whether prediction is accurate, however, not whether an answer is useful. That is why evaluation also needs metrics such as BLEU and ROUGE, as well as newer approaches such as LLM-as-Judge.
Inference: Temperature and Sampling
After training, a model outputs a token-level probability distribution. How we choose a token from that distribution directly determines the entropy of the output.
Temperature
Temperature scales the logits, the model’s raw scores, and therefore controls the shape of the probability distribution.
- T → 0: approaches argmax; almost all probability concentrates on the highest-scoring token, output entropy approaches 0, and generation becomes deterministic.
- T = 1: uses the model’s original distribution.
- T > 1: flattens the distribution, reducing differences between token probabilities and increasing randomness and output entropy.
Top-p, or Nucleus Sampling
Top-p controls entropy from another direction by truncating cumulative probability. For example, top_p=0.9 retains only the smallest token set whose cumulative probability reaches 90%, discarding the long tail. This effectively places an upper bound on output entropy.
Practical Choices
| Scenario | Temperature | Reason |
|---|---|---|
| Code generation | Low (0–0.2) | Determinism matters and the correct answer is often narrow |
| Creative writing | High (0.7–1.0) | Diversity and surprise are desirable |
| RAG question answering | Low (0–0.3) | The response should remain faithful to retrieved documents |
| Data extraction | 0 | Structured output must be deterministic |
Context and Conditional Entropy
This section is especially important for RAG engineers.
From an information-theory perspective, injecting information into the context window is an attempt to reduce the conditional entropy of the model’s future-token predictions.
$$ H(Y|X) \leq H(Y) $$Conditional entropy—the uncertainty about Y when X is known—is never greater than unconditional entropy. The more complete and relevant the information in X, the lower the conditional entropy and the more certain the output.
This provides an information-theoretic explanation for why RAG can improve answer quality:
- A user asks a question, leaving the model with high uncertainty and high entropy.
- The system retrieves relevant documents and injects them into the context, reducing conditional entropy.
- The model generates from sufficient context, producing a more certain and accurate answer.
Conversely, irrelevant or contradictory retrieved documents may add noise rather than reduce uncertainty. This is why retrieval quality is so important in a RAG system.
Hallucination Detection: Entropy as a Signal
A practical observation is that when token-level entropy suddenly rises during generation—an entropy spike—the model may be starting to fabricate information.
On material that the model has seen and can predict confidently, the distribution tends to be sharp and low-entropy. Where the model lacks evidence and has to invent, the distribution becomes flatter and higher-entropy.
Some hallucination-detection methods use this property:
- Monitor token-level log probabilities.
- Calculate entropy over a sliding window.
- Mark positions where entropy exceeds a threshold as potential hallucinations.
APIs can expose token log probabilities, making this approach implementable at the product layer. Combined with observability tools such as Langfuse, entropy-based signals can support quality monitoring of LLM output in production.
Entropy’s Hidden Influence in Prompt Engineering
We rarely calculate entropy directly while writing prompts, but prompt design fundamentally changes the entropy of the output distribution.
- More explicit instructions → smaller output space → lower entropy → more stable and controllable output
- Open-ended prompts → larger output space → higher entropy → more diversity but less control
- Few-shot examples → constrain the output pattern → lower entropy
- Structured-output requirements such as JSON Schema or XML tags → greatly compress the output space → substantially lower entropy
This also explains why a good prompt is usually concrete and constrained. It helps the model narrow its search space and reduces uncertainty in the decisions it must make.
“Entropy” in a Codebase: A Useful Metaphor
Entropy also appears in AI engineering as a metaphor rather than a mathematical definition.
In Harness Engineering, OpenAI describes how a codebase accumulates inconsistent patterns, style drift, and technical debt when Codex agents generate code autonomously. The article calls this degradation entropy and describes a garbage-collection mechanism in which agents periodically scan for deviations and open refactoring pull requests.
Here, entropy borrows the thermodynamic metaphor that a system naturally moves toward disorder without external intervention. It is not a strict mathematical use, but it captures an important engineering insight: autonomous systems require continuous governance.
Summary
| Stage | Role of entropy | Practical meaning |
|---|---|---|
| Training | Cross-entropy loss | Minimize the gap between predicted and true distributions |
| Evaluation | Perplexity ($2^H$) | Measure foundational predictive capability |
| Inference | Temperature / Top-p | Control randomness and diversity |
| Context | Conditional entropy $H(Y \mid X)$ | Injecting relevant documents lowers conditional entropy and improves RAG output |
| Product | Entropy-spike detection | Signal potential hallucinations and output-quality problems |
| Prompt | Output-space constraints | A good prompt reduces decision uncertainty |
| Engineering | Metaphor for code degradation | Autonomous systems need garbage collection to resist disorder |
In one sentence: LLM training minimizes cross-entropy, inference uses temperature to control output entropy, evaluation uses perplexity to measure quality, and products can use entropy signals to detect hallucinations. Entropy is a unifying language across the LLM lifecycle.