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?

In classic programming, a person writes the rules and the computer follows them exactly. In machine learning, nobody writes the rules: the computer is shown many examples with the right answers and adjusts itself (its weights) until it gets them right, so the rules are learned from data.

Question 2 · Multiple choice

In a neural network, what are weights (also called parameters)?

  1. The amount of memory the model needs
  2. Numbers on the connections between units that are adjusted during training
  3. The rules a programmer writes for the model
  4. The number of questions a user can ask per day

b. Weights are the numbers that set how strongly one unit influences the next. Training nudges them, example after example, until the network gives good answers. They are not written by a programmer (c), and they have nothing to do with memory size (a) or usage limits (d), although more weights do mean a bigger model.

Question 3 · True or false

An LLM answers questions by looking them up in a database of facts stored inside it.

False. An LLM generates text by predicting one token at a time. Its knowledge is spread across billions of weights, not stored as a list of facts it can look up. That is why it can produce a fluent answer that sounds right but is wrong.

Question 4 · Short answer

What is a token? Is a token the same as a word?

A token is a small piece of text that the model reads and writes. A tokenizer splits text into tokens before the model sees it. A token is not the same as a word: a common word may be one token, but a long or rare word can be split into several tokens, and spaces, punctuation, and digits also count. On average, an English token is a bit more than half a word on the newest Claude models.

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

1 → B, 2 → C, 3 → E, 4 → A, 5 → D.

Question 6 · Multiple choice

What does the word “deep” in deep learning refer to?

  1. The model understands topics deeply
  2. The neural network has many layers
  3. The training data comes from the “deep web”
  4. The model thinks for a long time before answering

b. Deep learning means training neural networks with many layers of units. It says nothing about understanding (a), data sources (c), or thinking time (d); “thinking” is a separate feature of reasoning models.

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?

A token is about 0.555 words, so 90,000 words ÷ 0.555 ≈ 162,000 tokens. That fits in a 200K-token window, but only just: about 38,000 tokens would be left for instructions, your questions, and the model’s answers (including its thinking). It fits easily in a 1M-token window. (It is only an estimate: the real number depends on the tokenizer and the text. On an older tokenizer, such as Claude Haiku 4.5’s, the same book is closer to 120,000 tokens.)

Question 8 · Short answer

You ask an AI assistant the same question twice, in two new chats, and get two differently worded answers. Why?

At each step the model produces a probability distribution over possible next tokens, and the software samples from it: it usually picks a likely token, but not always the single most likely one. Small differences early in the answer lead to different wording later. (The app may also use a different model or settings, or the model may have been updated.)

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.

The model’s knowledge comes from training data that ends at its knowledge cutoff, so it doesn’t know about last month’s change. Because it generates plausible text rather than admitting it doesn’t know, it describes what it learned about the older version.

Better options: turn on web search, paste the relevant part of the official documentation (or the changelog) into the chat so it is in the context window, or check the official docs yourself.

Question 10 · Multiple choice

Which of these happens during post-training?

  1. The model reads trillions of tokens of web pages and books
  2. People rank several answers, and the model is adjusted toward the preferred ones
  3. The tokenizer splits your prompt into tokens
  4. The app adds tool results to the context window

b. That is RLHF, one part of post-training (along with fine-tuning on example conversations and reinforcement learning on checkable tasks). Option a is pre-training. Options c and d happen every time you use the model, not during training.

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?

The model itself remembers nothing between requests: its weights don’t change while you chat, and it only “knows” what is in its context window for the current request. A new chat starts with an empty conversation. A memory feature works by saving notes about earlier chats and automatically adding them to the context of new chats, so the model can read them.

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.

D → C → A → B. Then the loop repeats (more tool calls) until the task is done and the model replies to you. Note that the model never runs anything itself; the app does.

Question 13 · Multiple choice

Why does a reasoning model at high effort usually take longer and cost more?

  1. It uses a larger context window
  2. It writes many thinking tokens before the answer, and those are billed as output
  3. It must search the web before answering
  4. It runs on a different, more expensive tokenizer

b. Thinking is extra generated text, so it takes time and is billed like other output tokens. Higher effort generally means more thinking. The context window size (a), web search (c), and the tokenizer (d) are not what effort controls.

Question 14 · Short answer

What does multimodal mean? Give one practical way to use it while building a web app.

A multimodal model handles more than one kind of data, for example text and images. While building a web app, you can paste a screenshot of an error message, of a page that looks wrong in the browser, or of a design sketch, and ask the AI to explain the error, find the layout problem, or build the page.

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.

The model doesn’t see individual letters. The tokenizer turns the word into a few tokens (pieces of the word), and the letters inside each token are not directly visible to the model; it only knows them indirectly from training. So it has to “guess” the spelling from memory, which can fail.

Workarounds: (1) reasoning: the model first spells the word out one letter at a time, so each letter becomes its own token, and then counts; (2) tools: an agent writes and runs a tiny program that counts the letters exactly. For important counts and calculations, you can also check the result yourself.

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.

Everything (instructions, the report, all the messages) competes for space in the context window. When it fills up, older material is dropped or summarized (compaction), so early instructions and details can be lost. Even before it is full, very long contexts reduce accuracy and recall (context rot).

Remedies: start a fresh chat for a new task and restate the key instructions; give only the relevant parts of the report instead of all 400 pages; repeat important instructions or put them somewhere the app always loads (in Claude Code, a CLAUDE.md file); ask for a summary of the important points, then continue in a new chat from that summary.

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.

Claude Code gives the model tools (read a file, edit a file, run a shell command, and so on). The model never runs anything itself: it replies with a structured request to use a tool, Claude Code (the harness) runs the tool, often after asking you for permission, and puts the result back into the context. The model then decides the next step, and the loop repeats until the task is done. LLM + tools + loop = an agent.

Because the tools take real actions (changing files, running commands, possibly deleting things or reaching the internet), a wrong or misled model can cause real damage. That is why you review permission requests and diffs, keep your work in Git, and stay “in the loop”.

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?

Pre-training (predicting the next token over trillions of tokens of text) gives the model its broad knowledge of language, facts, and code, producing a base model: a powerful autocomplete. Post-training (fine-tuning on example conversations, RLHF, and reinforcement learning on checkable tasks) teaches it to follow instructions, answer helpfully, reason through problems, and refuse harmful requests, producing an assistant model.

A base model alone is a poor chatbot because it simply continues text: given a question, it might continue with more questions or ramble instead of answering.

Question 19 · True or false

“If you set the temperature to 0, the model’s answers will always be correct.”

False. Temperature only controls how random the choice of the next token is. At temperature 0 the model mostly picks its single most likely token, but the most likely answer can still be wrong (a hallucination is often a confident, “likely” answer). Even at temperature 0, outputs are not guaranteed to be identical every time, and Anthropic’s newest models don’t let you change the temperature at all.

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.

  1. Low effort: a simple, well-defined task. (b) High (or higher) effort: a hard, multi-step problem where careful reasoning pays off. (c) Low effort: a quick lookup.

The trade-off: more effort means more thinking, which usually improves quality on hard problems but is slower and uses more tokens (more cost, or more of your usage limit). On easy tasks, extra thinking mostly wastes time and tokens.

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.

Answers vary. A good answer records the app, the model the app says it is using, the assistant’s claims, and the official values with a link and date. They may not match because a model only “knows” about itself what was in its training data or system prompt; it can be unsure or wrong about its own name, version, or cutoff, and the docs may distinguish a “reliable knowledge cutoff” from a broader “training data cutoff”. The official documentation is the source to trust.

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.

Results vary by model. (The correct count for the example: “Mississippi’s” has 5 s’s and “assistants” has 4, so 9 in total.) A good answer reports both attempts and connects any difference to tokens: when the word is spelled out letter by letter, each letter becomes visible to the model as a separate token, so counting gets easier. If the assistant ran code to count, note that this is a tool, which is another reliable workaround.

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.

Answers vary. A good write-up states the app, model, and levels used; reports the response times; checks whether each answer is actually correct; and draws a conclusion about the trade-off (higher effort is usually slower but more reliable on hard problems, while on easy problems the difference may be small). If the app doesn’t offer an effort setting, say so and describe what it offers instead.

© 2025-2026 Y. Rosenthal. All rights reserved.