TetSharing
AI basics tutorial · 10 chapters
  1. What machine learning is
  2. Features and labels
  3. Train, test and validation data
  4. Overfitting
  5. Accuracy, precision, recall
  6. k-nearest neighbors
  7. LLMs, tokens and context
  8. Prompt basics
  9. Embeddings and similarity
  10. Responsible AI (NIST AI RMF)

Final AI basics quiz

Chapter 7 of 10 · By Ethan Cole · Updated November 21, 2026

How LLMs Work: Tokens and Context Windows Explained

A large language model (LLM) is a machine learning model trained on very large amounts of text to do one thing: given some text, predict what comes next. Chat assistants, code assistants and writing tools are built by running that prediction over and over, adding one piece of text at a time.

Tokens

LLMs do not read letters or whole words. Text is first split into tokens by a tokenizer. A token is often a whole common word, a piece of a longer word, a punctuation mark or a space-plus-word combination. Rare words are split into several pieces, so the model can handle words it has never seen as a whole. Each token is mapped to a number, and the model works entirely with those numbers.

A common rule of thumb is that a token is about three quarters of an English word on average, but this varies between tokenizers, and languages other than English often need more tokens for the same meaning. Usage limits and prices of LLM services are usually counted in tokens.

A toy tokenizer

Real tokenizers learn their vocabulary of pieces from data. The example uses a tiny hand-written vocabulary and the same greedy longest match idea some tokenizers use: at each position, take the longest piece in the vocabulary. Words outside the vocabulary fall apart into smaller pieces, down to single letters if necessary.

Python 3 · editable

Expected output

7 tokens: ['the', 'cat', 'sat', 'on', 'the', 'mat', '.']
4 tokens: ['un', 'believ', 'able', '!']
4 tokens: ['token', 's', 'are', 'fun']
7 tokens: ['z', 'o', 'r', 'b', 'l', 'a', 'x']
window of 6 keeps: ['.', 'token', 's', 'are', 'fun', '!']

Notice that "unbelievable" becomes three tokens, and that the made-up word at the end breaks into many small pieces. The final lines simulate a context window.

Context windows

The context window is the maximum number of tokens a model can take into account at once: your instructions, the conversation so far, any documents you pasted, and the reply being written. When a conversation grows beyond it, older material has to be dropped or summarized, which is why a long chat can "forget" its beginning. Window sizes differ widely between models and are listed in each provider's documentation.

Prediction, not lookup

An LLM produces the likely next token given patterns in its training data. It does not look facts up in a database unless the application around it adds that step. As a result, it can produce fluent text that is wrong, a failure often called hallucination and described by NIST as confabulation. Treat outputs as drafts to check, especially facts, numbers, citations and code.

Summary

  • An LLM predicts the next token, one at a time.
  • Tokenizers split text into word pieces; rare words take more tokens.
  • The context window caps how many tokens fit at once, including the answer.

Quiz: LLMs, tokens and context

1. What is the basic task an LLM is trained on?

Generation repeats next-token prediction many times.

2. Why might a rare word use several tokens?

Subword tokenizers break unfamiliar words into known pieces.

3. What counts against a model's context window?

Everything the model reads and writes in a request shares the window.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.