Chapter 5

Language models: what’s under the hood of your generative AI system?

30 min read

Language models (LLMs) form the heart of generative AI applications. But what are LLMs, and how does that explain why a prompt you enter into a language model produces a response from that language model that is understandable to people.

#5.1Different forms of AI and how they relate

The term 'artificial intelligence' is often used as an umbrella term, but in reality it encompasses a wide range of technologies with different characteristics and applications. To properly understand how generative AI works, it is therefore important to first have a picture of the broader AI landscape. AI refers to systems that can perform tasks that normally require human intelligence. Think of reasoning, learning, planning, recognizing speech or images, and even generating new content such as text or images (Russell & Norvig, 2016).

We usually distinguish between three main categories of AI: symbolic AI, machine learning, and generative AI. Symbolic AI, also called 'GOFAI' (Good Old-Fashioned AI or weak AI), consists of explicit rules and logic. An example is a chess program that calculates all possible moves through a tree structure and then makes the most strategic choice. Symbolic AI simply follows preprogrammed rules or learns patterns based on data. This approach was dominant until the late 1980s (Crevier, 1993). There is also a distinction here between so-called weak AI and strong AI. Strong AI is a hypothetical concept in which an AI system would be capable of human-like consciousness and general intelligence. It is also called artificial general intelligence (AGI). Such a system would not only be able to perform tasks, but also reason, understand, learn, and even experience emotions like a human. For now, this is still the stuff of the future, if it ever becomes reality.

Figure 5.1 Depiction of the field of artificial intelligence and generative AI according to AI for Education (AI for Education, 2023)
Figure 5.1 Depiction of the field of artificial intelligence and generative AI according to AI for Education (AI for Education, 2023)

With the rise of machine learning, a subcategory within AI, the approach changed radically. Instead of explicitly programming how a system should make decisions, models learn to do this themselves based on data. This made it possible to recognize patterns in data without human intervention at every step. Machine learning uses statistical techniques such as linear regression and decision trees (T. M. Mitchell, 1997).

A step further is deep learning, which uses artificial neural networks with multiple layers. These networks can learn complex patterns that were previously inaccessible to simpler algorithms. Think of recognizing objects in images or predicting words in a sentence. Deep learning forms the backbone of the most advanced AI applications we use today (LeCun et al., 2015).

Generative AI is a specialized form of deep learning. Instead of only classifying or predicting, generative AI creates new output: text, images, audio, or video. Systems such as ChatGPT, DALL·E, and Google Gemini are models trained on enormous amounts of data and can use that knowledge to generate original content. Generative AI is therefore not only reactive, but also creative: it writes poems, programs software, creates artwork, and simulates conversations (Bender et al., 2021). But generative AI only does this based on your creative input and the creative training data on which the model was trained. The output can therefore be creative, but it is rarely original if you do not give it your own creative twist.

#5.2Neural networks and training

After exploring the AI landscape, we now look at the engine of generative AI: neural networks. Everything you see when you have an AI model write a text, generate an image, or make a summary is the result of what such a network has learned. But how does AI actually learn? And what happens behind the scenes when you enter a prompt? In this chapter, you will discover step by step how an AI model is trained and how a neural network functions.

#5.2.1How does a neural network work?

An artificial neural network is inspired by the human brain. It consists of layers of ‘neurons’ that process information. Each neuron takes input values, performs a mathematical calculation, and sends the result to the next layer. The connections between neurons have ‘weights’ that determine how important a given signal is. By adjusting these weights, the network learns to make increasingly better predictions.

In deep learning - the technique on which generative AI is based - these networks consist of dozens or hundreds of layers. Each layer recognizes a slightly more abstract pattern than the previous one. Where the first layer in an image, for example, recognizes only edges or areas of color, a higher layer can distinguish an eye, face, or chair (LeCun et al., 2015). In language models it works in a similar way: the first layers recognize individual words, the following layers understand grammatical structures or even the tone and intent of a text.

An important component of generative AI is the so-called transformer, introduced by Vaswani et al. (2017). This technique itself determines which words in a sentence are important for understanding the meaning correctly (see section 5.2.3). This allows models such as GPT-4 to analyze longer texts and give coherent answers. The T in GPT therefore stands for transformer.

Figure 5.2 Visualization of how a neural network works to detect characteristic parts in a figure (source: Wikimedia/Cyberbotics Ltd.).
Figure 5.2 Visualization of how a neural network works to detect characteristic parts in a figure (source: Wikimedia/Cyberbotics Ltd.).

#5.2.2What is machine learning?

The next step is to understand how neural networks actually learn: we call this process machine learning. Machine learning is the process by which computer systems learn from examples. Instead of programming explicit instructions, we give the model a large amount of data as training data - for example millions of texts - and let the system discover for itself what the patterns are. That training data is the foundation on which the model learns, for example, what is grammatically correct, which words often occur together, and what a particular writing style sounds like (M. Mitchell, 2019).

This learning process is based on trial and error. The model makes a prediction (for example: what is the next word in a sentence?), is told whether the answer was correct, and adjusts itself. This error correction happens using so-called ‘backpropagation’: the computational model sends an error signal back through the network and adjusts the internal weights on that basis (Goodfellow et al., 2016).

#5.2.3How does a language model learn by guessing the next word?

To understand how a language model learns, you can compare it to an advanced prediction machine that learns from millions of examples. During training, the model is shown sentences in which it must repeatedly predict what the next word is, complete with probabilities.

Step 1: The training sentence

Suppose the model is given the sentence: "The cat sits on the mat"

Step 2: The prediction game with probabilities

At first, the model sees only: "The". It predicts with probabilities:

  • "dog" (15% chance)
  • "cat" (12% chance) ✓ correct!
  • "man" (10% chance)
  • "car" (8% chance)
  • ... and thousands of other words with lower probabilities

Why is "dog" wrong? Not because dogs cannot exist after "The", but because in this specific training sentence the correct answer was "cat". The model now learns that it should increase the probability of "cat" after "The" slightly.

Step 3: The attention mechanism - what should I pay attention to?

With longer sentences it becomes harder. Suppose the model sees: "The black cat with the white spots sits on the"

The attention mechanism helps the model 'remember' that:

  • "cat" is the subject (not "spots")
  • "sits" is the action
  • We are probably looking for a place where the cat is sitting

This mechanism assigns weights to different words. The word "cat" gets a lot of attention (0.8), while "with" gets little attention (0.1).

Step 4: From training to use - the role of temperature

After training, the model can generate text. For the sentence "The cat sits on the" it has these predictions:

  • "mat" (45% chance)
  • "couch" (20% chance)
  • "chair" (15% chance)
  • "table" (10% chance)
  • "moon" (0.1% chance)

This is where temperature comes into play:

  • Low temperature (0.2): the model almost always chooses "mat" - safe but boring.
  • Normal temperature (0.7): usually "mat", sometimes "couch" or "chair" - natural variation.
  • High temperature (1.5): it may even choose "moon" - creative but possibly nonsensical.

Step 5: Learning from billions of examples

After seeing billions of sentences, the model learns complex patterns:

  • "The prime minister announced that..." → policy will probably follow (70%)
  • "The cat sits on the..." → probably a piece of furniture or a mat (85%)
  • "The research shows that..." → probably a scientific conclusion (75%)

The result

The model becomes a master at predicting likely word combinations. The attention mechanism helps it 'remember' relevant context over longer distances in the text. By adjusting the temperature, we can determine how 'safe' or 'creative' the output becomes.

But remember: the model does not really ‘understand’ that cats are animals. It only knows that in texts where "cat" appears, words such as "meows" (65% chance), "mice" (45% chance), or "tail" (40% chance) also often appear. This explains why AI can sometimes sound convincing and still say factually incorrect things - it is based on statistical patterns, not real understanding.

#5.2.4Different ways AI is trained

In supervised learning, examples with labels are used to learn to recognize patterns (Hastie et al., 2009). Imagine that you want to teach a computer to recognize whether an email message is spam or not. You then give the system a large collection of emails, and with each email you provide a clear label: this is spam, and this is not spam. This label helps the algorithm establish relationships between characteristics of the email (such as words, sender, links) and the label.

Who does the work?
A data scientist or machine learning engineer sets up the model and collects and labels the data. Sometimes the labeling is done manually, for example by human data labelers. If you need thousands or millions of examples, this is often outsourced to specialized companies. In chapter 9 we discuss the social aspects of this.

How much time does it take?
Building a well-functioning model can take weeks to months, depending on the size of the dataset, the complexity of the problem, and how well the data is prepared. Just collecting and labeling data can already take many tens to hundreds of hours. For creating LLMs or vision models that can recognize images, this can run into thousands or even millions of hours.

#5.2.5Unsupervised Learning - learning without examples

Unsupervised learning enables systems to discover patterns without predefined labels (Bishop & Nasrabadi, 2006). Imagine this: you give an algorithm customer data (such as age, purchase history, and location), but you do not say which customers are loyal customers or which are high-risk customers. The model must then try to recognize patterns by itself. For example: this group of people comes back often, and that group only once. Or: this group of people returns products more often, and this group does not.

Who does the work?
Here too, data scientists build the model and data labelers work on the training. The work lies mainly in collecting and cleaning the data, choosing suitable algorithms (such as clustering or dimensionality reduction), and interpreting the patterns.

How much time does it take?
Unsupervised learning can be faster than supervised learning because you do not need labels. But interpretation by data scientists takes more time, because the results are not self-evident. Count on a few days to a few weeks for an average analysis. For LLMs or general vision models (models that can recognize and generate images without prior knowledge), this increases enormously.

#5.2.6Reinforcement Learning - learning through reward and punishment

In reinforcement learning, a system learns optimal behavior through interaction with an environment and feedback via rewards (Sutton & Barto, 1998). This way of learning is a bit like how people or animals learn. You have the computer perform a task (for example making a robot walk or play a game) and give a reward when it goes well. If it goes wrong, it gets no reward or even ‘punishment.’ Based on that feedback, the system learns what works and what does not.

Who does the work?
Reinforcement learning is usually carried out by specialized AI researchers or engineers because it is technically complex. It often requires simulations, a great deal of computing power (Graphics Processing Units - GPUs), and experience with frameworks such as OpenAI Gym, Unity ML, or TensorFlow Agents.

How much time does it take?
This is often the most time-consuming. Training a good reinforcement learning model can take days to weeks - sometimes longer - depending on the complexity of the task. Think of hundreds to thousands of hours of compute time and preparation, including building the simulation environment.

Learning methodWith or without labelsWho carries it out?Average time investment
Supervised LearningWith labelsData scientists + data labeling teamsSeveral weeks to months
Unsupervised LearningWithout labelsData scientistsSeveral days to weeks
Reinforcement LearningFeedback-basedAI researchers / engineersWeeks to months (enormous compute time)
Table 5.1 Overview of machine learning methods.

The learning process of a language model proceeds technically speaking in roughly six steps:

  1. Tokenization. The input (text) is divided into smaller units: tokens. These can be words, but also syllables or even individual characters. For example, the sentence "The sun is shining" is divided into: ["The", " sun", " is shining"]. Division can be done in multiple ways, but a rule of thumb is that 1 token is about 3-4 letters long (CriticalMynd, 2025). This means that, for example, a blog of about 750 words is converted into about 1,000 tokens, an essay of 3,000 words into about 4,000 tokens, and a book of about 750,000 words into 1 million tokens.
  2. Vectorization. Each token is converted into a sequence of numbers: a vector. This vector contains information about the position of the word in a language space - comparable to coordinates on a map.
  3. Embedding. All vectors are combined in a so-called embedding space that represents the meaningful relationships between words. Words with similar meanings are located close together in this space.
  4. Training via backpropagation. The network learns by making predictions and correcting errors. If, for example, it predicts the word “sun” after “The”, and the correct answer was “moon”, then the model adjusts the internal parameters to make better predictions in the future.
  5. Loss functions measure how good or bad a prediction is. They steer the learning process by saying: “You were off by 30%.”
  6. Fine-tuning is an additional training phase in which an already trained model is adapted to specific tasks or contexts - for example, legal or medical applications. In this process, the weights in the model are adjusted and a copy of the language model is created.
Figure 5.3 Visualizations of neural networks and training based on the video “Generative AI in a nutshell” by Henrik Kniberg (2024).
Figure 5.3 Visualizations of neural networks and training based on the video “Generative AI in a nutshell” by Henrik Kniberg (2024).

#5.2.7Diffusion Models (Diffusers) - generative AI for images

A diffusion model is used specifically for generating images. Diffusion models, as first described by Ho et al. (2020), are a type of generative AI that is trained by gradually adding noise to an image or audio file. Think of this noise as static interference: elements that do not belong in the original image, such as distortion or random colors. This added noise is called ‘Gaussian noise’ because it follows a normal distribution - a perfect bell-shaped curve.

In the training phase, more and more noise is added until the file is completely unrecognizable. At the end, the image or sound consists only of noise. This phase is called diffusion. What the model still knows about the original image is stored only in its memory. Then the reverse process begins: the model tries step by step to remove the noise again and reconstruct the original image as well as possible. This reconstruction phase is called reverse diffusion. In doing so, the model uses what it has previously learned about patterns and structures in other images. Through this process, the model learns to generate new images that resemble the structures it knows from the training data.

During this learning process, the model becomes better and better at predicting which noise has been added. By analyzing the difference between the original image and the distorted version, the model learns which patterns are noise and which belong to the image. This continuous interplay between adding and removing noise forms the core of how diffusion models function and learn​. In this process, AI is taught by data labelers what is and is not correct: for example, pictures in which a dog would get 5 legs during diffusion instead of 4 legs.

Figure 5.4 Images used to show the diffusion process for generating and recognizing images from Yang et al. (2024).
Figure 5.4 Images used to show the diffusion process for generating and recognizing images from Yang et al. (2024).

#5.3Bias and data quality

One of the most discussed and at the same time often misunderstood aspects of AI is the risk of bias: systematic distortions in the output of an AI system that lead to inequality, misrepresentation, or even discrimination. At first glance, AI seems neutral: it is 'just an algorithm.' But in reality, AI systems are only as good (or as bad) as the data on which they were trained. In this section, you will discover how bias arises, why it is so persistent, and what you can do to recognize and reduce it.

#5.3.1Origins of bias in AI

Bias in AI usually does not arise from ill intent on the part of developers, but because datasets are used in model training that are unevenly distributed in terms of content. Think, for example, of a language model trained mainly on English-language texts from American websites: that model will perform especially well in American contexts and may have trouble with idioms or cultural references from other parts of the world (Bender et al., 2021).

A classic example comes from the medical world: an AI model that had to recognize skin conditions performed poorly on people with darker skin tones, because the training data mainly consisted of photos of white skin (Buolamwini & Gebru, 2018). So the problem was not in the model itself, but in the representation of the data on which it was trained. If the training data mainly includes men, or people from a certain income group, or texts from a certain ideology, then the model will reproduce those imbalances in the output. Suppose you are conducting sociological research into preferences in pop music; your results via AI are likely to be colored. This is because the data on which the AI is trained already contains a certain bias, for example mostly Western data.

AI models are therefore not only technical systems, but also a reflection of the world in which they came into being, with all the associated culture, politics, prejudices, and discrimination (Crawford, 2021b).

#5.3.2Strategies for reducing bias

Although you can never completely rule out bias, there are ways to limit its impact, such as:

  • Dataset diversification: collect training data from multiple sources, with attention to representing diversity in gender, ethnicity, language, region, age, and socioeconomic background.
  • Bias audits: conduct systematic tests on AI output to determine whether certain groups are systematically disadvantaged.
  • Transparency: document how the model was trained, which data was used, and which choices were made in the process (Model Cards, datasheets).
  • Human oversight: always have sensitive decisions - such as in selection procedures or health care - checked by people.

#5.3.3Representativeness and accountability

An important question is: who is the model intended for, and who remains out of view? AI is often developed in Silicon Valley, but used all over the world. Without explicit attention to context, this can lead to applications that do not respect cultural differences, or even have harmful consequences. Think of automatically generated images that reinforce stereotypes, or summaries that leave out certain perspectives.

Responsible use of AI therefore means not only technical precision, but also social reflection. Who determines what counts as ‘normal’ in a dataset? Who has access to the technology, and who does not? And who bears the consequences of mistakes?

Bias can arise at any stage: when collecting data, selecting features, training the model, or even interpreting the output.

#5.4AI hallucinations

Suppose you ask a language model who the president of France is. You expect Emmanuel Macron as the answer. But the model replies: François Hollande. Or even worse: Victor Hugo. These kinds of incorrect or made-up answers are known as AI hallucinations. The word ‘hallucination’ is used here because the AI appears to invent factual information based on what sounds plausible, without that information actually being true. In this section, you will discover what AI hallucinations are, why they occur, how to recognize them, and what implications they have for the use of generative AI.

#5.4.1What are AI hallucinations?

An AI hallucination is output that seems grammatically and stylistically correct, but is factually incorrect, misleading, or completely made up (Maynez et al., 2020). In language models such as ChatGPT, this means that the model generates sentences that do not correspond to reality, but do sound as if they could be true. For example, a chatbot may cite a non-existent scientific study or summarize an article that does not exist. In image generators such as DALL·E, hallucinations can lead to illogical images, for example a hand with extra fingers or non-existent objects.

Hallucinations are one of the biggest challenges to the reliability of generative AI, especially in contexts where factual accuracy is essential, such as scientific writing, medical advice, or legal support.

#5.4.2Why do hallucinations arise?

The fundamental mechanism of a large language model is based on probability calculation. The model repeatedly predicts which token - a word or part of one - is likely to follow based on all preceding tokens. This process, also called ‘next-token prediction,’ is purely statistical. The model does not know what is true or what exists; it only knows what sounds likely within the context (Brown et al., 2020).

If the training data is insufficient on a particular subject, or if the context of the prompt is vague, the model can make incorrect assumptions. Sometimes it invents sources because it has learned that an article with a year and an author’s name is a good format, without actually checking whether that article exists. The model’s so-called temperature setting - which determines how creative the model is allowed to be - also plays a role here: the higher the temperature, the greater the chance of surprising but unreliable output.

#5.4.3Consequences of hallucinations

In academic or professional contexts, AI hallucinations can have serious consequences. A student who cites a non-existent source risks being accused of fraud. A doctor who relies on AI for a rare diagnosis may choose the wrong treatment. Even in creative contexts, such as storytelling or design, hallucination can be problematic if the user expects a certain level of consistency.

That is why human intervention is always necessary: the user must check the information, verify the sources, and remain aware of the system’s limits. Chatbots are not oracles, but advanced guessing machines. Without a critical eye, their output can mislead, despite a professional or convincing tone.

#5.4.4Recognizing and dealing with hallucinations

There are several strategies for recognizing and dealing with AI hallucinations:

  • Check sources: ask the model for the exact URL or DOI of an article. Invented sources often have no working link. A source can still be real even if the link does not work, however. Therefore, especially ask for the title, author, year, and publication, and look them up yourself.
  • Ask for justification: have the AI explain the reasoning behind a particular conclusion. Vague or circular argumentation is a warning signal.
  • Use fact-checking tools: combine AI output with tools such as Google Scholar, Consensus, PubMed, or Wikipedia to verify claims.
  • Lower the temperature: if you have control over the model settings, lower the temperature to make the output more predictable and factual.

#5.5The 'Black Box' of AI

Although generative AI often seems impressive because of its fluent language and quick responses, many of the underlying processes remain invisible and difficult to understand - even for the developers. This phenomenon is known as the 'black box' problem. An AI model may arrive at accurate predictions or generate convincing texts, but it is often not clear how or why it does so. In this section, we dive deeper into the nature of this opacity, the attempts to clarify it, and its ethical and social implications.

#5.5.1Why is AI so opaque?

The complexity of modern neural networks is hard to overstate. Models such as GPT-4 contain hundreds of billions of parameters - weights and connections between neurons that together determine the model’s behavior (OpenAI, 2021). When you enter a prompt, thousands of calculations are carried out in fractions of a second. Each of these calculations contributes a little bit to the final result, but no single individual step is decisive on its own. This makes it virtually impossible to reconstruct exactly which paths are responsible for the generated output.

In addition, these models learn via ‘backpropagation’ based on enormous amounts of data. They internalize patterns without following explicit logic or rules. The result is that they function like a kind of complex intuition: extremely efficient, but difficult to explain. This lack of transparency stands in the way of control, trust, and accountability - especially in contexts with high social risks, such as diagnosing a medical condition, making decisions about granting benefits, or admission to educational programs.

#5.5.2Attempts at explainability

AI may sometimes seem like a mystery to you: an answer comes out, but you do not know how it was produced. To counter that 'black box' feeling, the research field of ‘explainable AI’ (XAI) emerged. Researchers are looking for ways to make it clearer how AI arrives at a decision. They do this, for example, by showing what the model pays attention to in a text, or by temporarily replacing a complicated model with a simpler model that is easier to understand (Lipton, 2018).

Two of the techniques for this are, for example:

  • SHAP (SHapley Additive exPlanations), which tries to determine which input features contributed the most to a particular output.
  • LIME (Local Interpretable Model-agnostic Explanations), which locally approximates the behavior of a complex model with a simpler, interpretable model.

You may forget these terms again after this, but what is important to remember is that these kinds of methods provide only a limited view. They give an impression of what the model may have done, but no conclusive proof. In language models such as GPT, this is even more difficult, because the input is constantly changing and the outcome depends on long contexts and probability calculations.

#5.5.3Ethical implications of opacity

The black-box nature of AI raises fundamental questions about responsibility and liability. Who is responsible if an AI system makes a mistake - for example, a discriminatory decision in a hiring procedure or an incorrect medical recommendation? If we cannot explain why a model does something, can we really use it in decisions that affect people’s lives?

Cynthia Rudin (2019) argues that black box models should not be used in sensitive domains such as justice, medicine, or credit provision. Instead, she advocates interpretable models that may be slightly less powerful but whose operation is understandable and testable.

Moreover, opacity leads to distrust. Citizens, policymakers, and professionals want to understand why a system arrives at certain conclusions. Without explainability, AI systems lose legitimacy. Transparency and explainability are therefore not luxuries, but conditions for socially responsible AI use.

5.6Self-study questions

    5.6.1Check questions

    1. What is the difference between supervised learning, unsupervised learning, and reinforcement learning? Give a brief example of each.
    2. What are AI hallucinations and why do they arise? Name two strategies for recognizing hallucinations.
    3. Explain what the 'black box' problem of AI is and why this raises ethical questions.

    5.6.2Reflection questions

    1. Chapter 4 describes how bias in AI systems arises from unevenly distributed datasets. Think of a concrete example from your own field of study where this could be problematic. How would you deal with this as a future professional?
    2. This chapter compares using AI to driving a car without understanding how the engine works. To what extent do you think users of AI systems should understand the underlying technology? What are the advantages and disadvantages of more technical knowledge for ordinary users?
    3. Because of AI use, some words suddenly get used much more often. For example, in English it is noticeable that the word ‘delve’ suddenly appears much more often in texts. Some people think this could be because certain LLMs are mainly trained by people from certain regions. For instance, the word ‘delve’ might be more common in Kenya, where many LLMs are trained. What do you think? Argue your answer.
    5.6.3Suggested answers
    1. Supervised learning uses labeled examples to learn patterns (for example labeling emails as spam or not spam). Unsupervised learning looks for patterns without labels (for example grouping customers based on purchasing behavior). Reinforcement learning learns through reward and punishment (for example teaching a robot to walk through positive feedback for good steps).
    2. AI hallucinations are convincing-sounding but factually incorrect answers generated by AI. They arise because AI statistically predicts what is likely to come next, without real knowledge of facts. You can recognize them by checking sources, asking for justification, using lower temperature settings, and using fact-checking tools.
    3. The black box problem means that we cannot explain how AI arrives at specific decisions, even when the results are accurate. This raises ethical questions about responsibility, transparency, and liability, especially in sensitive domains such as health care or the justice system, where explainability is crucial.
    4. Personal reflection desired. Think, for example, of medical diagnoses that may contain prejudices about certain patient groups, or HR systems that discriminate in job applications. As a professional, you can be aware of these risks, require diverse datasets, conduct regular audits, and always maintain human oversight in important decisions.
    5. Personal opinion desired. Arguments for more knowledge: better critical use, more trust, better quality control. Arguments against: technical complexity is not accessible to everyone, and the focus should be on practical use. A middle ground could be a basic understanding of possibilities and limitations without in-depth technical details.