02How Large Language Models Work
Tokens, context windows, training, temperature and hallucination — the handful of concepts that explain almost every LLM behavior you'll see.
What you'll learn
- Explain tokens and why they matter for cost and limits
- Understand the context window as the model's working memory
- Know the difference between training and inference
- Use temperature and system prompts deliberately
- Compare open-weight and closed models
Step 1 — Text becomes tokens
LLMs don't read letters or whole words. They read tokens: chunks of text, usually a word or part of a word. In English, one token averages about ¾ of a word, so 1,000 tokens ≈ 750 words. Filipino, emoji and code often use more tokens per word.
Illustrative tokenization — each colored chip is one token. Exact splits vary by model.
Tokens matter because everything is measured in them: API pricing (per million input and output tokens), context limits and speed.
Step 2 — Tokens go into the context window
The context window is everything the model can “see” at once: the system prompt, the conversation so far, any documents you pasted, tool results — and its own answer as it writes. Modern models have windows from about 8,000 tokens (small local models, by default) to hundreds of thousands or more (frontier cloud models).
Step 3 — The model predicts the next token, over and over
An LLM is fundamentally a next-token predictor. Given all tokens so far, it calculates a probability for every possible next token, picks one, appends it, and repeats. That simple loop — run by a neural network with billions of parameters — produces essays, code and reasoning.
Training vs. inference
| Training | Inference | |
|---|---|---|
| What happens | The model learns from massive datasets by adjusting its parameters | The trained model is used to answer your prompts |
| Who does it | AI labs, using thousands of GPUs over weeks or months | You — via an app, an API, or your own computer |
| Cost | Millions of dollars | Fractions of a cent per request, or free when local |
| When knowledge is set | Up to the training cutoff date | Fixed — unless you provide new info in the context |
Most modern models then go through extra training steps — instruction tuning and reinforcement learning from human (and AI) feedback — which teach them to follow instructions, be helpful and refuse harmful requests. That's why a chat model answers questions rather than just continuing your text.
The settings you control
| Setting | What it does | Recommended use |
|---|---|---|
temperature | Randomness when picking tokens. 0 = most predictable, higher = more varied. | 0–0.3 for extraction, classification, facts. 0.7–1.0 for brainstorming and creative writing. |
max_tokens | Upper limit on the length of the answer. | Set it to avoid runaway costs; raise it for long documents. |
| System prompt | Persistent instructions that frame every reply (role, rules, format). | Put business rules, tone and boundaries here. |
top_p | Alternative randomness control (nucleus sampling). | Usually leave at default; adjust temperature instead. |
| Stop sequences | Text that ends generation when produced. | Useful for structured, templated outputs. |
Why models hallucinate — and how to reduce it
Because the model's job is to produce the most plausible continuation, it will produce something plausible even when it doesn't actually know. It has no built-in “I'm unsure” signal unless trained and prompted to express one. You reduce hallucination by:
- Grounding — paste the source document or connect a knowledge base (RAG, Lesson 08) and instruct: “Answer only from the provided text.”
- Permission to not know — “If the answer isn't in the document, say you don't know.”
- Asking for quotes or citations — the model must point to where it found each claim.
- Tools — give it search, a calculator or database lookups instead of relying on memory.
- Verification — a human or a second AI pass checks critical outputs.
Reasoning (“thinking”) models
Many current models can spend extra tokens “thinking” before answering — working through a problem step by step internally. This improves results on math, logic, planning and coding, at the cost of time and tokens. Use reasoning modes for complex analysis and agent planning; skip them for simple drafting where speed matters.
Open-weight vs. closed models
| Closed / proprietary | Open-weight | |
|---|---|---|
| Examples | Claude, GPT, Gemini | Llama, Qwen, Gemma, Mistral, DeepSeek, gpt-oss |
| How you use it | Through the provider's app or API | Download and run anywhere — laptop, server, cloud |
| Strengths | Usually the most capable; no hardware needed; managed | Privacy, no per-token cost, offline, full control, customizable |
| Trade-offs | Data leaves your machine; usage costs; vendor changes | Needs hardware; smaller models are less capable; you maintain it |
You don't have to choose one. A common setup: open-weight models locally for private, high-volume or simple tasks (Track 4), frontier cloud models for the hardest work.
Key takeaways
- Tokens (~¾ word) are the unit of cost, speed and limits.
- The context window is the model's only working memory — if it's not in there, the model doesn't know it.
- LLMs generate one token at a time by predicting what's most plausible, which is why they can hallucinate.
- Low temperature for facts and data; higher for creativity. Ground answers in sources to cut hallucinations.
- Open-weight models give privacy and zero per-token cost; closed models usually lead on raw capability.
Knowledge check
0 / 3Q1Roughly how many English words fit in 1,000 tokens?
A token averages about ¾ of an English word.
Q2You need an LLM to extract invoice numbers reliably. Which temperature fits best?
Low temperature makes output more consistent and predictable — ideal for extraction.
Q3Why does a chatbot not know your company's refund policy?
Models only know their training data plus what's in the context. Provide the policy.