8 8. How Large Language Models Work
You don’t need the math to use AI well, but you do need a correct mental model of what a large language model (LLM) does. It explains why LLMs write so fluently, why they sometimes make things up, why they “forget”, and how a coding agent can run commands on your computer.
8.1 Programming vs. training
In classic programming, a person writes the rules, for example a fraud check: if the amount is over $5,000 and the card was used abroad in the last hour, flag it. Machine learning works the other way around: you show the computer many examples with the right answers (thousands of transactions labeled “fraud” or “not fraud”), and it adjusts itself until it gets them right. The rules are learned, not written.
| Classic program | Machine-learning model | |
|---|---|---|
| Who writes the rules? | A programmer | Nobody; they are learned from examples |
| How do you change its behavior? | Edit the code | Train it on different examples, or give it different instructions |
| Can you see why it decided something? | Yes, read the code | Only partly; it is largely a “black box” |
8.2 Neural networks and deep learning
Almost all modern AI uses an artificial neural network, a design loosely inspired by the brain. It is made of many simple units (“neurons”) arranged in layers. Every connection between units has a number, called a weight (or parameter), that sets how strongly one unit influences the next. Training works like this: show the network an example, compare its answer with the correct one, and nudge every weight slightly in the direction that would have made the answer better. Repeat billions of times.
Deep learning means training neural networks with many layers. The largest models now have more than a trillion weights. Nobody sets these numbers by hand, and nobody can fully explain what each one does. Most LLMs use a network design called the transformer (the “T” in GPT), introduced by Google researchers in 2017 (paper).
8.3 An LLM predicts the next token
A large language model (LLM) is a deep neural network trained on enormous amounts of text to do one thing: given some text, predict what comes next.
Text so far: "Joe went to the"
The model's guesses for the next word (made-up numbers):
store 31%
park 12%
doctor 6%
...
circus 1%
intelligent 0.0001%
To write a whole answer, the model repeats this: pick one of the likely next pieces, add it to the text, and predict again.
"The capital of France is" -> predict -> " Paris"
"The capital of France is Paris" -> predict -> "."
"The capital of France is Paris." -> predict -> (end of answer)
That sounds simple, but to predict text well across millions of books, websites, and programs, a model has to pick up grammar, facts, styles, reasoning patterns, and how code works. That’s why LLMs can seem to understand (whether they “really” do is debated).
8.3.1 Tokens
As of Oct 16, 2026 · Official docs: Claude models overview
LLMs don’t read letters or whole words. Before the model sees your text, a program called a tokenizer splits it into tokens: a common word is often a single token, while a longer or rarer word is split into pieces. Spaces, punctuation, and digits count too. Each company uses its own tokenizer, so the same text can be a different number of tokens on different models.
As a rule of thumb, Anthropic’s docs say 1 million tokens is roughly 555,000 English words on the newest Claude models (about 750,000 on models older than Claude Opus 4.7). So in English, a token averages a bit more than half a word. Tokens matter because:
- Prices for using models through an API are quoted per million tokens.
- Limits, such as how much text a model can consider at once, are counted in tokens.
- Quirks: some tasks that are easy for people are hard when you only see tokens.
In 2024, popular chatbots famously answered that “strawberry” has two r’s (it has three) (TechCrunch). The model doesn’t see s-t-r-a-w-b-e-r-r-y; it sees a few tokens (something like straw + berry; the exact split depends on the tokenizer), and the letters inside a token are not directly visible to it.
Modern systems can work around this: a reasoning model can first spell the word out one letter at a time (so each letter becomes its own token) and then count, and an agent can run a one-line program that counts. When exact counting, spelling, or arithmetic matters, ask the AI to use a tool, or check the result yourself.
8.3.2 Choosing the next token
The model doesn’t output one word; it outputs a probability distribution: a probability for every token it knows. The software then samples from it: it usually picks a likely token, but not always the single most likely one. That’s why the same question asked twice can get two differently worded answers.
As of Oct 6, 2026 · Official docs: Glossary · Model deprecations
Temperature is a setting that controls how random the sampling is. At a low temperature the model almost always picks the most likely token, so answers are more predictable. At a high temperature it picks less likely tokens more often, so answers are more varied, but also more likely to go off track. Even at temperature 0, the output is not guaranteed to be identical every time.
Chat apps choose these settings for you. Many developer APIs let you set the temperature, but some newer models no longer allow it: on Anthropic’s newest Claude models, the API rejects non-default temperature values, and Anthropic recommends steering the model through your prompt instead.
8.4 How an LLM is trained
8.4.1 Pre-training
First, the model reads a gigantic collection of text: public web pages, books, articles, code, and other licensed or collected data, adding up to trillions of tokens. Over and over, it predicts the next token, and its weights are nudged whenever it is wrong. This pre-training takes months on thousands of specialized chips and uses a great deal of electricity.
The result is a base model: an excellent autocomplete, but not yet an assistant. Pretrained models “are not inherently good at answering questions or following instructions” (Anthropic glossary): ask one a quiz question and it might just continue with more quiz questions.
8.4.2 Post-training: from autocomplete to assistant
Post-training turns the base model into a helpful assistant:
- Fine-tuning on example conversations teaches it to answer questions and follow instructions.
- Reinforcement learning from human feedback (RLHF): people rank several answers to the same prompt, and the model is adjusted to prefer answers like the higher-ranked ones. (Reinforcement learning means learning by trial and error from rewards and penalties.)
- Reinforcement learning on checkable tasks: the model practices problems whose answers can be verified automatically (math with a known result, code that must pass tests) and is rewarded for correct results. This is a key way reasoning models are trained (DeepSeek-R1 paper).
Post-training also shapes the model’s personality and what it refuses to do, and it can have side effects, such as a tendency to tell users what they want to hear (see the next chapter).
8.5 Reasoning (“thinking”) models and effort
As of Oct 6, 2026 · Official docs: Effort
A reasoning model (or “thinking” model) writes out intermediate steps, called thinking, before its final answer, like working a math problem on scratch paper. Thinking helps on multi-step problems such as math, planning, and debugging. The cost: answers are slower and more expensive, because thinking tokens are billed like other output tokens.
Most current flagship models let you control how much they think. Anthropic calls this dial effort, with five levels: low, medium, high, xhigh, and max; OpenAI and Google offer similar dials. Use low effort for quick, simple tasks and high effort for hard problems where quality matters more than speed.
8.6 Multimodal models
As of Oct 16, 2026 · Official docs: Claude models overview
A multimodal model handles more than one kind of data. All current Claude models accept text and images as input and produce text. Some other models, such as Google’s Gemini 3.8 Flash, also accept audio and video. Creating images, video, or speech is usually done by separate, specialized models. As a developer, you can paste a screenshot (an error message, a page that looks wrong, a design sketch) and ask the AI about it.
8.7 What a model knows: training data vs. context window
As of Oct 16, 2026 · Official docs: Claude models overview · Context windows
An LLM has two very different kinds of “memory”.
8.7.1 Long-term memory: frozen at the knowledge cutoff
What a model learned in training is stored in its weights and stops at the knowledge cutoff, the date its training data ends. Anthropic lists a “reliable knowledge cutoff” for each model: June 2026 for all its current models, and earlier dates for older ones (February 2025 for Claude Haiku 4.5). A model knows little about later events and software versions, and may confidently describe an old version of a fast-changing tool (including Claude Code itself). Apps work around this with web search or documents you paste in.
8.7.2 Working memory: the context window
The context window is the maximum amount of text, measured in tokens, that the model can consider at once. It holds everything for the current request: hidden instructions from the app (the system prompt), the conversation so far, files, tool results, and the model’s own output, including its thinking.
| Model (as of October 2026) | Context window | Roughly |
|---|---|---|
| Claude Fable 5.1, Opus 5.5, Sonnet 5.5, Haiku 5.5 | 1M tokens | 555,000 words |
| Claude Haiku 4.5 (an older model) | 200K tokens | 150,000 words |
Other frontier models also offer about 1 million tokens. Three things to know:
- The model remembers nothing between requests. Its weights don’t change while you chat; each time you send a message, the whole conversation so far is fed to the model again. A new chat starts empty, unless an app’s “memory” feature adds saved notes to the context for you.
- When the window fills up, older material must be dropped or summarized (called compaction), and details can get lost.
- More context is not automatically better. Anthropic’s docs warn that as the token count grows, accuracy and recall degrade, which they call context rot. Give the model what it needs, not everything you have.
Context, Models, Effort, and Usage in Claude Code shows how to manage the context window and effort in Claude Code.
8.8 Why LLMs sometimes make things up
An LLM generates the most plausible-sounding continuation; it does not look facts up. When it doesn’t know, a fluent, confident, wrong answer can still sound the most plausible. A 2025 paper by researchers at OpenAI and Georgia Tech argues that training and testing make this worse: “like students facing hard exam questions”, models are rewarded for guessing rather than admitting uncertainty (Kalai et al.). Such invented answers are called hallucinations; the next chapter covers how to catch them.
8.9 Tools: how an LLM becomes an agent
As of Oct 6, 2026 · Official docs: How tool use works · How Claude Code works
By itself, an LLM can only produce text. A tool is a function that the software around the model lets it ask for: search the web, read a file, edit a file, run a terminal command, query a database. As Anthropic’s docs put it, “The model never executes anything on its own.”
You: "Fix the failing test"
|
v
+----------------+ 1. asks for a tool: +--------------------+
| LLM | ---- run "npm test" ------> | The app (for |
| (the model) | | example, Claude |
| | <--- the test output ------ | Code) runs it |
+----------------+ 2. added to the context +--------------------+
|
v
3. Next step: read a file, edit the code, run the tests again ...
(loop until the task is done, then reply to you)
The model replies with a structured request (which tool, with what inputs); the app runs the tool, often after asking your permission, and adds the result to the context; the model reads it and decides the next step. Repeating this loop is what makes an agent: an LLM plus tools plus a loop.
Claude Code works exactly this way: the model does the reasoning, and Claude Code (the harness around the model) provides the tools, runs them, and manages the context. That is how it can edit files and run commands for you, and why you must review what it does. See How Claude Code Works; the Model Context Protocol (MCP), a standard way to plug in more tools, is covered in MCP Servers.
8.10 Key takeaways
- An LLM predicts the next token; prices and limits are counted in tokens.
- Pre-training gives knowledge (up to a cutoff); post-training makes an assistant.
- The context window is working memory; outside it, the model remembers nothing.
- Tools plus a loop turn an LLM into an agent such as Claude Code.
Practice Questions
Easy
Question 1 · Short answer
What is the main difference between classic programming and machine learning?
Question 2 · Multiple choice
In a neural network, what are weights (also called parameters)?
- The amount of memory the model needs
- Numbers on the connections between units that are adjusted during training
- The rules a programmer writes for the model
- The number of questions a user can ask per day
Question 3 · True or false
An LLM answers questions by looking them up in a database of facts stored inside it.
Question 4 · Short answer
What is a token? Is a token the same as a word?
Question 5 · Matching
Match each term with its description.
| Term | Description | |
|---|---|---|
| 1. Pre-training | A. The date after which the model’s training data ends | |
| 2. RLHF | B. Reading trillions of tokens and predicting the next one, over and over | |
| 3. Context window | C. People rank answers, and the model is adjusted toward the preferred kind | |
| 4. Knowledge cutoff | D. A function the model can ask the app to run | |
| 5. Tool | E. The maximum amount of text the model can consider at once |
Question 6 · Multiple choice
What does the word “deep” in deep learning refer to?
- The model understands topics deeply
- The neural network has many layers
- The training data comes from the “deep web”
- The model thinks for a long time before answering
Medium
Question 7 · Calculate
Use the rule of thumb on this page: 1 million tokens is about 555,000 English words on the newest Claude models. Roughly how many tokens is a 300-page book of about 90,000 words? Would it fit in a 200K-token context window? In a 1M-token window?
Question 8 · Short answer
You ask an AI assistant the same question twice, in two new chats, and get two differently worded answers. Why?
Question 9 · Scenario
You ask an AI chatbot (with web search turned off) how to use a feature that was added to a software tool last month. It confidently describes how the feature worked in an older version. Explain why, and give two ways to get a better answer.
Question 10 · Multiple choice
Which of these happens during post-training?
- The model reads trillions of tokens of web pages and books
- People rank several answers, and the model is adjusted toward the preferred ones
- The tokenizer splits your prompt into tokens
- The app adds tool results to the context window
Question 11 · Short answer
You start a new chat, and the AI has no idea what you discussed yesterday. Why? And how can an app’s “memory” feature still let it know some things about you?
Question 12 · Ordering
Put these steps of a tool call in the right order.
- A. The app runs the tool (after your permission, if needed) and adds the result to the context.
- B. The model reads the result and decides what to do next.
- C. The model replies with a structured request: which tool to use, with what inputs.
- D. You ask the agent to do a task.
Question 13 · Multiple choice
Why does a reasoning model at high effort usually take longer and cost more?
- It uses a larger context window
- It writes many thinking tokens before the answer, and those are billed as output
- It must search the web before answering
- It runs on a different, more expensive tokenizer
Question 14 · Short answer
What does multimodal mean? Give one practical way to use it while building a web app.
Hard
Question 15 · Long answer
Explain why counting the letters in a word such as “strawberry” can be hard for an LLM, and describe two ways modern AI systems get around the problem.
Question 16 · Scenario
Early in a very long session you told an AI assistant, “Always answer in bullet points.” Since then you have pasted a 400-page report and chatted for hours. Now it answers in paragraphs and seems to have forgotten details from the start. Explain what is probably happening, and suggest three remedies.
Question 17 · Long answer
An LLM can only produce text, yet Claude Code can edit files and run commands on your computer. Explain how this works, and why it makes reviewing the agent’s actions important.
Question 18 · Compare
Compare a base model with an assistant model. What does pre-training contribute, what does post-training contribute, and why would a base model alone make a poor chatbot?
Question 19 · True or false
“If you set the temperature to 0, the model’s answers will always be correct.”
Question 20 · Scenario
You can choose the effort level for each request. Which level would you choose for (a) renaming a variable in one file, (b) tracking down a bug that only happens sometimes and involves many files, and (c) a quick question about what a command does? Explain the trade-off.
Challenge
Question 21 · Hands-on
Ask an AI assistant: “Which model are you, and what is your knowledge cutoff?” Then look up the same model on its maker’s official documentation. Do the answers match? Explain why they might not.
Question 22 · Hands-on
Ask an AI assistant to count how many times a given letter appears in a long, unusual word or phrase (for example, the letter “s” in “Mississippi’s assistants”). Then ask it to spell the word out letter by letter first and count again. Compare the two answers and explain any difference.
Question 23 · Hands-on
If an AI app you have access to lets you choose the effort or thinking level, give it the same tricky logic puzzle at the lowest and the highest level. Compare the time it took, the quality of the answer, and (if shown) the amount of thinking.