How Large Language Models Work: Tokens, Context, Generation, and Hallucinations

Understand token prediction, context windows, temperature, reasoning modes, and why models hallucinate — plus how to distinguish raw generation from web search and RAG.

Source reliability tiers: [Verified] = backed by official documentation or trusted primary sources; [Community Consensus] = widely observed by community users but not officially confirmed; [Unverified / Speculative] = inferred or not yet independently verified — treat with caution.

🎯 What you will learn: What is a token? Why do models sometimes fabricate facts with complete confidence? What do terms like context window, temperature, parameter count, and reasoning model actually mean in practice?


1. The One-Sentence Summary

[Verified] Today's mainstream Large Language Models (LLMs) are built on the Transformer architecture (Vaswani et al., 2017, arXiv:1706.03762). Their core operation is straightforward: given all preceding tokens, predict the probability distribution of the next token, sample one, and repeat.

LLM Pipeline

💡 Analogy: Think of an LLM as an extraordinarily well-read autocomplete improviser. It is not querying a structured database of facts; it is continuing a text sequence based on learned statistical patterns — which explains both why it writes fluent prose and why it can invent nonexistent academic papers. [Community Consensus]


2. Seven Core Concepts

Concept Plain-English Explanation Where You Encounter It Reliability Tier
Token The smallest unit of text a model processes — often a word fragment, a single CJK character, or a punctuation mark API billing is priced per token; "128K context" measures token count Verified (standard across vendor docs)
Parameter Count The number of tunable internal weights inside the model (e.g., 7B = 7 billion parameters) Choosing models for local deployment (see Local Model Deployment Costs & Suitability: Hardware, VRAM, Power, and Time) Verified
Context Window The maximum number of tokens (input + output combined) the model can attend to in a single request Long document analysis, extended chats, coding agents reading repositories Verified
Temperature Controls sampling randomness: lower values produce deterministic, conservative outputs; higher values encourage variety and creativity API parameters, roleplay frontends (SillyTavern Illustrated Guide: Connections, Character Cards, World Info, and Prompts) Verified
Embedding Vector A numerical list representing text in high-dimensional space where semantically similar texts sit close together RAG knowledge base retrieval Verified
Attention The mechanism that lets each token weigh its relevance against every other token in the context The core building block of the Transformer architecture Verified (original paper)
Hallucination When a model confidently outputs false or fabricated statements Every domain, especially factual lookups and citations Community Consensus

2.1 A Quick Token Experiment

[Community Consensus] The same passage expressed in Chinese, English, or source code splits into different numbers of tokens. Different vendors also use different tokenizers, so identical word counts can produce noticeably different API token bills. Most major providers offer interactive web tokenizer tools where you can inspect how your text is split.


3. How Models Learn (Overview; see Pre-training, Fine-tuning, LoRA, and RAG: Concepts and Differences)

Diagram 1

4. What Makes Reasoning / Thinking Models Different?

[Verified] Many providers now offer a "thinking" or "reasoning" mode: before emitting its final response, the model generates an explicit internal chain of reasoning (which may be visible or summarized in the UI). Examples include DeepSeek's thinking mode, OpenAI's reasoning models, and Claude's extended thinking (documented in each provider's API reference).

Standard Mode Thinking / Reasoning Mode
Latency Fast Slower
Cost Lower Higher (reasoning tokens are billed as output tokens)
Best suited for Casual chat, rewriting, straightforward Q&A Math, complex debugging, multi-step architecture planning
Inside an Agent Routine edits Complex multi-file tasks; the effort setting in DSH and Codex adjusts reasoning budget (see DSH Daily Workflow, Profiles, and Advanced Usage / Codex Local Guide: Installation, Permissions, Project Rules, and Troubleshooting)

[Community Consensus] "Thinking longer ≠ guaranteed accuracy," though it significantly improves success rates on hard multi-step problems.


5. Why Models Hallucinate

Diagram 2

Practical ways to reduce hallucinations [Community Consensus]:

  1. Enable web search and require source links (then click through to verify the primary source yourself);
  2. Paste the reference material directly into the prompt context (the core idea behind RAG);
  3. Explicitly instruct the model: "If the answer is not in the provided text or you are unsure, state that you do not know";
  4. Test your setup using the honesty prompts in Hands-On Model Evaluation: Controlled Conditions, Test Bank, and Rubric.

6. A Model Is Not the Same as a Product

Layer Examples What It Determines
Model Weights DeepSeek V4, GPT series, Claude series Raw reasoning ceiling
API / Serving Layer Official APIs, cloud hosts, third-party relays Pricing, throughput, uptime, context limits
Harness / Application Shell ChatGPT web app, DSH, Codex, SillyTavern Tools, memory management, permissions, day-to-day UX

[Author's View] The exact same model weights placed inside a different harness can feel night-and-day different in practice. That is why Hands-On Model Evaluation: Controlled Conditions, Test Bank, and Rubric emphasizes evaluating the full combination rather than a bare model name in isolation.


7. Unverified Items and Practical Caveats

  • Exact parameter counts and architectural details of proprietary models: rarely disclosed officially; treat rumored numbers with skepticism.
  • Internal implementation of "thinking mode" differs across vendors; the description here is a high-level summary.
  • Token-to-character ratios vary by language and vendor tokenizer.

Hands-On Practice: Step-by-Step Verification

Goal: Distinguish pure model generation from web search and knowledge-base retrieval.

The sections above explained tokens, context windows, and hallucinations. Follow the steps below to observe how grounding changes model behavior in practice.

  1. Write a short paragraph of your own containing specific details, paste it into the prompt, ask a question restricted strictly to that passage, and verify the answer sentence by sentence.

  2. Start a fresh chat without the passage and ask the same question. Observe whether the model admits missing context or guesses, and compare both outputs side by side.

  3. Add a verifiable factual claim into your passage, ask the model to quote the exact supporting sentence, and check whether the quoted sentence matches verbatim.

  4. Open your API or platform usage dashboard and inspect the input and output token counts separately. Notice that token counts do not map 1:1 to words or characters.

  5. Draw three separate boxes for the core model, web search tools, and local knowledge retrieval; even when an assistant returns a citation link, always open the link to confirm the source text actually supports the claim.

Back to Tutorials Back to Learn →Back to top ↑
On this page
Tutorial diagram100%
Tutorial diagram