How AI models work

How AI models work

How does an AI model like ChatGPT come up with its answers? See how it predicts one word at a time, and what tokens, context and training mean.

A person pausing thoughtfully, with a thought bubble and a speech bubble

How do humans think?

Before understanding how LLMs work, let’s pause and reflect on how we think and talk in our day-to-day life.

We think for a while, analyse the situation, and respond by speaking a sequence of words, one after another.

Four token tiles linked by arrows, with the newly added final token highlighted

Tokens

LLMs work in much the same way. They predict the next probable word (AI jargon: “token”) in a sequence, and repeat until the next prediction is a “stop” token.

A row of input tokens inside a window, with one new token added at the end

The context

The principle of an AI model is simple at its core. Give it x tokens, and it outputs the (x+1)th token. The x input tokens are called the “context”, and the maximum number of tokens it can take is the “context window”.

A scholar and a layman side by side

A scholar vs a layman

We judge people by how well-spoken, knowledgeable and contextually aware they are. This is shaped by their intelligence, experience, knowledge and memory.

A model learning from a huge library of text

Training

Similarly, the next token a model predicts is not random. It is based on patterns learned during training, from trillions of tokens of text gathered from the internet and other sources.

A sentence with a blank at the end and two possible words, money and peace

Predict the next token

The most important thing in life is ________

There is no right answer to this. Someone might say money, while someone else might say peace.

Connected neurons, some links stronger than others

The neurons

For each person, the answer depends on how their brain is wired by their knowledge, experiences, aspirations and current situation. One person weighs x more, another weighs y more.

AI models replicate the same concept, but mathematically.

A simple next-token predictor

Let’s simplify. Say all the words in English are numbered from 1 to 50,000. Multiply each word in “Most important thing in life is” by a weight, and add them up:

 100 × 0.1
5230 × 0.5
 400 × 0.8
 523 × 0.6
4943 × 0.9
  95 × 0.3
 sum = 7736 → maybe “peace”

Training tunes these weights, and the weights are called parameters.

Rows of GPUs in a data centre

Billions of parameters

A typical LLM has billions of parameters, so billions of calculations happen for every next token. That is why we need big GPUs and data centres.

Token embeddings

Is there a better way to represent a word than one number? What if we ask each word 100 questions, like:

  1. Are you a noun?
  2. Are you a colour?
  3. Are you a food?

Each answer is a score from 0 to 1. So “red” and “blue” get almost the same scores, and similar words sit close together.

Attention

Check out these sentences:

I need a match to light the candle.

Their outfits were a perfect match.

Same word, two meanings. The model works it out by focusing on the words that matter, like “candle” or “outfits”. That is called attention.

Summary

  • AI models work by predicting the next token.
  • They have no memory. The context is the input; the next token is the output.
  • So every message and piece of data must be fed into the context (the AI app handles this).
  • A good model focuses on the right parts of the context. When it confidently states something false, that is called hallucination.