4. Language Models#
Large language models support features such as drafting replies, summarizing records, and answering questions. This chapter opens the box enough to make sound technical decisions: how text becomes tokens, how the transformer predicts the next token, and what happens at inference time that affects cost, speed, and output quality.
Tokens: Models read and write tokens, which set the limits of the context window and the cost of each call.
Transformers: Embeddings and attention let a transformer use preceding context to produce a distribution over the next token.
Inference: Sampling settings such as temperature shape output variety, while model size, output length, and reasoning effort shape latency and cost.
Chapter Video
This visual walkthrough follows text through a transformer, from tokens and embeddings to the prediction of the next token.
Transformers, the tech behind LLMs | Deep Learning Chapter 5 (27:14) by 3Blue1Brown
Hosted on YouTube; property of its original creator, embedded here for educational reference only.
Learning Objectives
By the end of this chapter, you will be able to:
Explain how text is converted to tokens and why token counts matter.
Describe how embeddings and attention let a transformer predict the next token.
Explain how temperature and sampling change model output.
Relate model size, context length, and output length to latency and cost.
Count tokens and estimate the cost of a model workload in Python.
Chapter Flow
Glossary
# |
Term |
Explanation |
|---|---|---|
1 |
Token |
A unit of text, often a word or part of a word, that a language model reads or writes. |
2 |
Tokenizer |
The component that splits text into tokens and maps them to numeric IDs. |
3 |
Context window |
The maximum number of tokens a model can consider in one request. |
4 |
Embedding |
A numeric representation of a token or other input, learned for a task. |
5 |
Transformer |
The neural network architecture behind modern language models. |
6 |
Attention |
The mechanism that lets a transformer weigh how each token relates to the others. |
7 |
Next-token prediction |
Producing a distribution over the next token, given preceding context. |
8 |
Parameter |
A numeric weight learned during training and used during inference. |
9 |
Sampling |
Choosing the next token from the model’s probability distribution. |
10 |
Temperature |
A setting that controls how random sampling is; lower values give more predictable output. |
11 |
Latency |
The time between sending a request and receiving the response. |
12 |
Hallucination |
Fluent model output that is false or unsupported. |
13 |
Token ID |
An integer identifying a token in a particular vocabulary. |
14 |
Byte-pair encoding (BPE) |
A subword approach that merges adjacent units according to ordered rules. |
15 |
Input tokens |
Tokens supplied as context, including the counted request overhead. |
16 |
Output tokens |
Tokens generated during the response, subject to the interface’s accounting rules. |
17 |
Output limit |
A separate cap on generated output. |
18 |
Causal mask |
A restriction blocking a position from attending to later positions. |
19 |
Autoregressive generation |
Generating successive tokens conditioned on the current prefix. |
20 |
Greedy decoding |
Selecting a highest-probability candidate at each step. |
21 |
Top-p sampling |
Sampling from a leading candidate set reaching a probability threshold. |
22 |
Time to first token (TTFT) |
Elapsed time until the first generated token arrives. |
23 |
Streaming |
Delivering response pieces as they become available. |
24 |
Truncation |
Cutting off a response before its intended completion. |
25 |
Prefill |
Processing the supplied input context. |
26 |
Decoding |
Generating continuation tokens. |
27 |
Grounding |
Connecting generated content to appropriate evidence. |
28 |
Tokenization |
The conversion of text into model input units. |
29 |
Attention mask |
A restriction on which positions may contribute to a representation. |
30 |
Output budget |
Space reserved for generated content. |
31 |
Reasoning model |
A model trained to generate intermediate work before its answer. |
32 |
Reasoning tokens |
Generated tokens of intermediate work, counted as output whether or not they are returned. |
33 |
Inference-time compute |
Extra generation spent at request time to improve an answer, also called test-time compute. |