Barefoot AI HubTHE BAREFOOT FREELANCER
Start learning
◆ AI Foundations · Lesson 2 of 5

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.

⏱ 20 min📶 Beginner🧪 3-question check

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.

The barefoot freelancer sent invoice #1042.

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.

INPUTYour prompt“Write a tagline for…”
TOKENIZETokens[Write][ a][ tag]…
MODELNeural networkbillions of parameters
PREDICTNext-token odds“Fresh” 31% · “Your” 18%…
OUTPUTPick → append → repeatuntil done
Generation is a loop: predict one token, add it to the context, predict the next.

Training vs. inference

TrainingInference
What happensThe model learns from massive datasets by adjusting its parametersThe trained model is used to answer your prompts
Who does itAI labs, using thousands of GPUs over weeks or monthsYou — via an app, an API, or your own computer
CostMillions of dollarsFractions of a cent per request, or free when local
When knowledge is setUp to the training cutoff dateFixed — 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

SettingWhat it doesRecommended use
temperatureRandomness 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_tokensUpper limit on the length of the answer.Set it to avoid runaway costs; raise it for long documents.
System promptPersistent instructions that frame every reply (role, rules, format).Put business rules, tone and boundaries here.
top_pAlternative randomness control (nucleus sampling).Usually leave at default; adjust temperature instead.
Stop sequencesText 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:

  1. Grounding — paste the source document or connect a knowledge base (RAG, Lesson 08) and instruct: “Answer only from the provided text.”
  2. Permission to not know — “If the answer isn't in the document, say you don't know.”
  3. Asking for quotes or citations — the model must point to where it found each claim.
  4. Tools — give it search, a calculator or database lookups instead of relying on memory.
  5. 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 / proprietaryOpen-weight
ExamplesClaude, GPT, GeminiLlama, Qwen, Gemma, Mistral, DeepSeek, gpt-oss
How you use itThrough the provider's app or APIDownload and run anywhere — laptop, server, cloud
StrengthsUsually the most capable; no hardware needed; managedPrivacy, no per-token cost, offline, full control, customizable
Trade-offsData leaves your machine; usage costs; vendor changesNeeds 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 / 3

Q1Roughly how many English words fit in 1,000 tokens?

Q2You need an LLM to extract invoice numbers reliably. Which temperature fits best?

Q3Why does a chatbot not know your company's refund policy?

Finished this lesson?Your progress is saved in this browser.