Skip to content
Brandregard
Rows of black server racks with coloured lights in a NASA machine room, inside a Brandregard frame
Cover: NASA's Discover supercomputer, framed. Shown for illustration; it is not tied to any model in this article. Image: NASA/Pat Izzo
Guide

How Do LLMs Work? From Your Prompt to a Reply

  • Rowan Ledger
  • 5 min read
  • Facts checked

In a basic next-token setup, a large language model (LLM) writes text one token at a time. A token is usually a word or word fragment, though punctuation can count too.

At each step, the model assigns probabilities to possible next tokens; a separate decoding rule chooses one. The chosen token joins the text, and generation repeats. Pretraining starts from scratch; fine-tuning is further training after pretraining.

What are large language models?

Sources describe what makes such a model "large" in slightly different words. A research survey first posted in 2023 says researchers coined the name for pretrained language models of significant parameter scale, meaning a great many adjustable values.

The Hugging Face glossary uses the term for transformer language models trained on a large amount of data. It adds that they tend to have many learnable parameters. A Transformer is one model design discussed below.

What happens to the words you type?

Your text reaches the model as tokens. The Hugging Face glossary says a token is usually a word, but can be punctuation or a fragment of a less common word.

Why split words at all? In 2015 and 2016 machine translation research, a study handled rare and unknown words by writing them as chains of smaller units. Its authors adapted byte pair encoding, a compression method, so a fixed list of different-length pieces could cover an open-ended vocabulary.

A row of tall black server cabinets with small coloured lights beside a perforated machine room floor
Photo for illustration: one row of NASA's Discover supercomputer. It is not tied to any model in this article. Image: NASA/Pat Izzo

Next, the original 2017 Transformer turned tokens into vectors, which are lists of numbers. It used embeddings, learned during training, to make that conversion. It also added information about each token's position, so that word order could count.

How does attention connect the words?

The Transformer was introduced in 2017 as a design built on attention alone, and its authors tested it on translation. Beltagy and colleagues reported in 2020 that memory for full self-attention grew with the square of the input length.

The Hugging Face course says attention layers can give some words more weight while representing another word. Consider this imagined input: "The trophy did not fit in the bag because it was too big". In this illustration, a layer might give "trophy" or "bag" more weight while representing "it" than it gives "the."

This is a simplified illustration, not a diagram of a current chat product’s internals.

How does the model choose the next token?

In the original Transformer, the final step turned the model's internal result into a probability for each candidate next token. Choosing among those candidates is a separate step, called decoding.

Two basic choices differ like this:

  • Greedy search takes the most likely token every time, and it is the default in Hugging Face's Transformers library.
  • Sampling, as that library describes it, draws a token at random according to the probabilities, so less likely tokens can also appear.

For example, imagine a model has produced "The cat sat on the". Suppose it rates "mat" high and "sofa" lower. A top-choice rule would take "mat", while a random draw weighted by those ratings could take "sofa".

The chosen token then joins the text, and the original Transformer read its earlier output when producing the next piece.

The library guide says greedy search can begin repeating itself on longer outputs. A separate study found a different problem with unrestricted sampling.

A 2019-2020 study examined open-ended writing with GPT-2 models. It found that maximizing likelihood, including with beam search, could produce bland, incoherent, or repetitive text.

Unrestricted random sampling failed too: in the paper's GPT-2 Large example, it produced gibberish. The authors proposed nucleus sampling, which drops the unreliable low-probability tail and then samples from the remaining tokens.

How is a large language model trained?

The Hugging Face course says pretraining starts from scratch, with random weights (adjustable numbers) and no prior knowledge. It adds that this stage usually draws on a very large amount of data.

For the Transformer language models in its course, Hugging Face says the data is raw text and the training target comes from that input itself. People therefore do not have to label the data at this stage.

The course gives next-word prediction as an example task: the model reads the preceding words and predicts what follows, without seeing later words.

Fine-tuning comes after pretraining. One documented recipe is InstructGPT, described in a 2022 paper. Its authors first trained GPT-3 further on demonstrations of the behavior they wanted, collected from human labelers.

They then collected rankings of model outputs and used them in reinforcement learning from human feedback, a further training stage guided by those judgments.

In that study, the larger model did not win one comparison judged by people. In the paper’s 2022 human evaluations on the authors’ prompt set, people preferred outputs from 1.3B-parameter InstructGPT to those from the 175B GPT-3 baseline. The authors add that InstructGPT still made simple mistakes.

In our view, this single comparison is a good reason not to judge a model by parameter count alone.

Why do language models sometimes hallucinate?

A made-up answer, often called a hallucination, is a statement that sounds plausible but is wrong. A 2025 preprint by Kalai and colleagues argues that training and evaluation procedures reward guessing over admitting uncertainty.

We think you should read that as one explanation put forward by its authors, rather than a settled cause.

Groups of black server cabinets under blue light around a white pillar in a NASA machine room
Photo for illustration: the Discover supercomputer room. It is not tied to any model or answer in this article. Image: NASA/Pat Izzo

Does a model only look one word ahead?

Not always: as of 27 November 2026, Anthropic reports that Claude 3.5 Haiku, writing poetry, picked possible rhyme words early and built lines toward them.

In our view, you should not carry this finding over to LLMs in general. Anthropic also says its method for tracing the model's internal steps sees only part of the model's computation, even for short and simple prompts.

Does a long prompt get read evenly?

Not necessarily. A context window is the span of input a model can accept, and a 2023 paper by Liu and colleagues counted such windows in tokens.

The authors tested models available then on two tasks: question answering across multiple documents, and key-value lookup. Across those tests, results were often best when relevant information sat near either end of the input. In those tests, performance fell when relevant information appeared in the middle, even for models built for long context.

Line chart of accuracy by position of the answer document, high at the start and end and low in the middle
Figure 1 from the paper (page 2): one model, 20 documents. License: creativecommons.org/licenses/by/4.0. Image: Liu et al., TACL, CC BY license

The chart above comes from the journal version: Liu et al., TACL, CC BY license.

These measurements do not tell you how newer models behave.

What should you check before relying on an answer?

We think important claims in a reply deserve a check against the original document, however fluent the wording is. We think that, with long files, you should also ask which passage supports the answer, then read it yourself.

For more reading on AI models and software, start from https://blog.brandregard.com/ and follow the topics that interest you.

Photo credit: NASA/Pat Izzo.

Sources

  1. Beltagy et al.: Longformer, The Long-Document Transformer (accessed 2026-11-27)
  2. Zhao and colleagues, A Survey of Large Language Models (accessed 2026-11-27)
  3. Hugging Face Transformers glossary (accessed 2026-11-27)
  4. Hugging Face course, How do Transformers work (accessed 2026-11-27)
  5. Sennrich, Haddow and Birch, Neural Machine Translation of Rare Words with Subword Units (accessed 2026-11-27)
  6. Vaswani and colleagues, Attention Is All You Need (abstract) (accessed 2026-11-27)
  7. Vaswani and colleagues, Attention Is All You Need (full text) (accessed 2026-11-27)
  8. Liu and colleagues, Lost in the Middle (accessed 2026-11-27)
  9. Hugging Face Transformers generation strategies (accessed 2026-11-27)
  10. Holtzman and colleagues, The Curious Case of Neural Text Degeneration (accessed 2026-11-27)
  11. Ouyang and colleagues, Training language models to follow instructions with human feedback (accessed 2026-11-27)
  12. Kalai and colleagues, Why Language Models Hallucinate (accessed 2026-11-27)
  13. Anthropic, Tracing the thoughts of a large language model (accessed 2026-11-27)

Take this page with you

Share

More stories