I9 · Generative AI and Large Language Models in Practice

What Is a Large Language Model?

A Large Language Model (LLM) is a neural network, usually a Transformer, trained on huge amounts of text to predict the next token. Repeating that prediction, one token at a time, produces fluent text. "Large" refers to its billions of parameters and the amount of data used to train it.

10 min Intermediate · Sublevel Advanced

What you need: The earlier topics of this module.

Definition

ChatGPT doesn't look answers up. At each step, it predicts the next piece of text.

A Large Language Model (LLM) is a neural network, usually a Transformer, trained on huge amounts of text to predict the next token. Repeating that prediction, one token at a time, produces fluent text. "Large" refers to its billions of parameters and the amount of data used to train it.

In simple words

Your phone's keyboard suggests the next word after "Happy", probably "birthday". An LLM is the same idea, made vastly more powerful. It has read so much text that it can continue almost any sentence, which turns out to be enough to answer questions, write code and summarise reports.

Overview

  • A large language model (LLM) is a neural network, usually a transformer, trained on huge amounts of text to predict the next token.
  • "Large" refers to billions of parameters and trillions of training tokens.
  • By predicting the next token repeatedly, an LLM can answer questions, write, summarise, translate and code.
  • Pre-training is followed by instruction tuning and alignment with human feedback.
  • LLMs can be wrong with confidence (hallucination) and reflect biases in their data.
  • Well-known LLMs include GPT, Claude, Gemini, Llama and Mistral; ChatGPT (November 2022) made them mainstream.

Why it matters

LLMs are behind most of today's AI products. Knowing how they work tells you what to expect: why they make things up, why they can be wrong in a confident voice, why long inputs cost more, and why they don't know recent events.

Key terms

Large language model (LLM)
A very large neural network trained on text to predict the next token.
Token
A chunk of text, often part of a word, that the model reads and writes.
Transformer
The neural network architecture, based on attention, used by almost all LLMs.
Pre-training
The first, enormous training stage on general text.
Fine-tuning
Further training on smaller, targeted data to shape behaviour.
Context window
The maximum number of tokens the model can consider at once.

Types

LLMs can be grouped into several types by architecture, training stage and access. These are the main ones.

1. Base (pre-trained) models

Models trained only to predict the next token on large text collections.

  • How it works: They continue text in the style of their training data but do not reliably follow instructions or hold conversations.
  • Example: Given "The capital of France is", a base model continues "Paris, which is…".
  • When to use it: Use them as starting points for fine-tuning and research; most users want instruction-tuned models instead.

2. Instruction-tuned and chat models

Base models further trained to follow instructions and converse helpfully and safely.

  • How it works: Supervised fine-tuning on example conversations and reinforcement learning from human feedback (RLHF) teach the desired behaviour.
  • Example: ChatGPT, Claude and Gemini answering questions in a chat window.
  • When to use it: Use them for nearly all practical applications, from writing help to customer support.

3. Reasoning models

Models trained to work through problems step by step before answering.

  • How it works: They spend extra computation generating intermediate reasoning, often trained with reinforcement learning on problems with checkable answers.
  • Example: Solving a multi-step maths or programming problem more reliably than a standard chat model.
  • When to use it: Use them for complex maths, coding and analysis, accepting higher cost and slower answers.

4. Open-weight and closed models

Open-weight models publish their parameters; closed models are available only through an API or app.

  • How it works: Open-weight models such as Llama and Mistral can be run and fine-tuned on your own hardware; closed models are managed by their providers.
  • Example: A hospital running an open-weight model on its own servers so patient data never leaves the building.
  • When to use it: Choose open weights for privacy, control and customisation; choose closed models for top capability without managing infrastructure.

5. Small and specialised language models

Compact models, from under a billion to a few billion parameters, often tuned for a domain or device.

  • How it works: Distillation, quantisation and focused training data make them cheap and fast while keeping useful skills.
  • Example: An on-device model on a phone suggesting replies offline; a model trained for legal Hindi.
  • When to use it: Use them when cost, speed, privacy or offline use matters more than broad general knowledge.

Explained in detail

Prediction, repeated

At its core, an LLM does one thing: given a sequence of tokens, it outputs a probability for every possible next token. To write a reply, the system picks a token from that distribution, appends it, and asks again, hundreds of times. Fluent paragraphs, working code and step-by-step explanations all emerge from this loop. Because predicting the next word well across billions of documents requires capturing grammar, facts, styles and patterns of reasoning, the model's internal representations end up encoding a great deal about language and the world, though imperfectly.

How LLMs are built

StageDataResult
Pre-trainingTrillions of tokens of web pages, books, code and moreA base model that continues text but does not follow instructions well
Instruction tuningThousands of example instructions with good answersA model that responds helpfully to requests
Preference training (for example RLHF)Human or AI ratings comparing answersA model steered towards helpful, honest and harmless answers

Key milestones

  • 2017: the transformer architecture is introduced in the paper "Attention Is All You Need" by researchers at Google.
  • 2018: BERT and the first GPT show that pre-training on large text then fine-tuning works across many tasks.
  • 2020: GPT-3, with 175 billion parameters, shows that a large enough model can do new tasks from a few examples in the prompt.
  • 2022 onwards: chat assistants built on instruction-tuned LLMs reach hundreds of millions of users; open-weight models and multilingual models, including for Indian languages, follow.

Strengths and limits

LLMs are strong at drafting, summarising, translating, explaining, brainstorming and writing code. Their limits follow from how they work. They produce plausible text, not verified truth, so they can state false facts confidently, a behaviour called hallucination. Their knowledge stops at a training cutoff unless they are connected to search or documents. They can be inconsistent, sensitive to phrasing, and can reflect biases in their training data. Each response uses significant computation. Good use of LLMs therefore combines their fluency with grounding in reliable sources, clear prompts and human checking for anything important.

"Large" refers both to the number of parameters, from billions to trillions, and to the size of the training data. Bigger models trained on more data have generally become more capable, a pattern studied as scaling laws.

How it works

  1. Your text is split into tokens: common words or pieces of words, each with a number.
  2. Each token becomes a vector, called an embedding, that captures something of its meaning.
  3. Transformer layers use attention to let each token take context from the others.
  4. The model outputs a probability for every possible next token.
  5. One token is chosen, added to the text, and the process repeats.

Two stages of training

StageWhat happens
Pre-trainingIt reads huge amounts of text and learns to predict the next token. It's now a powerful autocomplete.
Post-trainingPeople's examples and ratings (RLHF and related methods) teach it to follow instructions and be helpful.

The maths

What the model computes at each step
The score the network gives token k
Every token in the vocabulary

In code

The smallest possible language model: count which word follows which, then generate. A real LLM learns far richer patterns, but the job is the same.

tiny_lm.py
import random
from collections import Counter, defaultdict

text = "the cat sat on the mat and the cat saw the dog and the dog sat on the mat"
words = text.split()

follows = defaultdict(Counter)
for current, following in zip(words, words[1:]):
    follows[current][following] += 1

print("After 'the':", dict(follows["the"]))

random.seed(3)
word = "the"
output = [word]
for _ in range(7):
    options = follows[word]
    word = random.choices(list(options), weights=list(options.values()))[0]
    output.append(word)
print(" ".join(output))

Output

After 'the': {'cat': 2, 'mat': 2, 'dog': 2}
the cat saw the mat and the cat

Common mistakes

  • Believing an answer because it's well written. Fluency isn't accuracy.
  • Assuming it remembers you. Unless the product adds memory, each new chat starts fresh.
  • Thinking it counts letters or does arithmetic reliably. Token-based models can stumble at both.

Check yourself

Question 1

1. What is an LLM fundamentally trained to do?

Question 2

2. Why do LLMs sometimes state false facts confidently?

Summary

  • An LLM is a Transformer trained to predict the next token.
  • Generation is that prediction, repeated.
  • Pre-training gives knowledge, and post-training gives helpful behaviour.

Read it. Now try it yourself.

Open the interactive activity

Finished reading?

Sources: Stanford CS224N · Hugging Face LLM Course