Skip to content
POLARISAI ACADEMY
Going deeper (optional)

This lesson is an optional deeper look that goes with course F01. It has worked examples with numbers and technical words. Start with F01 if you are new. Go to course F01 →

FD01 · First Light

How Language Models Work

You can count tokens with a stated rule set, recompute a three-token distribution at temperatures 0, 0.7 and 1.4, and name the mechanical reason a reply went wrong.

Level
First Light
Time
8 h (estimate)
Prerequisites
F01
Course 1 of 7
Jump to

After this course you can

The student can calculate exact token counts for supplied strings, trace next-token probabilities across temperatures 0.0, 0.7, and 1.4, and match 15 defective model transcripts to their exact mechanical cause.

Tokens and subword splits

A language model reads tokens. The letters you typed are already cut into those pieces before any prediction starts. Subword tokenization keeps frequent chunks, such as the or ing, as whole tokens. In this lesson's simplified rule, close to WordPiece, a rare word is cut into the longest pieces the vocabulary already contains, and single letters are the fallback when nothing longer fits. Real BPE tokenizers instead merge pairs in a learned order, with bytes as the fallback. The rule is fixed for a given tokenizer. The same spelling can be one token in one system and several tokens in another.

Token count decides how much text fits and how much work a reply takes. A short familiar word may spend one token. A long name, a word from another language, or a run of punctuation can spend several. In many tokenizers the space is glued to the following word, and a period stands alone. Dividing the character count by four gives only a rough average for English prose. An exact count uses one named rule set and follows it from the left. Guessing from character length will miss the split.

The next token and the weights

At each step the model assigns a score to every token in its vocabulary. Softmax turns those scores into probabilities. Each score is passed through the exponential function, then divided by the sum of those exponentials, so the results add to 1. One token is drawn from that distribution and stuck onto the end of the text. The same computation then runs for the following step. Fluency comes from those local choices lining up, one token after another.

Those scores come from the weights, a large list of numbers inside the network. People also call the numbers parameters. Training is the period when the weights still change. The trainer shows real text, asks for the next token, and nudges the weights so probability moves toward the token that actually followed. That pass is repeated across a huge pile of text. When training stops, the weights are stored. Your later chat does not keep editing them.

Inference is a reply with the weights held still. The context goes in, a distribution comes out, a token is chosen, and the new context goes in again. Nothing in that loop searches the web, and nothing opens a database of facts. If a separate tool is attached, that tool is a later add-on. The plain model step only transforms the current tokens through the fixed weights. Two runs can differ when the sampler draws a non-top token, and they match when the sampler always takes the top one.

Context window and the cache

The context window is the maximum number of tokens the model can take in one forward pass. The count includes the prompt and the tokens already generated. A token outside that limit is absent from the computation. The model continues from a shortened view, often the latest tokens that still fit. A long pasted article can crowd out the question you typed at the top. Because the limit counts tokens, a rare word split into many pieces fills the window faster than the same number of common words.

The KV cache is a speed store used during inference. For each earlier token the model has already built a key vector and a value vector, the K and the V in the name. Attention uses those vectors when the new token is scored against earlier ones. Keeping them lets the model reuse that work on the next step. The cache holds neither a summary nor extra facts. When a token leaves the window, its cache entry leaves with it.

Sampling, cutoff, and fluent mistakes

Temperature rescales the scores before softmax. Divide every score by T. A T below 1 makes the largest score pull further ahead, so the distribution gets sharper. A T above 1 pulls the scores together, so the distribution gets flatter. As T approaches 0 the sampler keeps only the winning token. People call that greedy. Top-k keeps the k highest probabilities, deletes the rest, and rescales what remains. Top-p sorts from the top and keeps the smallest set whose probabilities sum to at least p, then rescales that set.

These three settings change which continuation you are likely to see. They leave the weights untouched, so they add no stored fact. The weights stop at a knowledge cutoff, the last date in that training collection. Later text is missing unless you put it in the prompt. A name or a release from after the cutoff can still appear, because a next token is always chosen and a plausible one can score highly. When the date matters, read it in the notes for that model.

A fabricated detail has the same source. The sampler picked tokens that continue the local pattern. A made-up citation, a firm date, or a bad arithmetic step can each be the high-scoring continuation. There is no inner step that compares the sentence with a source. You catch some of these by doing the small sum yourself, by pasting the source into the window, or by using a tool that returns a document. Even then the reply is a string of next-token draws, now conditioned on that extra text.

An example

A count and a temperature, both by hand

Use this example tokenizer, built so a count by hand is exact. Lowercase the text. Split on spaces, and keep each space as a leading mark on the next piece. A whole word is one token when it is in this list: the, cat, sat, on, a, mat. Any other word is read from the left and cut by the longest match in this piece list: un, re, ing, ly, tion. When none of those matches, take one letter. A period is always its own token.

Take the string The cat sat on a mat followed by a period. It holds 21 characters. The tokens are the, cat with a leading space, sat with a leading space, on with a leading space, a with a leading space, mat with a leading space, and the period alone. That is 7 tokens. Dividing 21 by 4 gives 5.25. unhappily is absent from the word list. The piece un matches, then the letters h, a, p, p and i, then the piece ly. Those are 7 tokens.

Start from three example probabilities at temperature 1: 0.50, 0.30 and 0.20, for the candidate tokens cat, sat and mat. They are examples, picked so the arithmetic stays visible. If those probabilities came from a softmax, the underlying scores are their logarithms, up to one added constant. Divide the scores by T and run softmax again. That is the same as raising each probability to the power 1/T, then dividing by the sum of the three powers.

At temperature 0.7 the power is about 1.429. The three powers round to 0.3715, 0.1791 and 0.1003, and their sum is 0.6509. Divide: 0.3715/0.6509 gives 0.571, 0.1791/0.6509 gives 0.275, and 0.1003/0.6509 gives 0.154. At temperature 1.4 the power is about 0.714. The powers round to 0.6095, 0.4232 and 0.3168, and those rounded powers sum to 1.3495. The shares round to 0.452, 0.314 and 0.235. Those three rounded shares add to 1.001, while the exact shares add to 1. At temperature 0 the sampler takes cat, the leader, with probability 1.

Exercise

Check it on paper

Using the example tokenizer, write every token in A cat sat unhappily followed by a period. Then take the example probabilities 0.50, 0.30 and 0.20. Temperature 0.5 means each one is raised to the power 2. Square them, divide by the sum, and round to three decimals. From the worked shares, write the probability of cat at temperatures 0, 0.7 and 1.4. Then name one cause for each slip: a match score from after the cutoff, with no article in the prompt; an opening rule that disappears after a very long paste; the same phrase repeated at temperature 0; a citation printed with no source and no tool result in the window.

The eleven tokens are a, cat with a leading space, sat with a leading space, un with a leading space, h, a, p, p, i, ly, and the period. The squares are 0.25, 0.09 and 0.04, the sum is 0.38, and the shares are 0.658, 0.237 and 0.105. The probability of cat is 1 at temperature 0, 0.571 at 0.7, and 0.452 at 1.4. The slips are a knowledge cutoff, a context window that dropped the early tokens, greedy decoding, and a next-token continuation with no source check.

Check questions

Answer on your own first. Then open a question to see the answer.

How many tokens does the example tokenizer make from sat followed by a period, with no leading space?

Two. sat is in the word list, and the period is its own token.

In the worked example, cat starts at 0.50. What is cat's probability rounded to three decimals at temperature 0.7, and what is it at temperature 0?

0.571 at temperature 0.7, and 1 at temperature 0. Greedy decoding keeps only the leading token.

What does a correct KV cache store, and does that correct cache change the next-token probabilities?

It stores the key vector and the value vector for each earlier token still in the window. A correct cache leaves the probabilities unchanged and only saves recomputation.

Why can the model state a plausible citation that was never in the prompt and never returned by a tool?

The citation-shaped tokens were a likely continuation of the text so far. The plain model step does no search and does not compare the sentence with a source.

Take with you

A reply is a chain of tokens drawn from a distribution fixed by the weights and the current window. Temperature, top-k and top-p change the draw, and a fluent sentence can still be false when the source was never in that window.

Final task

Complete a 20-item keyed diagnostic set on tokenisation, sampling math, and failure classification, plus submit a script (token_check.py or .js) that passes 10 fixed unit tests on token counting and sampling parameter bounds. (Optional Portfolio item: plain-language explainer page).

The final task is marked against fixed answer keys and automated tests once the tests open.

Tests and diplomas coming

This lesson is open to read. Tests, diplomas and accounts come later; the waitlist is for those. Join the waitlist