Menu

LLMs course · Lesson 1 of 12

What Is a Large Language Model (LLM)? How LLMs Work, Simply Explained

What a large language model (LLM) is and how it works in plain words: next-token prediction, training, temperature and limits, with a tiny model you can run.

  • Beginner
  • Pillar guide
  • 6 min read
  • Updated Oct 2026
On this page
  1. How an LLM works in four steps
  2. See the idea in a tiny model
  3. How LLMs are trained
  4. What LLMs are good at, and where they fail
  5. Common misconceptions
  6. Key takeaway

A large language model (LLM) is a computer program that has learned to predict the next piece of text. Give it some words, and it works out which word (more precisely, which token) is most likely to come next. Then it adds that word and predicts again, and again, until the answer is finished. That simple loop, repeated with a very large neural network trained on a huge amount of text, is what lets an LLM write emails, answer questions, translate and write code.

“Large” refers to two things: the model has billions of internal numbers (called parameters), and it was trained on a very large amount of text, such as books, websites and code.

How an LLM works in four steps

  1. Text becomes tokens. Your prompt is split into small pieces called tokens, such as words or parts of words, and each one becomes a number. See tokens and tokenization.
  2. Tokens become vectors. Each token is turned into a list of numbers that represents its meaning, called an embedding.
  3. The model looks at the context. A neural network called a transformer uses a mechanism called attention to work out how every token relates to the others, so “bank” means something different in “river bank” and “bank account”.
  4. It predicts the next token. The model gives a probability to every possible next token, picks one, adds it to the text, and repeats.

See the idea in a tiny model

Real LLMs are enormous, but the core idea fits in a few lines. This tiny model “trains” by counting which word follows which in some example sentences, then predicts the next word from those counts:

import random
from collections import Counter, defaultdict

corpus = """the customer paid the invoice . the customer opened a ticket .
the agent closed the ticket . the agent sent the invoice .
the customer paid late . the agent closed the account .""".split()

# "Training": count which word follows which (a bigram model)
follows = defaultdict(Counter)
for current, nxt in zip(corpus, corpus[1:]):
    follows[current][nxt] += 1

def next_word_probs(word):
    counts = follows[word]
    total = sum(counts.values())
    return {w: round(c / total, 2) for w, c in counts.most_common()}

print("after 'the':     ", next_word_probs("the"))
print("after 'customer':", next_word_probs("customer"))
after 'the':      {'customer': 0.3, 'agent': 0.3, 'invoice': 0.2, 'ticket': 0.1, 'account': 0.1}
after 'customer': {'paid': 0.67, 'opened': 0.33}

After “customer”, the model has learned that “paid” is twice as likely as “opened”. An LLM does the same kind of thing, but instead of looking only at the previous word, it looks at the whole conversation, and instead of simple counts it uses a neural network with billions of parameters.

Now let the model write, one word at a time. Temperature controls how adventurous the choice is:

def generate(start, steps, temperature, seed):
    rng = random.Random(seed)
    words = [start]
    for _ in range(steps):
        probs = next_word_probs(words[-1])
        if not probs:
            break
        # temperature reshapes the probabilities: low = pick the likeliest, high = more variety
        weights = [p ** (1 / temperature) for p in probs.values()]
        words.append(rng.choices(list(probs), weights=weights)[0])
    return " ".join(words)

print("low temperature: ", generate("the", 6, temperature=0.1, seed=1))
print("high temperature:", generate("the", 6, temperature=2.0, seed=1))
low temperature:  the customer paid late . the customer
high temperature: the customer opened a ticket . the

At a low temperature, the model almost always takes the most likely word (“paid”). At a high temperature, less likely words get a real chance (“opened”). Real LLM APIs have the same setting: use a low temperature for factual or structured tasks, and a higher one for brainstorming.

How LLMs are trained

1. Pre-training. The model reads a huge amount of text and learns to predict the next token, over and over. This teaches it grammar, facts, reasoning patterns and coding styles. It is extremely expensive and done once by the company that builds the model. The result is a base model, which continues text but does not reliably follow instructions.

2. Instruction tuning. The base model is trained further on many examples of instructions with good answers, so it learns to follow requests.

3. Preference training. People (and sometimes other models) compare answers and choose the better one. The model is trained to prefer answers like the chosen ones, which makes it more helpful and safer. RLHF (reinforcement learning from human feedback) is the best-known method.

The result is the chat model you use through an app or API.

What LLMs are good at, and where they fail

Good at: writing and rewriting text, summarising, answering questions about text you give them, extracting fields from messy input, classifying, translating, and writing and explaining code.

Common limits:

  • Hallucinations. They can state false things confidently, because they generate likely-sounding text, not checked facts.
  • Knowledge cut-off. They do not know about events after their training data, or about your private data, unless you give it to them, for example with retrieval (RAG).
  • Context limits. They can only read a limited amount of text at once (the context window).
  • Counting and exact maths. These can go wrong, partly because of how text is split into tokens.
  • Variation. The same question can get different answers.

Knowing these limits is what lets AI engineers design around them: give the model the right information, check its outputs, and measure quality with test sets.

Common misconceptions

  1. “The model looks things up.” By itself, it does not; it generates from what it learned. Looking things up has to be built around it.
  2. “Bigger is always better.” Smaller models are often good enough, and much cheaper and faster.
  3. “It understands like a person.” It is very good at patterns in language; treat its output as a strong draft that still needs checking.

Key takeaway

An LLM is a next-token predictor: it turns text into tokens, works out the context with attention, and picks likely next tokens one at a time. Training on huge amounts of text, followed by instruction and preference training, turns that predictor into a helpful assistant. Next, learn how text is split up: tokens and tokenization.

By , Data Engineer and Founder of DataDank · Last reviewed Oct 2026 · Concepts that apply to all current LLMs. The example runs on plain Python 3 with no packages; its output is real.

Progress is saved in this browser only. No account needed.

Search
Filter by type