★ FOUR BRAND-NEW COURSES ANNOUNCED! ★ Machine Learning End To End  •  Hands-On Deep Learning with Python  •  Generative AI & LLMs with Python  •  AI Agents and Agentic AI with Python  ★  Open the “Educational” menu above to explore them ★  ★  NEW! 5. JOB ASSISTANCE: JD Report • Resume customization • AI Mock Interviews — FREE with course bundles, or just ₹499 / US$5. Single Payment. No Subscription. ★★ FOUR BRAND-NEW COURSES ANNOUNCED! ★ Machine Learning End To End  •  Hands-On Deep Learning with Python  •  Generative AI & LLMs with Python  •  AI Agents and Agentic AI with Python  ★  Open the “Educational” menu above to explore them ★  ★  NEW! 5. JOB ASSISTANCE: JD Report • Resume customization • AI Mock Interviews — FREE with course bundles, or just ₹499 / US$5. Single Payment. No Subscription. ★★ FOUR BRAND-NEW COURSES ANNOUNCED! ★ Machine Learning End To End  •  Hands-On Deep Learning with Python  •  Generative AI & LLMs with Python  •  AI Agents and Agentic AI with Python  ★  Open the “Educational” menu above to explore them ★  ★  NEW! 5. JOB ASSISTANCE: JD Report • Resume customization • AI Mock Interviews — FREE with course bundles, or just ₹499 / US$5. Single Payment. No Subscription. ★★ FOUR BRAND-NEW COURSES ANNOUNCED! ★ Machine Learning End To End  •  Hands-On Deep Learning with Python  •  Generative AI & LLMs with Python  •  AI Agents and Agentic AI with Python  ★  Open the “Educational” menu above to explore them ★  ★  NEW! 5. JOB ASSISTANCE: JD Report • Resume customization • AI Mock Interviews — FREE with course bundles, or just ₹499 / US$5. Single Payment. No Subscription. ★

The one job of a language model: predict the next token

The one job of a language model: predict the next token
12-10-2026  |  By Tejas Desai, Ecube Analytics  |  Generative AI: Tokenization & decoding (Chapter 2)  |  3 min read
READ 4TH OF 8 IN THIS SERIES

The one job of a language model: predict the next token

Tokenization & decoding · Part 4 of 8

Strip away the hype and a large language model has exactly one job: look at the text so far and predict the next token. Everything it can do is built on that single skill, trained on plain running text with no labels at all.

Free labels from ordinary text

Take any sentence and shift it by one token. The text so far is the input; the next token is the answer. No human has to annotate anything, because the text carries its own answers. That is why pretraining can use a huge amount of text.

At every position the model produces a probability for every token in the vocabulary. The loss rewards putting a high probability on the token that really came next:

loss = − Σt log P( xt | x< t )

The next-token objectiveThree examples of an input token, the true next token, the probability the model gave it, and the resulting loss.Training: at every position, predict the token that actually comes nextinputThelanguage modeltrue next tokencatp = 0.50 loss = ?ln p = 0.69inputcatlanguage modeltrue next tokensatp = 0.25 loss = ?ln p = 1.39inputsatlanguage modeltrue next tokenonp = 0.10 loss = ?ln p = 2.30average loss = 1.46 ? perplexity = e^1.46 = 4.3
Three positions of a training example. The orange bar is the probability the model gave the true next token; the loss is ?ln of it.

Teacher forcing

During training the model is always shown the true previous tokens, not its own earlier guesses. This is called teacher forcing. It makes training stable and parallel, but it means the model is never trained on its own mistakes. That is one reason a low training loss does not by itself guarantee good generated text.

Perplexity: the effective number of choices

Average the loss over many tokens and raise e to that power, and you get perplexity. Read it as “the model is about as unsure as if it were choosing evenly among this many tokens.” A perplexity of 20 is like picking at random from 20 equally likely options. Smaller is better, and it is handy for comparing models. It does not measure whether the output is useful, which is why other kinds of evaluation exist.

import math

p_true = [0.50, 0.25, 0.10]      # probability the model gave to the real next token
loss = -sum(math.log(p) for p in p_true) / len(p_true)
print(round(loss, 3), round(math.exp(loss), 2))     # 1.461 4.31 : loss and perplexity

Three guesses of 0.5, 0.25 and 0.1 give an average loss of 1.46, a perplexity of 4.3. If the model had spread its probability evenly over 20 tokens every time, the perplexity would be exactly 20.

Takeaways

  • Pretraining predicts the next token from the text so far; the text supplies its own labels.
  • The loss is the negative log of the probability given to the true token.
  • Teacher forcing trains on true history, so training loss is not the same as generation quality.
  • Perplexity is e raised to the average loss: the effective number of equally likely choices.

Next in the series (Part 5): A language model made of nothing but counts

This blog is from chapter 2, course-2-generative-ai of 4 world-class courses on aibits.ecubeanalytics.com
← Previous Article 4 of 8 in this series Next →