Lesson 2 of 2 How Generative AI Models Work

Explaining How Training and Inference Shape Model Behavior

This lesson explains how a generative model's behaviour follows from how it was trained and how it generates output: pretraining, fine-tuning and alignment, next-token prediction and sampling, and the limitations that every AI solution must design around.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish pretraining, fine-tuning and alignment.
  • Explain how next-token prediction and sampling generate output.
  • Identify the limitations of generative models, such as fabrication and the training cutoff.

Training

Pretraining exposes a model to a very large collection of text (and, for multimodal models, images and audio) and trains it to predict the next token. Through this task the model learns grammar, facts, reasoning patterns and styles. The result, a foundation model, is general-purpose but not yet a helpful assistant.

Fine-tuning continues training on smaller, curated datasets, for example examples of instructions and good responses, so that the model follows instructions. Alignment techniques such as reinforcement learning from human feedback (RLHF) train the model to prefer helpful, honest and harmless responses. Organizations can also fine-tune some models on their own examples to teach a consistent style or format, but for knowledge that changes, supplying facts in the prompt (grounding) is usually better than fine-tuning.

Put in order

From raw text to assistant

Exam skills 1.2.1

Put the stages that turn a new model into a helpful assistant in order.

Log in to try this activity Interactive activities are free with an account.

Inference and sampling

Inference is using the trained model. The model processes the prompt and produces a probability for every token in its vocabulary as the next token. A sampling step chooses one, it is appended, and the process repeats until the model emits a stop token, reaches a stop sequence or hits the maximum output tokens. The model has no memory between requests: anything it should know about earlier turns must be included in the prompt.

Sampling settings shape the output. Temperature scales the probabilities: low values make the most likely tokens even more likely, giving focused, repeatable text; high values flatten the distribution, giving more varied and creative text. Top_p (nucleus sampling) restricts the choice to the smallest set of tokens whose probabilities add up to p. Microsoft recommends adjusting one of the two, not both. Reasoning models generate internal reasoning tokens before their answer and are controlled with a reasoning effort setting instead of temperature.

The following diagram shows how temperature reshapes the next-token probabilities.

How sampling chooses the next token. For the prompt Please send the, the model gives every token in its vocabulary a probability. A low temperature sharpens the distribution so the most likely tokens win almost every time, giving focused, repeatable text; a high temperature flattens it so less likely tokens are chosen more often, giving varied text. Top_p limits the choice to the smallest set of tokens whose probabilities add up to p. The chosen token is appended and the process repeats until a stop condition.
Probabilities are illustrative.
Put in order

One token at a time

Exam skills 1.2.1

Put the steps a model repeats during inference in order.

Log in to try this activity Interactive activities are free with an account.

Limitations to design around

Because a model predicts plausible text, it can fabricate facts, citations or figures with confidence. Its knowledge stops at its training cutoff, and it knows nothing about private company data unless that data is supplied. It can reflect biases in its training data, and its output can vary between identical requests.

Common mitigations follow from these causes. Grounding supplies trusted, current data in the prompt and instructs the model to answer only from it; when that data is retrieved from a search index first, the pattern is called retrieval-augmented generation (RAG). Clear instructions and examples reduce variation, lower temperature increases consistency, guardrails filter harmful content and human review catches what remains.

The following diagram compares a model answering alone with a grounded model.

Why grounding reduces fabrication. A model alone predicts plausible text from its training data, which stops at its training cutoff and contains no private company data, so it can answer a question about a new internal policy confidently and wrongly. With grounding, trusted and current data is retrieved, for example from a search index, and placed in the prompt with an instruction to answer only from it; this is retrieval-augmented generation (RAG).
Grounding supplies the facts the model was never trained on.
Scenario questions

Design around the limits

Exam skills 1.2.1

Trace each problem to a model limitation and choose the mitigation that addresses its cause.

Log in to try this activity Interactive activities are free with an account.
Worked example

Explaining an incorrect answer

An HR chatbot built on a general model gives a confident but wrong answer about the company's new parental leave policy, which was published last month. Why did this happen, and what should the team do?

The model has never seen the policy: it is private and newer than the training cutoff, so the model generated a plausible answer from general patterns.

The team should ground the chatbot in the policy documents, for example by giving an agent file search over them, and instruct it to say when the documents do not contain the answer. Lowering the temperature would make the wrong answer more consistent, not correct, and fine-tuning on the policy would have to be repeated every time the policy changes.

Check yourself

Does a model remember earlier requests?

Show answer

No. Each request is independent, so an application must send the conversation history or use a conversation object to keep context.

Exam Tips

Remember for the exam

  • Pretraining teaches general patterns; fine-tuning and alignment make a model follow instructions safely.
  • Inference predicts one token at a time; models keep no memory between requests.
  • Lower temperature gives more consistent output; adjust temperature or top_p, not both.
  • Grounding (RAG) addresses missing or outdated knowledge and reduces fabrication.