AI chat assistants are now part of everyday life, and many people wonder how they “know” what to write. There is no magic: behind every answer sits a huge amount of arithmetic, and the core ideas are the same ones you meet in high school and early college. Conditional probability, exponential functions, logarithms and scientific notation. This article uses the topic as an excuse to review exactly those four tools, with small numbers made up for the example.
The core idea: predicting the next word
A language model takes a piece of text and answers one precise question: how likely is each word in the vocabulary to come next? After “the sky is”, words like “blue” or “cloudy” should get high probability, and “screwdriver” should get almost none.
Mathematically this is a conditional probability: the probability that the next word is given the preceding context, written . For it to be a valid distribution, every value must lie between 0 and 1 and the values over all possible words must sum to exactly 1.
Softmax: from arbitrary scores to probabilities
Internally the model produces a score for each word, any real number, possibly negative or larger than 1. To turn scores into probabilities it applies softmax:
The exponential function does the heavy lifting. Since is always positive, no probability comes out negative. Since it is increasing, a higher score still means a higher probability. It also amplifies gaps: a score just 1 higher becomes about 2.718 times larger, because . Dividing by the total is the same normalization you use to turn counts into percentages.
Worked example: three candidate words
Suppose a toy vocabulary with three candidates after “the sky is”: “blue” with score 2, “cloudy” with score 1 and “screwdriver” with score 0.
- Exponentiate each score, using e ≈ 2.718.
- Add them up to get the denominator.
- Divide each exponential by the sum. For "blue":
- Repeat for the other two words.
- Check: the three probabilities must add to 1 (any tiny gap is rounding).
So roughly 66.5% for “blue”, 24.5% for “cloudy” and 9% for “screwdriver”. A score of 0 did not become probability 0, because . That is why real models give absurd words a small but never exactly zero probability.
The probability of a whole sentence
Generating a sentence repeats the process word by word. By the chain rule of probability, the chance of the full sequence is the product of each step’s chance, conditioned on everything before it:
Suppose a four-word sentence where the model gave 0.5, 0.4, 0.2 and 0.1 at each step. The sentence probability is , or 0.4%. Now picture a 50-word answer where every step has probability 0.1: the product is . Computers store numbers with limited precision, and values that small can round to zero, a problem called underflow.
Why logarithms come in
The fix is to work with log-probabilities, using . Taking logs of the chain rule turns the product into a sum:
- Take the log of each factor.
- Add them.
- Undo the log by raising 10 to the result and compare with the direct product.
Both routes give 0.004. For the 50-word case, instead of storing we store just . Because log is increasing, the sentence with the highest log-probability is also the most probable one. Real models usually use the natural log, but the logic is the same in any base.
Orders of magnitude and scientific notation
Model sizes are described with phrases like “billions of parameters”. Read them with scientific notation: a billion is , a trillion is . The order of magnitude is roughly the integer part of the base-10 log. If a hypothetical model A has parameters and model B has , B is two orders of magnitude larger, meaning 100 times larger, not “twice as large”.
Common mistakes
- Thinking softmax is just dividing by the sum. Without the exponential, negative scores would give negative “probabilities”.
- Adding probabilities of consecutive steps. Sequential events combine by multiplication.
- Forgetting that log-probabilities are negative. Probabilities are at most 1, so their logs are at most 0; closer to 0 means more likely.
- Mixing up orders of magnitude. Going from to is 1,000 times more, not 3 times.
- Treating high probability as certainty. 66.5% still leaves 33.5% for other words.
Frequently asked questions
Does softmax always return values between 0 and 1?
Yes. Each term is positive and divided by a sum that contains it, so with more than one word it lies strictly between 0 and 1, and all of them add up to 1.
Why base e for the exponential?
Any base above 1 would work. Base e is preferred because its derivative is itself, which keeps training calculations simple.
Does taking logs change which sentence is most likely?
No. The logarithm is increasing, so it preserves order.
Do I need calculus to follow this?
Not for what is shown here. Conditional probability and the rules of exponents and logarithms are enough.