New: “The Map of AI”, my book on artificial intelligenceMy book: “The Map of AI”Discover it

How ChatGPT really works (and what sits behind the chat)

ChatGPT seems to know things, but it does one thing only: it predicts the next piece of text, thousands of times per answer. Understanding that tells you when to trust it and when not to. Here are tokens, context, attention and training, with examples to try.

Mattia PanattaUpdated 9 minBeginner
Word tiles flowing into a glowing lens and branching into probability paths
AI-generated illustration
On this page
  1. A machine that predicts the next token
  2. Tokens: the machine’s words
  3. How it grasps meaning: embeddings and attention
  4. The three stages of training
  5. What’s inside: compression, not an archive
  6. Why writing costs more than reading
  7. What an LLM is not
  8. What it means for you, in practice

In 30 seconds

  • An LLM (Large Language Model) does one thing: it predicts the next piece of text, one at a time, thousands of times per answer.
  • Text is encoded as tokens. A token may cover a word, part of a word, spaces or punctuation.
  • Attention combines information from the context positions available to the model.
  • It is built in three stages: pre-training (reading half the internet), instruction tuning (learning to answer), reinforcement (learning what we prefer and how to reason).

If you already know what a token is, skip to “The three stages of training”.

Type a question into ChatGPT, Claude or Gemini and the answer starts appearing word by word, as if someone were typing it on the other side. It feels like talking to someone who “knows”. In reality, behind the chat there is a mechanism that is surprisingly simple in its core idea and incredibly complex in its scale. Understanding it changes how you use it: you stop trusting it where you shouldn’t and start using it where it shines.

A machine that predicts the next token

At each step an autoregressive language model scores possible next tokens. The system selects one and adds it to the context. The number of steps for an answer depends on its encoding.

Try it below: you can pick the next word yourself or let the “model” choose. The probabilities are illustrative, but the mechanism is exactly this.

LLM · demo

How an LLM generates

The model does not "know" the sentence: at each step it scores every possible next word and picks one.

Pick the next word yourself

The sentence as tokens (split and IDs are illustrative)

Two things to notice:

  1. The model does not plan the whole sentence in the human sense. Every choice shapes the next ones. That is why, once it heads down the wrong path, it tends to continue consistently along it (one source of hallucinations).
  2. The answer is not fixed. Ask the same question twice and you may get different answers, because each choice is “drawn” from the options according to their probabilities. Temperature controls how daring the model is: low, it almost always picks the most likely; high, it picks unlikely ones more often.

Try it

Ask a chatbot the same thing twice in two new chats (“Write a slogan for a neighbourhood pizzeria”). The answers will differ. Now ask a precise fact (“When was the Transformer introduced?”): the answer will be almost always identical, because one continuation is far more likely than any other.

Tokens: the machine’s words

Two tokenizers can assign different lengths to the same text. These counts were obtained with tiktoken, encoding each lowercase word on its own with no leading or trailing space.

Text cl100k_base o200k_base
house 1 1
intelligence 1 2
antidisestablishmentarianism 6 6

Repeat the test using the encoding name and exact text. For a cost estimate use the selected model’s tokenizer or usage record. Adjacent text, spaces and punctuation can change the result.

How it grasps meaning: embeddings and attention

Each token is turned into an embedding: a list of thousands of numbers that represents its meaning as a point in space. Words with similar meanings end up close together: “king” and “queen”, “Rome” and “Milan”, “invoice” and “receipt”.

But a word alone is ambiguous. Is a “bank” a lender or the side of a river? Here comes the idea that changed everything in 2017, introduced by eight Google researchers in Attention Is All You Need: the Transformer and its attention mechanism.

With attention, every token “looks at” the other tokens in the text and decides how relevant each is (while writing, the model only sees the ones that come before, not the ones it has yet to produce). In “I deposited my salary at the bank”, the token “bank” pays a lot of attention to “deposited” and “salary”, and its meaning shifts towards the lender. This happens across dozens of stacked layers: the first ones capture grammar, the last ones concepts, intentions, tone.

Why you should care

Attention works on everything in the text: your question, instructions, uploaded documents, the previous conversation. The model understands better when you give it context: who you are, what you need it for, who the answer is for. It’s the basis of every good prompt (see the prompting guide).

The three stages of training

A model like Claude Opus 5.5 or GPT-6 isn’t born able to converse. It goes through three very different stages.

1. Pre-training: reading almost everything

The model reads a huge amount of text (web pages, books, scientific papers, code) and trains on the usual task: predicting the next token. Each mistake nudges its parameters, the numbers that govern its behaviour. Frontier models have from hundreds of billions to several trillion.

This stage needs tens of thousands of GPUs for months and costs hundreds of millions of dollars. The result is a “base model” that knows a lot but can’t answer: if you write “How do I register as a freelancer?”, it might continue with “How do I deregister? How do I change tax regime?”, because online that question often appears in a list of questions.

2. Instruction tuning: learning to answer

The base model is trained on hundreds of thousands of examples of good conversations written by people: question and ideal answer. It learns the dialogue format, to follow instructions, to sound like an assistant.

3. Reinforcement: learning what is better

Finally the model learns from a quality signal. In the classic form (RLHF, reinforcement learning from human feedback) people compare two answers and choose the better one; the model learns to produce more answers of the preferred kind. Since 2024, reinforcement on verifiable problems (maths, code, tasks with a checkable answer) is used more and more: the model tries, is rewarded when right, and learns reasoning strategies. That’s how models that “think” before answering were born.

What’s inside: compression, not an archive

A common mistake is to picture the model as an archive of texts from which it “retrieves” answers. It isn’t. The parameters are a statistical compression of what it read: regularities, concepts, relationships, styles. The model doesn’t have Leonardo’s Wikipedia page: it has a distributed representation of “things people say about Leonardo”.

Practical consequences:

  • Well-known facts are reliable, rare ones aren’t. It won’t get Dante’s birth year wrong; it may invent last quarter’s court ruling, confidently.
  • It has a knowledge cut-off. It knows little or nothing after its training data ends, unless it uses web search. That’s why, for questions about current events, it’s worth asking explicitly to search the web and cite sources.
  • It doesn’t know what it doesn’t know. There’s no reliable “certainty meter”: the same confident tone accompanies right and wrong answers.

Why writing costs more than reading

When you send a message, the model works in two phases:

  1. Reading (prefill). It processes all the input text in parallel, in one go. Fast and efficient.
  2. Writing (decode). It generates the answer one token at a time: for each token it has to run through the whole model. Slow, and it ties up the GPU much longer.

That’s why output usually costs several times more than input in price lists. And that’s why models that reason at length, generating thousands of “thinking” tokens before the answer, cost more and are slower. Speed also varies a lot between models: the smallest write several times faster than the largest.

What an LLM is not

  • Not a search engine. Without web search it consults nothing: it remembers, in the statistical sense above.
  • Not a calculator. Modern models know when to use a tool, though: for maths they write and run code. Ask for it explicitly when numbers matter.
  • It doesn’t remember other chats unless a memory feature is on, which actually inserts saved notes into the new conversation’s context.
  • Not conscious. The philosophical debate is open and interesting, but for daily use the useful thing to remember is different: it produces plausible text, which is often also true. “Often” isn’t “always”.

What it means for you, in practice

  1. Give context. Attention works on what you write: more relevant information, more relevant answers.
  2. Check rare facts and numbers. The more specific, recent or niche the information, the more it needs checking.
  3. Ask for tools when needed. “Use web search and cite sources”, “compute by running code”.
  4. Repeat important questions. If two answers to the same question diverge, the model isn’t sure.
  5. Pick the model for the job. A fast, cheap model is enough for an email; a complex analysis needs one that reasons. See the guide on choosing a model and plan.

Go deeper

If you want to see all this animated, 3Blue1Brown’s “Large Language Models explained briefly” lasts eight minutes and is the best visual introduction there is. If you have three hours and want to really understand, Andrej Karpathy’s “Deep Dive into LLMs like ChatGPT” is worth a university course. Here they are: 3Blue1Brown, Large Language Models explained briefly and Andrej Karpathy, Deep Dive into LLMs like ChatGPT.

Ready-made requestAsk the model to show you its limits

Explain in plain words how you produced the answer I just asked for: which parts are well-known facts, which are plausible reconstructions, which would need a source or a calculation. Also tell me which information might have changed after your training data ends.

In one lineAn LLM doesn't look up the true answer: it writes the most likely one. Its strengths and flaws all follow from that.

Sources

Informational content. Prices, models and rules change often: check official sources before deciding. Written with the support of AI tools.

Found it useful? Keep going

The book · €19.90“The Map of AI”, my bookEverything behind the guides, in a single path, as PDF and EPUB. The first chapter is free.Discover the book →Private lesson · €60/hourOne hour with me on Google MeetWant to really get it? A one-to-one lesson, jargon-free.Book a lesson →
Newsletter · freeEvery Monday, what matters

Start here · stop 2 of 4 →Hallucinations, why AI makes things up and how to protect yourselfA hallucination doesn't look like a mistake: it is written with the same confidence as a true answer, ruling number included. Here is why models make things up, when it happens most and a six-step method to avoid being fooled.