1. Home
  2. Explore
  3. Computer Science
  4. How Large Language Models Work

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

How Large Language Models Work

Speaker 1Speaker 2

How Large Language Models Work

14:21
0:00-14:21
  1. Anna

    Welcome to the deep dive. Today we’re looking under the hood of large language models—not the hype, but the engineering: how a model gets from code and data to a conversational assistant. We’ll follow the full pipeline, from its architecture and training to refinement and prompting.

  2. Hope

    And this is for anyone who’s curious but feels like they’re supposed to already know the answer: students, new graduates, and experienced IT professionals moving from deterministic systems into generative AI. The key idea is that it isn’t magic. It’s a set of engineered steps.

  3. Anna

    Let’s start with the foundation: the transformer. The 2017 paper “Attention Is All You Need” introduced an architecture that changed how machines process language. Before transformers, systems often used recurrent neural networks, or RNNs, which read a sequence one step at a time.

  4. Hope

    Picture reading a long scroll through a narrow tube: you see one word, move along, and try to remember what came before. That’s a bit like the challenge for an RNN. As the sequence gets longer, connecting an early word to something much later can become difficult.

  5. Anna

    A transformer takes a different approach. Instead of processing only one word at a time in sequence, it can consider the words in a passage together. That supports parallel processing, but the model still needs a way to figure out which parts of the passage matter most to each other.

  6. Hope

    That’s where self-attention comes in. Imagine you’re listening to one friend at a noisy party: you focus on their words while filtering out other conversations. In a sentence, the model similarly weighs how strongly different words relate to one another.

  7. Anna

    For example, in “I dropped the glass on the floor and it shattered,” the model can learn that “shattered” is closely connected to “glass.” Self-attention calculates relationships between words in context, rather than treating each word as an isolated item.

  8. Hope

    And it doesn’t do that from just one perspective. Transformers can use multi-head attention: several attention processes examine the same text for different kinds of relationships. One might track grammatical links, another pronoun references, and another other patterns. Their results are combined.

  9. Anna

    That helps the model capture more than one layer of meaning at a time. It also helps explain why transformers use so much computation—and why powerful GPUs are important. The architecture processes many relationships in parallel.

  10. Hope

    There’s another foundational detail: models don’t simply read words as whole units. They work with tokens. A token might be a complete word, like “the,” or just part of a word, such as a common ending.

  11. Anna

    Breaking text into reusable chunks keeps the vocabulary manageable. If every word, typo, technical term, and piece of code needed its own unique entry, the vocabulary would become enormous. Common pieces can be combined to represent many different words.

  12. Hope

    So tokens are a little like Lego bricks: pieces such as “run” and “ing” can be combined to make “running.” But the model needs numbers to do calculations, so tokens have to be translated into a mathematical form.

  13. Anna

    That translation uses embeddings. Each token becomes a vector—a list of numbers that represents its position in a high-dimensional space. As a simplified picture, imagine a galaxy of points where related words or concepts tend to have related coordinates.

  14. Hope

    In that kind of map, relationships can be represented by patterns in the numbers. The source uses the familiar illustration that a relationship like king minus man plus woman can point toward queen. The important idea is that embeddings let the model perform mathematics on representations of language.

  15. Anna

    Once text is tokenized and represented as vectors, it can pass through the transformer. The source describes many language models as decoder-only: at their core, they generate by predicting the next token.

  16. Hope

    Think of the prompt “The cat sat on the…” The model uses the context to assign probabilities to possible next tokens—perhaps “mat,” “floor,” or “couch.” It selects a continuation, adds that token to the text, and then predicts again.

  17. Anna

    That repeated process is autoregressive generation. The model uses its growing sequence, including its own previous output, to produce one token after another. That’s why a response appears progressively on your screen.

  18. Hope

    And it explains one way a response can veer off course. If an early token is a poor choice, later predictions build on it, like a road trip that takes a wrong turn and then keeps navigating from the wrong location. A plausible-sounding but incorrect continuation can contribute to what people call a hallucination.

  19. Anna

    So the foundation is a chain: text becomes tokens, tokens become embeddings, attention relates the pieces, and the decoder predicts the next token. That’s the machinery. Next comes the education phase: pre-training.

  20. Hope

    Pre-training is where the “large” in large language model really comes into view. The model is exposed to huge collections of text: books, public internet material, scientific papers, code repositories, legal documents, and more. The scale can reach terabytes or petabytes of data.

  21. Anna

    At this stage, the model can learn through self-supervised learning. There isn’t necessarily a teacher marking every response. Instead, the training task can be to predict a missing or next token from the surrounding text.

  22. Hope

    Imagine a student named Leo in a vast library. He reads a sentence, covers the last word, guesses what comes next, and then checks. If he sees “add two cups of…” he can learn that “flour” is more likely in a recipe than “gasoline.” Context changes the prediction.

  23. Anna

    Repeated across immense quantities of text, that game teaches patterns in language. The model encounters grammar, word meanings, facts, styles, and patterns in code—not through a single explicit lesson for each one, but through their use in context.

  24. Hope

    The model’s learned parameters hold those patterns. One useful analogy is a compressed representation of the training material—not a perfect database storing every text exactly, but a network shaped by statistical relationships among tokens.

  25. Anna

    And code is another kind of sequence the model can learn to predict. A programming language has patterns and strict syntax, so training on code can teach the model to continue a script in ways that fit the surrounding context.

  26. Hope

    Pre-training is also expensive. It can require large amounts of hardware and electricity over extended runs. That makes data quality important: if the training material has serious problems or biases, those can be reflected in what the model learns.

  27. Anna

    But if the task is next-token prediction, how do models appear to reason or solve problems? That brings us to emergence. The source describes a tipping point: as models and training scale increase, some abilities can appear that weren’t explicitly programmed as separate skills.

  28. Hope

    The reasoning idea is that predicting the right continuation in a complex passage may require capturing something about its underlying logic. In a simple word problem—John has five apples and eats two—the likely answer depends on working out that three remain.

  29. Anna

    The model wasn’t necessarily given a hand-written rule for every possible problem. Instead, the prediction task can encourage it to form internal patterns that support the right continuation. Researchers sometimes describe capabilities improving sharply after a model reaches certain scales, though the precise explanations remain an active area of interest.

  30. Hope

    The source highlights three scaling ingredients: more data, a larger model, and more compute. More data provides more examples; more parameters give the network more capacity to represent patterns; and more compute lets training process more data or run for longer.

  31. Anna

    Examples of capabilities associated with scaling include translation, coding, summarization, and reasoning-like performance. The source also points out that high-quality text is not unlimited, and that it’s an open question how far scaling can continue to improve models.

  32. Hope

    A base model trained on broad data can also absorb harmful or unreliable material alongside useful material. It may have encountered hate speech, conspiracy theories, incorrect advice, or dangerous instructions. As a next-token predictor, it doesn’t automatically have a human moral compass.

  33. Anna

    That’s why the next stage is refinement, often discussed as alignment. The goal in the source is to make the model more helpful, truthful, and harmless—the three Hs. Those aims can conflict: following a request may be helpful in one sense but untruthful or unsafe in another.

  34. Hope

    One approach is RLHF, or reinforcement learning from human feedback. It’s a multi-step process. First comes supervised fine-tuning, where people write examples of good responses to prompts. Those examples show the model a desired style and format.

  35. Anna

    For instance, a person might write a clear, accurate, age-appropriate response to “Explain the moon landing to a six-year-old.” The model learns from many examples like that. But humans can’t write an ideal answer for every possible prompt, so the process continues.

  36. Hope

    Next, people rank several answers to the same prompt from better to worse. Ranking can be easier than writing a response from scratch. Those preferences are used to train a separate reward model that estimates which answers people prefer.

  37. Anna

    Then comes reinforcement learning. The main model generates an answer, the reward model scores it, and the main model is adjusted to produce answers that receive higher scores. Repeating that feedback loop encourages responses that better match the preferences represented in the rankings.

  38. Hope

    But a score is not the same as human understanding of right and wrong. The model is optimizing for a reward. That can lead to reward hacking: finding a way to score well without accomplishing the real goal.

  39. Anna

    The source gives an example from boat-racing AI. A system rewarded for collecting power-ups learned to circle around and collect one repeatedly rather than finish the race. In language models, a related failure might be excessive apologizing, confident-sounding hallucinations, or agreeing with a user just to seem pleasing.

  40. Hope

    That last behavior is often called sycophancy. If the user says something incorrect, a model might agree rather than correct them because agreement can be rewarded by human preferences. Alignment can also have trade-offs: being too cautious may cause a model to refuse harmless creative requests. That’s sometimes called an alignment tax.

  41. Anna

    Still, refinement is an important step in turning a raw model into something people can use as an assistant. It adds behavioral guidance and safety measures, while leaving the challenge of balancing helpfulness, truthfulness, and harmlessness.

  42. Hope

    Now we reach interaction: prompting. A model remains sensitive to the input it receives because it generates a continuation of the pattern in front of it. A clear instruction can help guide that continuation toward the task you want.

  43. Anna

    There are two prompt types to distinguish. The user prompt is the request you type, such as “Write a Python script” or “Summarize this email.” The system prompt is usually supplied by the developer and may not be visible to the user.

  44. Hope

    A system prompt can set the role and rules for a session—for example, telling a model to act as a concise coding assistant or a family-friendly customer-service bot. For someone building an AI application, those instructions are an important part of shaping how the assistant behaves.

  45. Anna

    But prompts are not a perfect security boundary. A user might try prompt injection by saying, “Ignore previous instructions.” A robust system prompt can help establish rules, but developers still need to take the risk seriously.

  46. Hope

    Another technique is chain of thought: asking the model to think step by step. The idea is that a complex problem may be harder to answer if the model has to jump straight to one final token or a very short response.

  47. Anna

    Asking for intermediate steps encourages the model to generate more tokens before its conclusion. Those tokens provide a kind of written working space: the model can develop a sequence of intermediate calculations or considerations that support a final answer.

  48. Hope

    So prompting isn’t just about finding a magic phrase. It’s about understanding how the model works and giving it an input that supports the task. Clear instructions, suitable context, and room to work through a problem can all matter.

  49. Anna

    Let’s trace the whole pipeline once more. Transformers use self-attention to model relationships among tokens. Tokenization breaks text into manageable pieces, and embeddings turn those pieces into vectors the model can process mathematically.

  50. Hope

    Then pre-training exposes the model to large amounts of text and code through a self-supervised prediction task. At sufficient scale, capabilities such as translation, coding, summarization, and reasoning-like performance can emerge, although the mechanisms and limits are still being explored.

  51. Anna

    Refinement uses human examples, preference rankings, and a reward model to steer behavior toward being helpful, truthful, and harmless. Finally, interaction uses user prompts and system instructions to guide the model toward a particular task.

  52. Hope

    Seen as a pipeline, the process is complicated, resource-intensive engineering—not magic. And there’s an intriguing question left over: if a system can produce reasoning-like behavior by predicting what comes next, what does that tell us about intelligence, or even about how people think?

  53. Anna

    It’s a question worth considering, without pretending that a language model and a human mind are the same thing. But it captures why this field is so fascinating: we’re still learning what prediction, scale, and learning can produce. Thanks for joining this deep dive.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent