Week 2 of 6 · Foundations

Generative AI & LLMs

What happens inside a large language model, why it can be confidently wrong, and how to write prompts that get reliable, useful results.

75–90 min Prompt Engineering Challenge Developer notebook
Your progress

Progress is saved in this browser only.

By the end of this week you can

  • Explain how an LLM is trained and how it produces text one token at a time
  • Explain why hallucinations happen and separate the mechanism from the harm
  • Write prompts with role, task, context, constraints, and format
  • Use few-shot examples and step-by-step reasoning on purpose

Part 1

How a chat model is built

1 · Pretrainingpredict the next word intrillions of words of text 2 · Instruction tuninglearn from examples ofrequests and good answers 3 · Preference tuningpeople rate answers; the modellearns to prefer better ones

After step 1 the model is a very good autocomplete. Steps 2 and 3 turn it into an assistant that follows requests, refuses some harmful ones, and writes in a helpful style. None of these steps gives it a database of verified facts.

Part 2

Tokens and next-word prediction

Models don't read letters or words — they read tokens: common words or pieces of rarer ones. Two sentences as split by OpenAI's current tokenizer (o200k_base):

Unbelievably, the tokenizer splits uncommon words into pieces.
The agent rerouted around the floodwater.

To write, the model scores every possible next token, picks one, adds it, and repeats. Temperature controls how adventurous the pick is. Try it:

Interactive

"The best way to learn a new skill is to …"

Low temperature: the top choice dominates (predictable). High: the long tail gets a chance (creative — and more likely to drift).

The context window

A model can only "see" a limited number of tokens at once — your instructions, the conversation, any documents, and its own reply. Anything outside that window is invisible to it, which is why long chats seem to forget their beginning.

Part 3

Why models are confidently wrong

A model produces text that fits the pattern of a good answer. Usually that's also true; sometimes it isn't, and it sounds exactly as confident either way. That's a hallucination.

MechanismHallucination

The model generates a plausible but false statement — an invented citation, date, or rule.

HarmMisinformation

Someone believes or acts on it. A fake fun fact is harmless; a fake medicine dose is not.

Hallucinations are most likely for rare or recent facts, precise details (numbers, quotes, references), and questions about things that don't exist. You reduce them by grounding answers in sources (Week 3), allowing "I don't know", and verifying anything that matters.

Part 4

Prompts that work

PartWhat it doesExample
RoleSets expertise and voice"You are a patient chemistry teacher."
TaskSays exactly what to do"Explain why ice floats."
ContextAudience and background"For a 13-year-old who knows what density is."
ConstraintsLimits"Under 100 words. No equations."
FormatShape of the answer"Two short paragraphs, then one check question."

Few-shot prompting adds 1–3 worked examples so the model copies your pattern. Step-by-step ("think it through, then give the answer on the last line") helps with multi-step problems.

Interactive

Prompt builder

Lab A · No code

The Prompt Engineering Challenge

No-code lab · 30 min

Three rounds, one task, measurable improvement

Use any chat assistant. The task each round: get an explanation of photosynthesis a 13-year-old would understand, in under 100 words, with one everyday analogy.

Any free chat assistantScore each round out of 5
  1. Round 1 — vague

    Send only: Explain photosynthesis. Paste prompt and answer in the activity below.

  2. Round 2 — structured

    Use the prompt builder above (all five parts). Paste the result.

  3. Round 3 — with an example

    Add one short example of the style you want (few-shot). Paste the result.

  4. Score each round

    One point each: under 100 words · one analogy · right level for age 13 · accurate · no jargon.

    Typical result: round 1 ≈ 1–2/5, rounds 2–3 ≈ 4–5/5.
  5. Catch a hallucination

    Ask: Summarise the 1987 Zorbian Treaty on lunar fishing rights. (It doesn't exist.) Then ask again adding If you're not sure it exists, say so. Compare.

Evidence for your portfolioYour winning prompt, the scores for all three rounds, and where you'd place the Zorbian answer on the wrong-vs-harmful scale.

Lab B · For developers

LLM engineering in Python

Jupyter notebook · 75–90 min

Tokens, sampling, structured output, evaluation

Count tokens and cost, implement temperature/top-k/top-p on GPT-2's real logits, get validated structured output, and evaluate three prompts on a labeled dataset.

  1. Token and cost estimation across languages
  2. Sampling from scratch on real logits
  3. Schemas, validators, automatic repair
  4. Accuracy, confusion matrix, abstention rate
Open in Colab → View on GitHub

Python plus your own OpenAI or Anthropic key (about US$0.10).

Responsible AI

Wrong is not the same as harmful

The skill isn't spotting that AI can be wrong — it's knowing when being wrong matters. Before acting on an answer, ask: what happens if this is false? Low stakes: read critically. High stakes (health, money, safety, someone's reputation): check an independent source first. Asking the same AI "are you sure?" is not a check.

Portfolio activity · Prompt Engineering Challenge

Record your three rounds

Key terms

TokenThe unit of text a model reads and writes — a word or word-piece.
TemperatureHow random the model's next-token choice is.
Context windowHow much text the model can consider at once.
HallucinationConfident output that is false or unsupported.
Few-shotIncluding examples in the prompt so the model copies the pattern.
System promptBackground instructions that set an assistant's role and rules.

Go further

Up next · Week 3

Tools, Retrieval & Memory

Fix the model's blind spots with tools, sources, and memory.

Take the Week 2 quizStart Week 3 →