AI Foundations ← pitcsolutions.com All lessons

Lesson 9 of 13 in Transformers and LLMs, about 12 minutes

How LLMs Work: The Intuition

You learned English from patterns, not by memorising every sentence. Large language models learn the same way.

By the end of this lesson you will be able to

  • explain in plain words how an LLM learns from text
  • describe next-token prediction and why it is the core of ChatGPT
  • explain tokens, embeddings, attention and inference with everyday examples

Step 1: How did you learn English?

Parents talked to you. Teachers taught you. You read books, watched TV and practised speaking. But here's the key question: did anyone teach you every possible sentence?

No. You learned patterns. After hearing “I eat rice”, “I eat pizza”, “I eat mango”, you automatically understand “I eat pasta”, even if you've never heard it before.

The core idea

A Large Language Model (LLM) learns the same way. It doesn't memorise every sentence. It learns patterns in language.

Step 2: How an LLM learns

Imagine a baby AI. At first it knows nothing: you say “Hello” and it replies “?????”. Now feed it millions of books, Wikipedia, websites, research papers, news and code. That's training.

BooksWikipediaWebsitesResearch papersNewsCode→Training

After seeing billions of sentences, it starts noticing patterns: “The sky is” → blue. “Fish can” → swim. “Birds can” → fly. It is discovering statistical relationships, not memorising a dictionary.

Step 3: What happens during training?

Take the sentence “I love eating ice cream.” Hide the next word and ask the model to predict it:

At first

“I love eating ___” → pizza ❌
The correct answer is ice. The model adjusts its internal parameters slightly.

Trillions of times later

“I love eating ___” → ice is now very likely ✅

This repeated predict → check → correct loop is the essence of training.

Step 4: What does the model actually learn?

People ask: “Does ChatGPT memorise books?” No. It learns probabilities and associations:

Doctor → HospitalTeacher → SchoolKing → QueenDog → Barks

Step 5: The secret behind ChatGPT

Most people think ChatGPT searches a database. It doesn't. Type “The capital of France is” and the model scores every possible next word:

WordProbability
Paris99.7%
London0.2%
Delhi0.05%
Tokyo0.05%

It picks Paris, adds it to the text, then predicts the next token again. One token at a time.

Try it: generate text one token at a time

Each press adds the most likely next token (in blue). The probabilities are illustrative, but the process is exactly how an LLM writes.

Step 6: Tokenisation

LLMs don't read whole sentences, or even whole words. They read tokens. “Artificial Intelligence is amazing.” becomes Artificial Intelligence is amazing .. Longer or rarer words get split: unbelievable → un + believ + able. Every token then gets an ID number, because computers only process numbers:

TokenID
Artificial5821
Intelligence1942
is35
amazing9043

Try it: a toy tokeniser

Type anything. A “·” marks a token that starts with a space. Real tokenisers (like GPT's) learn their splits from data, so the exact pieces will differ, but the idea is the same.

Step 7: Embeddings (word meaning)

Which words are similar: dog, cat, car, lion? You'd say dog, cat and lion are related. The computer learns the same thing. Instead of storing words, it stores vectors (lists of numbers):

Dog → [0.31, 0.82, 0.44, …]
Cat → [0.29, 0.80, 0.42, …]
Car → [0.91, 0.05, 0.63, …]

Dog and Cat have close vectors because their meanings are similar. Car is far away.

A two-dimensional map of word meanings: dog, cat and lion cluster together; car, bus and bike cluster together; king and queen sit near each other Dog Cat Lion Car Bus Bike King Queen animals Real embeddings have hundreds of dimensions; this is a 2-D sketch.

Step 8: The Transformer, the brain of an LLM

Before 2017, models mostly read left to right. Transformers changed everything by looking at all the words together. Take: “The trophy didn't fit in the suitcase because it was too big.” What is “it”? The trophy. The Transformer uses attention to focus on the most relevant words. See lesson 8 for the details.

Step 9: Attention, the superpower

“Benedict hit the ball because he was angry.” Who was angry? Benedict. Your brain paid attention to the relationship between the words. Transformers do the same mathematically: instead of treating every word equally, they give more importance to the relevant ones.

Step 10: Inference, when you ask a question

When you ask ChatGPT “Explain AI”, the model does not learn anything new. It simply predicts the next token again and again:

Explain→Artificial→Intelligence→is→the→ability→…

One token at a time until the response is complete. That's inference.

The complete LLM pipeline

LLM pipeline. Training: books, articles, websites, papers and code are tokenised, converted to numbers and embeddings, and a Transformer learns language patterns. Using: the user types a prompt, it is tokenised, the Transformer predicts next tokens one by one, producing the final response. Training (done once) Books, articles,websites, papers,code Tokenisation Numbers →embeddings Transformerattention layers Learnspatterns Using the model (every question) User's prompt Tokenise Transformer predictsthe next token Final response append the token and repeat
The experienced librarian

Think of an LLM as a very experienced librarian who has read millions of books but doesn't memorise every page. They have learned how language is structured. When you ask a question, they don't look it up in a book; they use everything they've learned to predict the most likely next words, one token at a time.

Fun facts

A common misconception to correct

Students often say “ChatGPT knows the answer.” More accurate: “ChatGPT generates the answer by repeatedly predicting the most probable next token, based on the prompt and the patterns it learned in training.”

That's why LLMs can be impressive yet still make mistakes, or hallucinate, when the most probable continuation isn't factually correct.

Key takeaways

  • LLMs learn patterns in language, not a list of memorised sentences.
  • Training = predict the next word, check, correct, repeated trillions of times.
  • Text → tokens → IDs → embeddings → Transformer with attention.
  • Answering = predicting one token at a time (inference). No learning happens while you chat.

Check your understanding

1. How does ChatGPT produce an answer?

Generation is repeated next-token prediction, one token at a time.

2. Why are Dog and Cat stored as similar vectors?

Embeddings are learned from how words are used; similar usage means similar vectors.

3. Why can an LLM “hallucinate”?

It generates plausible text, and plausible isn't always correct.