How Does the Final Hidden State Become the Next Token?
How a transformer turns its last hidden state into scores, chances and one chosen token, explained simply for your AI interview.
In short
The last hidden state is a vector for the newest position. A linear layer turns it into one score per possible next token, softmax turns those scores into chances, and the model picks one token, adds it to the text and repeats.
9 min readIntermediateCommon · estimateFacts checked 26 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
At the end of a transformer, each position gets a vector called the hidden state. A linear layer turns that vector into one score for every token in the vocabulary. Softmax turns the scores into chances that add up to one. The model then picks a token, either the most likely one or a random draw, adds it to the text, and runs again.
Think of the same claims handler from earlier, typing a reply. Their notes list the likely next words, each with a rough chance, and they pick one, read the sentence again and repeat. A transformer ends the same way. The newest position's hidden state holds everything the model has worked out so far, and the last steps turn it into one word.
First, a linear layer gives one raw score for every token in the vocabulary. Softmax then turns those scores into chances that add up to one. In a bank's support bot that has written 'Your refund will be', made-up scores might give 'processed' about 67 percent, 'in' about 24 percent and 'delayed' about 9 percent. The 2017 transformer paper describes this same final step in one sentence.
Then the model picks. Greedy decoding always takes the most likely token, which is repeatable but can repeat itself on long replies. Sampling draws a token by its chance. That adds variety, and also the risk of an odd wording. The chosen token joins the text, and the whole model runs again for the next one.
For a bank, I would keep the pick steady for policy answers and never let the model's wording be the only check on a fee. The amount should come from the rate sheet in code, and the model only explains it.
What is the last step inside a transformer, in plain words?
Google's machine learning course calls LLMs, the large language models behind AI chat tools, essentially autocomplete mechanisms that predict tokens. A transformer is the neural network design behind modern language models, and it is where the hidden state comes from. A token is a piece of text the model reads and writes: a whole word, or part of one.
After the layers have read your text, each position holds a hidden state: a vector of numbers that carries what the model has worked out there. The model can't write that vector. It has to become scores, then chances, then one token.
Think of a claims handler at an insurer typing a reply. Their notes list likely next words with a rough confidence for each. They choose one, read the sentence again and repeat. A bank's support bot writing 'Your refund will be' works the same way, with its notes coming from the hidden state.
Key words
- Hidden state
- The vector of numbers a transformer makes for one position in the text.
- Vocabulary
- The fixed list of tokens the model can write, from word pieces to whole words.
- Score
- A raw number for one token. Higher means the model leans to it more. It is not a chance yet.
- Softmax
- A step that turns scores into chances, all positive and adding up to one.
How does the last hidden state become one score per token?
The vocabulary is the list of tokens the model can write. The final linear layer has one row for each token. Multiplying the hidden state with each row gives one score per token, and a higher score means the model leans more to that token.
From the last hidden state to one token
Hidden state
one vector for the newest position
Linear layer
a row for each vocabulary token
Scores to chances
Raw scores
one per token, not yet chances
Softmax
chances that add up to 1
Pick a token
the top one, or a draw by chance
Add it, repeat
the model runs again on the longer text
- 1The last hidden state goes into one linear layer.
- 2The layer gives one raw score per vocabulary token.
- 3Softmax turns the raw scores into chances that add up to 1.
- 4The model picks one token: the top one, or a random draw.
- 5The chosen token is added to the text.
- 6The whole model runs again for the next token.
The layer is a plain weighted sum for each token. The 2017 paper that introduced the transformer describes this final step in one sentence:
“We also use the usual learned linear transformation and softmax function to convert the decoder output to predicted next-token probabilities.”
Vaswani et al., Attention Is All You Need (2017)
The same paper shared one weight matrix between its two embedding layers and this final layer. Its English-German setup had a shared vocabulary of about 37,000 tokens, so its final layer made about 37,000 scores at each step, one per token.
How does softmax turn scores into probabilities?
Softmax does two jobs. It raises the constant e (about 2.72) to the power of each score, so every value is positive and a higher score grows much faster. Then it divides each value by the total, so the results add up to one.
Here are made-up scores for the next word after 'Your refund will be'. The code runs softmax, prints the chances and picks the top one.
import math
# Made-up scores a model gives the next word after "Your refund will be"
scores = {"processed": 2.0, "in": 1.0, "delayed": 0.0}
def softmax(scores):
# Turn raw scores into probabilities that add up to 1.
exps = {word: math.exp(s) for word, s in scores.items()}
total = sum(exps.values())
return {word: e / total for word, e in exps.items()}
probs = softmax(scores)
for word, p in sorted(probs.items(), key=lambda kv: -kv[1]):
print(f"{word:<10} {p:.2f}")
print("sum", round(sum(probs.values()), 2))
best = max(probs, key=probs.get)
print("greedy picks:", best)Output
processed 0.67 in 0.24 delayed 0.09 sum 1.0 greedy picks: processed
That is why 'processed' gets about 2.7 times the chance of 'in'.
How is the next token picked?
Greedy decoding takes the top token every time. Sampling draws one at random, weighted by its chance, so a less likely token can win. Hugging Face's docs describe both:
“It selects the next most likely token at each step.”
Hugging Face, Transformers docs
“Sampling, or multinomial sampling, randomly selects a token based on the probability distribution over the entire model's vocabulary (as opposed to the most likely token, as in greedy search).”
Hugging Face, Transformers docs
Greedy vs sampling on the same chances
Same chances
processed 0.67, in 0.24, delayed 0.09
Greedy
Take the top token
always the highest chance
Sampling
Draw by chance
delayed about 9 times in 100
Same wording
repeatable, but can repeat itself
Wording varies
a different path each run
- 1Greedy looks at the same chances and takes the top one.
- 2Sampling draws a token by its chance, so a less likely one can win.
- 3Same input and setup, same path, so the wording repeats.
- 4Each run can take a different path, so the wording varies.
Greedy is steady, but it breaks down on longer outputs because it begins to repeat itself. Two settings change only this pick step. Temperature makes the chances sharper or flatter before the draw. Top-p limits the draw to the most likely tokens, whose chances add up to a set share.
How would you explain two different answers to the same question?
Ask the bot twice with sampling on, and the two wordings can differ. Each word was a draw from the chances, so a different draw takes a different path. Greedy decoding, with the same input and setup, follows the same path each time.
Tip
Say that the pick is one step, and fix it at the right layer. For policy wording, use greedy or a low temperature. For a fee, take the number from the rate sheet in code, not from the model.
What does the interviewer want to hear?
A strong answer covers
- The last hidden state is a vector for one position, not a word.
- One linear layer gives one score per vocabulary token.
- Softmax turns the scores into chances that add up to one.
- The model picks one token, greedy or by sampling, then repeats.
- Temperature and top-p change only the pick step.
- Facts like fees come from code and data, not from the picked word.
Common traps
Trap"The hidden state is already the next word."
BetterIt is a vector. It becomes scores, then chances, then one token.
Trap"Softmax picks the best word."
BetterSoftmax only turns scores into chances. The pick step, greedy or sampling, chooses.
Trap"Temperature changes what the model knows."
BetterIt reshapes the chances the pick step draws from.
Can you spot the wrong step?
Spot the step
A refund bot's code turns its raw scores into chances that add up to 1. Which line does that?
Quick check
A bank's FAQ bot must word the same answer the same way every time for one question. Which pick method suits that best?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
Learn it in depth
Last updated on 26 November 2026
Building a support bot that has to give steady answers?
I design and build AI products and agents, including the interface and the system behind them. Book a short call and tell me what your answers must get right.