Skip to content
TutorialBeginner11 min read

How Large Language Models Work

Tokens, context windows, training and inference explained without maths, plus why models hallucinate and how to reduce the risk.

AIAI Skills Hub ResearchResearch team Published Mar 16, 2026Updated May 22, 2026 Reviewed May 22, 2026 11 min read

Prerequisites

  • • Read the AI introduction or know what a model is

What you will learn

  • Explain tokens, context windows and next-token prediction
  • Understand why models hallucinate and why citations need checking
  • Choose between a longer prompt, retrieval and a smaller task

Next-token prediction

A large language model (LLM) is trained to predict the next token in a sequence. A token is a chunk of text, often a word or part of a word. “Artificial intelligence” might be three or four tokens depending on the tokenizer.

At inference time, the model receives your prompt and produces a probability distribution over possible next tokens. It samples or selects one, appends it, and repeats. The result looks like reasoning because the training data contains reasoning-like text.

The context window

The context window is the amount of text the model can consider at once, including the prompt, the conversation and the output. Models differ in size, and a larger window does not guarantee better attention to every detail.

Practical consequences:

  • Very long documents can be truncated or under-attended.
  • Instructions at the start of a long conversation may be forgotten.
  • Splitting a task into focused chunks usually beats one giant prompt.
  • Keep the most important constraints near the task.

Why models hallucinate

A model generates plausible continuations. When it lacks a fact, it may fill the gap with something that sounds right. It does not have an internal truth check.

Hallucination risk rises when you ask for:

  • specific numbers without providing data,
  • citations or quotes without sources,
  • recent events beyond the training data,
  • niche details with little coverage in the training set.

Reduce the risk by supplying source material, requiring citations, asking for uncertainty, and verifying important claims. Retrieval (searching a document set and giving the model the relevant passages) is often more reliable than asking the model to remember.

Training and inference are different

Training is the expensive process of adjusting model weights on data. Inference is running the trained model on your prompt. When a product says it does not train on your data, it usually means your conversations are not used to update the weights. It does not mean the model has no access to your prompt at inference time.

A useful mental model

Think of an LLM as a very well-read colleague who speaks confidently, cannot check the internet unless given a tool, and forgets the beginning of a long meeting. You would still review their work.

Exercises

Do these before moving on. Reading is not the same as being able to apply it.

Exercise 1

Estimate token cost

Take a 600-word document and estimate its token count at roughly 0.75 words per token. Compare with a tokenizer if you have access.

Exercise 2

Spot context loss

In a long chat, ask the assistant to restate the original constraints. Note where it drifts.

References

Finished this tutorial? Continue with a guided path.

Browse learning paths →

Related resources