What are Log Probabilities (Logprobs) in LLMs?
- Authors
- Name
- Amit Shekhar
- Published on
Log Probabilities (Logprobs) in LLMs are the log of the probability (the chance) that the model gives to each token it can write next. They tell us how sure the LLM was about every token it picked.
In this blog, we will learn about Log Probabilities (Logprobs) in LLMs. We will also see how an LLM gives a probability to every next token, what a log is in plain words, why we use logprobs instead of plain probabilities, how to get the probability of a full sentence by adding logprobs, what top logprobs are, and where logprobs are used in the real world.
We will cover the following:
- How an LLM picks the next token
- What is a log?
- What are Log Probabilities (Logprobs)?
- Why do we use logprobs?
- Probability of a full sentence
- Top logprobs
- Where are logprobs used?
I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.
I teach AI and Machine Learning at Outcome School.
Let's get started.
How an LLM picks the next token
An LLM (Large Language Model) writes text one token at a time. A token is a small piece of text. It can be a full word like "cat" or a part of a word.
At every step, the LLM looks at the text so far and predicts which token comes next.
Let's say the text so far is "My pet is a". For the sake of understanding, let's assume that the LLM knows only three tokens: "cat", "dog", and "car".
The LLM does not pick one token directly. First, it gives a raw score to every token it knows. These raw scores are called logits. A higher logit means the model prefers that token more.
Then, a function called softmax turns these logits into probabilities. A probability is a number between 0 and 1 that tells us the chance of something. All the probabilities add up to 1.
| Token | Logit | Probability |
|---|---|---|
| cat | 3.64 | 0.70 |
| dog | 2.61 | 0.25 |
| car | 1.00 | 0.05 |
Here, we can see that the LLM gives a 70 percent chance to "cat", a 25 percent chance to "dog", and only a 5 percent chance to "car". The three probabilities add up to 1.
A real LLM knows tens of thousands of tokens or more, so it gives a probability to every one of them at every step.
We have a detailed blog on Top-k and Top-p Sampling that explains how the LLM picks one token from these probabilities.
Now, before we take the next step, we must understand what a log is.
What is a log?
A log answers one question: "How many times do we need to multiply a fixed number to reach this number?"
Let's take an example with the number 10.
- 10 * 10 = 100, so the log of 100 (with base 10) is 2.
- 10 * 10 * 10 = 1000, so the log of 1000 (with base 10) is 3.
The answer can also be a fraction or a negative number. For example, the log of 0.1 (with base 10) is -1, because 0.1 = 1 / 10.
The fixed number is called the base. In LLMs, we use the natural log, written as ln. Its base is a special number called e, which is about 2.718. We do not need to calculate it by hand. Every programming language does it for us.
We need to know three facts about the log for this blog:
- The log of 1 is 0.
- The log of any number between 0 and 1 is a negative number. The smaller the number, the more negative its log.
- The log turns multiplication into addition: log(a * b) = log(a) + log(b)
We will use the third fact very soon.
What are Log Probabilities (Logprobs)?
A Log Probability (Logprob) is the natural log of a probability.
logprob = ln(probability)
We take the probability that the LLM gave to a token, and we take its natural log.
Let's see some probabilities and their logprobs:
| Probability | Logprob | Meaning |
|---|---|---|
| 1.0 | 0 | Completely sure |
| 0.9 | -0.11 | Very sure |
| 0.5 | -0.69 | 50-50 chance |
| 0.25 | -1.39 | Not very sure |
| 0.1 | -2.30 | Unsure |
| 0.01 | -4.61 | Very unsure |
| 0.001 | -6.91 | Almost no chance |
Here, we can notice a few things:
- A logprob is always 0 or a negative number. It is never positive, because a probability is never more than 1.
- A logprob close to 0 means the model is very sure.
- A more negative logprob means the model is less sure.
Now, let's see the full flow from a token to its logprob as a diagram:
┌─────────────────────────────────┐
│ Text so far: "My pet is a" │
└─────────────────────────────────┘
↓
┌─────────────────────────────────┐
│ Logits │
│ cat 3.64, dog 2.61, car 1.00 │
└─────────────────────────────────┘
↓
softmax
↓
┌─────────────────────────────────┐
│ Probabilities │
│ cat 0.70, dog 0.25, car 0.05 │
└─────────────────────────────────┘
↓
natural log
↓
┌─────────────────────────────────┐
│ Logprobs │
│ cat -0.36, dog -1.39, car -3.00 │
└─────────────────────────────────┘
Here, we can see that the logits go through softmax to become probabilities, and the probabilities go through the natural log to become logprobs.
Let's see the code for the same:
import numpy as np
logits = np.array([3.64, 2.61, 1.00]) # scores for "cat", "dog", "car"
probs = np.exp(logits) / np.exp(logits).sum() # softmax
logprobs = np.log(probs) # natural log
print("probs: ", probs.round(2))
print("logprobs:", logprobs.round(2))
It will print the following:
probs: [0.7 0.25 0.05]
logprobs: [-0.36 -1.39 -3. ]
Here, we have used np.exp and a division to apply softmax, and then np.log to take the natural log. np.log is the natural log, not the log with base 10.
Planning your move into AI?
No matter which tech domain you work in today, with your effort, you can transition to these roles:
- AI Engineer
- Forward Deployed Engineer
- AI Agent Engineer
- Harness Engineer
- Voice AI Engineer
- AI Architect
- LLM Engineer
- LLM Inference Engineer
- Gen AI Engineer
- MLOps Engineer
- ML Engineer
- Deep Learning Engineer
Our AI and Machine Learning Program at Outcome School takes you there.
Now, the question is: why do we not just use the probabilities directly? Let's understand this.
Why do we use logprobs?
An LLM writes many tokens, one after another. To get the chance of the whole text, we need to multiply the probabilities of all the tokens. This is because the chance of one thing happening and then another thing happening is found by multiplying their chances.
The problem is that probabilities are numbers smaller than 1. When we multiply many small numbers, the result becomes tiny very fast.
- 0.1 * 0.1 = 0.01
- 0.1 * 0.1 * 0.1 = 0.001
- After 400 tokens: 0.1 multiplied 400 times is a decimal point, then 399 zeros, and then a 1.
A computer stores every number in a limited space. A normal Python float cannot hold a number much smaller than about 10 to the power of -308. Anything far smaller becomes 0. This problem is called underflow.
Here comes the log into the picture. Since the log turns multiplication into addition, we can add the logprobs instead of multiplying the probabilities. Adding negative numbers just gives a more negative number. It never becomes 0 by mistake.
Let's see this with an example:
import math
prob = 1.0
logprob = 0.0
for _ in range(400):
prob = prob * 0.1 # multiply probabilities
logprob = logprob + math.log(0.1) # add logprobs
print(prob)
print(round(logprob, 2))
It will print the following:
0.0
-921.03
Here, we can see that multiplying the probabilities gave us 0.0, which is wrong. The true answer is not zero, it is just very small. But adding the logprobs gave us -921.03, which is correct and easy to store.
This is why LLMs work with logprobs during training and in their outputs. We have a detailed blog on Cross-Entropy Loss that covers how the training loss is built from the log of a probability.
A quick note for you
No matter which tech domain you work in, get familiar with these topics:
- LLM
- RAG
- MCP
- Agent
- Fine-tuning
- Quantization
We put it all together in one video:
AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization
No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.
Now, let's get back to the topic.
Probability of a full sentence
Now, let's use logprobs to find how likely a full sentence is.
Let's say the prompt is "I love", and the LLM writes "to read books". The LLM gives the following probabilities:
| Token | Probability | Logprob |
|---|---|---|
| to | 0.5 | -0.69 |
| read | 0.2 | -1.61 |
| books | 0.6 | -0.51 |
Using plain probabilities, we multiply:
0.5 * 0.2 * 0.6 = 0.06
Using logprobs, we add:
-0.69 + (-1.61) + (-0.51) = -2.81
Both tell us the same thing. If we take e to the power of -2.81, we get back 0.06.
Let's see this as a diagram:
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ "to" │ │ "read" │ │ "books" │
│ prob 0.5 │ │ prob 0.2 │ │ prob 0.6 │
│ logprob -0.69 │ │ logprob -1.61 │ │ logprob -0.51 │
└───────────────┘ └───────────────┘ └───────────────┘
│ │ │
└───────────────────┼───────────────────┘
↓
add all the logprobs
↓
┌───────────────────────────┐
│ Sentence logprob = -2.81 │
│ e^(-2.81) = 0.06 │
└───────────────────────────┘
Here, we can see that each token has its own logprob, and we add them together to get the logprob of the full sentence.
This is how we find the probability of a full text without ever running into underflow.
To master Tokenization, Logits, and Cross-Entropy, and to build a Large Language Model (LLM) from scratch, check out our AI and Machine Learning Program at Outcome School.
Top logprobs
Till now, we have looked only at the token the LLM picked. But at every step, the LLM also gave probabilities to all the other tokens. We can use these other choices too.
Top logprobs are the logprobs of the few most likely tokens at a step, not just the picked one.
Let's go back to "My pet is a". The LLM picked "cat". The top 3 logprobs at this step are:
| Token | Logprob |
|---|---|
| cat | -0.36 |
| dog | -1.39 |
| car | -3.00 |
Here, we can see that "dog" was a strong second choice, and "car" was a weak third choice.
Many LLM APIs let us ask for these values. Usually, there are two options:
logprobs: set it totrueto get the logprob of every token the LLM writes.top_logprobs: set it to a number like3to also get the top 3 choices at every step.
A response for one token then looks like below:
{
"token": "cat",
"logprob": -0.36,
"top_logprobs": [
{ "token": "cat", "logprob": -0.36 },
{ "token": "dog", "logprob": -1.39 },
{ "token": "car", "logprob": -3.0 }
]
}
Here, we get the picked token, its logprob, and the top 3 choices with their logprobs.
Now, let's see where we use all this.
Where are logprobs used?
1. Confidence estimation
The logprob tells us how sure the LLM was. To turn a logprob back into a probability, we take e to the power of the logprob.
probability = e^(logprob)
For example, a logprob of -0.11 means a probability of 0.9, which is 90 percent sure. A logprob of -2.30 means a probability of 0.1, which is only 10 percent sure.
For a long answer, we can look at the average logprob of all its tokens. Based on this, we can show the answer to the user when the LLM is sure, and ask a human to check it when the LLM is unsure.
2. Classification with LLMs
Let's say we want to use an LLM to tell if a review is positive or negative. We ask the LLM to answer with only one word: "Positive" or "Negative".
Now, we read the top 2 logprobs of the first token:
| Token | Logprob | Probability |
|---|---|---|
| Positive | -0.05 | 0.95 |
| Negative | -3.00 | 0.05 |
Here, we get not just the label but also a score. The LLM is 95 percent sure that the review is positive. We can set a threshold, for example "accept only if the probability is more than 0.8", and send the rest for review.
3. Perplexity
Perplexity is a number that tells us how surprised a model is by a piece of text. Lower perplexity means the model found the text more natural.
We calculate it from the logprobs:
perplexity = e^(-(average logprob))
Let's take our sentence "to read books" again. The logprobs are -0.69, -1.61, and -0.51.
Step 1: We add the logprobs: -0.69 + (-1.61) + (-0.51) = -2.81.
Step 2: We find the average by dividing by the number of tokens: -2.81 / 3 = about -0.94.
Step 3: We flip the sign to get 0.94.
Step 4: We take e to the power of 0.94, which gives about 2.56.
So, the perplexity is about 2.56. Means, on average, the model was as unsure as if it was choosing between about 2.56 equally likely tokens at each step. If the model were completely sure of every token, the perplexity would be 1.
We use perplexity to compare language models on the same text. The model with lower perplexity predicts the text better.
Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.
4. Detecting hallucination risk
A hallucination is when an LLM writes something that sounds right but is wrong.
Let's say the LLM writes "The Eiffel Tower was built in 1889." If the token "1889" has a logprob of -0.02, the LLM was very sure. If it has a logprob of -2.5, the LLM was unsure, and the top logprobs show other years close behind.
A very low logprob on an important token, like a name, a date, or a number, is a warning sign. It does not prove that the answer is wrong, and a sure answer can still be wrong. But it helps us find the parts of the answer that we must check.
5. Reinforcement Learning training
In Reinforcement Learning (RL), the LLM learns from rewards. A good answer gets a positive reward and a bad answer gets a negative reward.
Policy gradient methods train the LLM in the following way. For every answer, we take the logprobs of its tokens and push them in a direction based on the reward:
- If the reward is positive, we increase the logprobs of the tokens in that answer, so the LLM writes such answers more often.
- If the reward is negative, we decrease the logprobs of those tokens, so the LLM writes such answers less often.
Let's see the case of a positive reward as a diagram:
┌─────────────────────────────────┐
│ LLM writes an answer │
│ "to read books" │
└─────────────────────────────────┘
↓
┌─────────────────────────────────┐
│ The answer gets a reward │
│ reward = +1 (good answer) │
└─────────────────────────────────┘
↓
┌─────────────────────────────────┐
│ Training increases the logprobs │
│ of "to", "read", and "books" │
└─────────────────────────────────┘
↓
┌─────────────────────────────────┐
│ Next time, the LLM writes │
│ such an answer more often │
└─────────────────────────────────┘
Here, we can see that a positive reward pushes up the logprobs of the tokens in the answer. A negative reward pushes them down in the same way.
In a simple form, the training tries to make this number as large as possible:
objective = reward * (sum of logprobs of the answer tokens)
Logprobs are also used to check how far the trained LLM has moved away from the original LLM, by comparing the logprobs of the two models on the same tokens. This keeps the training from changing the LLM too much.
Now we must have understood what Log Probabilities (Logprobs) in LLMs are, why we use them, and where they are used.
Frequently Asked Questions
Can a logprob be positive?
No. A logprob is always 0 or a negative number. This is because a probability is never more than 1, and the log of any number between 0 and 1 is negative. A logprob of exactly 0 means a probability of 1, which means the model was completely sure.
How do we convert a logprob back into a probability?
We take e to the power of the logprob. For example, a logprob of -0.11 gives a probability of 0.9, which is 90 percent sure. A logprob of -2.30 gives a probability of 0.1, which is only 10 percent sure.
What is the difference between logits and logprobs?
Logits are the raw scores that the LLM gives to every token it knows, and a higher logit means the model prefers that token more. Logprobs come after two steps: softmax turns the logits into probabilities that add up to 1, and then the natural log turns those probabilities into logprobs.
Does a high logprob mean the answer is correct?
No. A high logprob only means the LLM was sure about that token, and a sure answer can still be wrong. In the same way, a very low logprob on an important token, like a name, a date, or a number, does not prove the answer is wrong. It is a warning sign that helps us find the parts we must check.
That's it for now.
Thanks
Amit Shekhar
Founder @ Outcome School
You can connect with me on:
Follow Outcome School on:
