How language models work
Concepts · topic concepts/how-models-work
A language model turns text into tokens, predicts the next token from everything it has seen so far, and repeats until it stops. This topic covers the pieces behind that loop: tokens, the context window that bounds what the model can see, the split between training and inference, the trade-offs between model families and sizes, and how sampling settings shape the output.
Concepts
- Token
- The unit a language model reads and writes. A token is a chunk of text, often a word fragment, punctuation mark or common word, chosen by a fixed tokenizer. Models are priced, limited and timed in tokens, not words or characters. A rough rule for English is three to four characters per token, so a page of text is a few hundred tokens. glossary
- Context window
- The maximum number of tokens a model can take into account at once, counting the instructions, the conversation so far, any documents pasted in and the answer it is producing. Anything outside the window does not exist for the model. Larger windows cost more per call and do not guarantee the model uses everything inside them equally well. glossary
- Training vs inference
- Training is the one-off, expensive phase where a model's weights are adjusted on a large body of text until it predicts well. Inference is every later use of the frozen model to produce output. Nothing you type during inference changes the weights; the model only "remembers" what is in the current context window. glossary
- Model family and size
- Vendors publish models in families with several sizes. Larger models usually reason better and follow complex instructions more reliably, but cost more and respond more slowly. Smaller models are cheaper and faster and are often good enough for narrow, well-specified tasks. Choosing a model means matching the size to the difficulty of the job. glossary
- Sampling and temperature
- At each step the model has a probability for every possible next token and picks one by sampling. Temperature scales those probabilities: low values make the model pick the most likely token almost every time, high values spread the choice across less likely options. Low temperature gives consistent, conservative output; high temperature gives variety at the cost of more mistakes. glossary
Links
- Builds on: nothing
- Leads to: Capabilities and limits, Prompting
- Competencies drawing on it: Explains how a language model produces text and where it fails, Writes prompts that get reliable results
Lessons
- What the model can see (tutorial)
- How a language model works (explanation)
Your reference
Each lesson above adds its takeaways and its example here once you finish it. Your reference lists every lesson you have finished.
What the model can see
Unlocks when you finish What the model can see.
Takeaways
- A context window is the maximum number of tokens the model takes into account for one response, and a page of English text is a few hundred tokens.
- The window holds the instructions, the conversation so far, anything pasted or attached, tool results and the answer being produced. Anything outside it does not exist for the model.
- When a long chat contradicts itself, an early fact has left the window. Restate the fact in your next message instead of scolding the model.
- A fuller window costs more per answer, because every token in it is processed for every response, and the model does not use every part of a full window equally well.
Example
Write the closing section now. Same audience and length as we agreed.
Here is the closing section for the engineering team. I have kept the technical detail you asked for, so it runs to about four pages. Let me know if you want the deployment appendix as well.
How a language model works
Unlocks when you finish How a language model works.
Takeaways
- Input is tokens rather than words, and that alone explains a number of odd weaknesses.
- Output is one token at a time, sampled from a distribution the model scores. Temperature reshapes the distribution and doesn't add knowledge.
- The scores come from weights fitted to training text, so knowledge is frequency-weighted and stops at the cutoff.
- A second, shorter training stage on rated replies turns the text continuer into an assistant, and the ratings have side effects.
- Hallucination, sycophancy, dilution, and staleness all follow from those two stages.
Example
This lesson has no runnable example or prompt block.
Sources
AEC-02How agents think: tokens and context, reasoning strategies, model choice, system prompts, Agent Engineer Course (course)DLAI-2Generative AI for Everyone, DeepLearning.AI (course)DLAI-6Introduction to Generative AI for Software Development, DeepLearning.AI (course)Academy ai-capabilities-and-limitationsAI capabilities and limitations, Claude Academy (course)Academy ai-fluency-framework-foundationsAI Fluency: Framework and foundations, Claude Academy (course)Academy building-with-the-claude-apiBuilding with the Claude API, Claude Academy (course)Liu 2024Lost in the Middle: How Language Models Use Long Contexts, Transactions of the Association for Computational Linguistics 12 (2024), 157-173 (paper)