AI & ML Quick Path

Part 5: Large language models

How large language models work

Learning goal. Explain pretraining, next-token prediction, context windows, instruction tuning, open-weight versus hosted models, and why LLMs sometimes make things up.

In plain words

A large language model (LLM) is a decoder-only transformer with billions of weights. It is built in stages. Pretraining: the model reads a huge amount of text and learns one skill, predicting the next token (word piece). To do that well it has to absorb grammar, facts, reasoning patterns and code. Instruction tuning (supervised fine-tuning) then trains it on examples of questions and good answers, and preference tuning (such as RLHF or DPO) makes it prefer helpful, safe answers. When you chat, the model repeatedly predicts the next token, adds it to the text, and predicts again. The context window is how much text (your prompt, documents, conversation and its answer) it can consider at once. Hosted models are used through a provider's API; open-weight models can be downloaded and run on your own hardware. Because the model generates likely text rather than looking facts up, it can hallucinate: produce fluent, confident statements that are wrong. Giving it real documents (RAG, Part 6) and checking outputs reduce this.

Diagram Chatting with a model: one token at a time
  1. InputText so farYour prompt, then the tokens already written
  2. ModelModel predictsA likelihood for every possible next token
  3. ProcessPick one tokenChoose the next token from those likelihoods
  4. OutputAdd to textThe new token joins the text so far

Repeat until the model produces a stop token

The answer is built token by token, each new token joins the text.

Diagram What shares the context window
  1. Your prompt20%
  2. Documents40%
  3. Conversation25%
  4. Model's answer15%

Illustrative proportions only. The real split changes with every request.

Diagram Hosted model versus open-weight model

Hosted model

  • Used through a provider's API
  • Runs on the provider's hardware

Open-weight model

  • Weights can be downloaded
  • Runs on your own hardware

Real-world example

The Morgan Stanley case describes an internal assistant that answers advisors' questions from the firm's own knowledge, rather than relying only on what the model learned during pretraining. That is the standard industry pattern: a general LLM plus company documents.

Diagram General model plus company documents
  1. InputAdvisor's questionA question typed in plain language
  2. ProcessFind firm documentsLook up the relevant internal knowledge
  3. ModelModel reads bothThe question and the documents together
  4. OutputAnswerBased on the firm's own knowledge

The pattern from the case: the model answers from the firm's own knowledge.

How it works

  1. Pretrain: predict the next token on trillions of tokens of text (very expensive; done by model builders).
  2. Instruction-tune: learn to follow requests from example conversations.
  3. Preference-tune: learn which answers people prefer.
  4. Use: your prompt goes in; the model predicts one token at a time until it stops.
  5. Ground and check: supply documents and tools, validate outputs, keep people in the loop for important decisions.
Diagram Building the model versus using it

Building (model builders)

  1. ProcessPretrainPredict the next token on huge text
  2. ProcessInstruction-tuneLearn from example questions and answers
  3. ProcessPreference-tunePrefer helpful, safe answers
  4. ModelTrained modelReady to chat

Using (your system)

  1. InputYour promptPlus documents and tools
  2. ModelModel predicts tokensOne token at a time until it stops
  3. ProcessCheck the outputValidate, keep people in the loop
  4. OutputAnswerUsed for the decision

Model builders do the first lane. You mostly work in the second.

Diagram Skills stacked on top of next-token prediction
  1. ProcessPreference tuningPrefers helpful, safe answers
  2. ProcessInstruction tuningFollows requests from example conversations
  3. ModelPretrainingLearns grammar, facts, reasoning patterns and code by predicting the next token

Each stage adds behaviour on top of the one below.

Context window

The context window is the most text, counted in tokens, that the model can look at in one request. It holds your instructions, the documents you add, the conversation so far and the answer being written. When it is full, something has to go: the provider may refuse the request, or your code must cut the oldest or least useful text. Cut text is simply not seen by the model. Longer contexts also take more time and memory, because the model has to relate every token to the others. So send only what the task needs. Look up the limit for your model; do not assume one.

Diagram When the context window fills up
  1. InputPrompt, documents and chatEverything counted in tokens
  2. ProcessOver the limit?Compare the total with the model limit
  3. RiskCut or summariseDrop old turns, keep what matters
  4. ModelModel sees what is leftText that was cut is not seen
  5. ProblemLonger input costs moreMore time and more memory

Embeddings

An embedding is a list of numbers that stands for the meaning of a piece of text. Texts with similar meaning get similar lists, so a program can find related passages by comparing them. Search by meaning and RAG depend on this. See Embeddings.

Diagram Text turned into embeddings
ModelEmbeddingA list of numbers for the meaning
  • InputA sentenceBecomes one embedding
  • InputA document chunkBecomes one embedding
  • InputA search queryBecomes one embedding
  • OutputSimilar meaningEmbeddings end up close together

How transformers work

A transformer is the design of neural network inside modern LLMs. Its key idea is attention: for each token, the model weighs which other tokens in the context matter most for it. Many layers of this, stacked, let the model use the whole context when it predicts the next token. See Transformers.

Diagram Inside a transformer, simply
  1. OutputNext-token scoresThe output: a score for every possible token
  2. ModelMany repeated blocksEach block applies attention and then a small network
  3. ProcessToken embeddingsEach token becomes a list of numbers
  4. InputInput tokensThe text split into pieces

Read from the bottom up.

Python example

The simplest possible "language model" counts which word follows which (a bigram model). Real LLMs learn far richer patterns, but the next-word idea is the same:

Output — Output of quickpath.py bigram (executed)
P(next | 'the') = {'support': 0.14, 'ticket': 0.29, 'team': 0.29, 'customer': 0.29}
greedy generation: the ticket . the ticket . the

Always picking the single most likely word ("greedy" decoding) loops: "the ticket . the ticket". This is why real systems sample with settings such as temperature (next lesson).

Python — count next words (excerpt)
for a, b in zip(text, text[1:]):
    counts[a][b] += 1

.NET (C#) example

The same bigram model in plain C#:

C# — Program.cs
var text = "the support team answers the ticket . the team closes the ticket . the customer thanks the team . the customer opens a ticket ."
    .Split(' ');
var counts = new Dictionary<string, Dictionary<string, int>>();
for (int i = 0; i < text.Length - 1; i++)
{
    if (!counts.TryGetValue(text[i], out var next)) counts[text[i]] = next = new();
    next[text[i + 1]] = next.GetValueOrDefault(text[i + 1]) + 1;
}
var afterThe = counts["the"];
int total = afterThe.Values.Sum();
foreach (var (word, n) in afterThe) Console.WriteLine($"P({word} | the) = {(double)n / total:F2}");

To call a real LLM from .NET, see Calling an LLM and Running models locally.

Applied Maths - Statistics

Short, optional reading. Open a topic to see the formula, what it means and two worked examples. The numbers are small, made-up values chosen so you can check them by hand.

Next-token probability from counts (what "predict the next token" means)

Where it is used: the bigram model in this lesson, which estimates the next word from how often each word followed the current one.

Next-word probability from counts

P(next | current) = times "next" followed "current" ÷ times "current" was followed by anything

P(next | current) reads "the probability of next, given current".

In plain words: among all the times the model saw the current word, what share were followed by this next word?

Example 1: after "the". In the lesson's tiny text, "the" is followed 7 times: support 1, ticket 2, team 2, customer 2. P(ticket | the) = 2 ÷ 7 = 0.29. P(support | the) = 1 ÷ 7 = 0.14.

Example 2: a sum check. All the probabilities for one starting word must add up to 1: 1/7 + 2/7 + 2/7 + 2/7 = 7/7 = 1. If your counts do not add up like this, a count is missing or double-counted. A real LLM does the same with a softmax over its whole vocabulary.

Context window budget (adding up tokens)

Where it is used: deciding what fits in one request, because the prompt, the documents, the history and the answer share the same window.

Context budget

room for the answer = window size - (instructions + documents + conversation history)

All values are counted in tokens.

In plain words: everything you send, plus the answer, must fit inside the window.

Example 1: it fits. Window 8,000 tokens. Instructions 500, documents 5,000, history 1,500 add up to 7,000. Room for the answer = 8,000 - 7,000 = 1,000 tokens.

Example 2: it overflows. Same window, but documents are 7,000 tokens. Total input = 500 + 7,000 + 1,500 = 9,000, which is 1,000 over the window. Your code must cut at least 1,000 tokens, and more if you want room for an answer.

Maths behind it

  • Probability and conditional probability (next-token prediction)
  • Cross-entropy loss (pretraining objective)
  • Transformers and attention
  • Scaling laws (how quality grows with data and model size)

Common mistakes

  • Treating the model as a database of facts; it generates likely text.
  • Pasting more text than the context window holds; the start gets cut off or ignored.
  • Assuming the context window is unlimited; long inputs cost more and may be cut.
  • Confusing embeddings (meaning as numbers) with tokens (text pieces).
  • Assuming a bigger model always wins; smaller models are often good enough, faster and cheaper.
Diagram Why models make things up, and what helps

Model alone

  • Generates likely-sounding text
  • Does not look facts up
  • Can hallucinate with confidence

Model with grounding

  • Real documents supplied (RAG)
  • Outputs are checked
  • People review important decisions

Quick check

  1. What single task does pretraining teach?

    Show answer

    Predicting the next token in text.

  2. Why can an LLM state something false with confidence?

    Show answer

    It produces likely-sounding text; it does not check facts unless your system gives it sources and verifies output.

  3. What is the context window?

    Show answer

    The maximum amount of text (prompt, documents, conversation and answer) the model can consider at once.

Completion

Evidence of completion. A one-page explanation, for a non-technical colleague, of how a chat assistant produces an answer and why it can be confidently wrong.

Saved in this browser only. See all progress.