Generative AI & LLM Engineering · Chapter 1 · ~25 min · Beginner · No API key needed
What an LLM Actually Is — Inside the Next-Token Machine
ChatGPT, Claude and Gemini feel like magic. Underneath, they do one simple thing again and again: guess the next small piece of text. Make a prediction, move the dials, and repair a small order-extraction policy.
- Follow the next-token loop
- Explore text chunks & sampling
- Budget context and answer space
- Repair an order-bot policy
One small guess. Then the loop repeats.
Read → score → choose → append. These are teaching examples, not a live AI response.
1Super-powered autocomplete
Your phone suggests the next word. A Large Language Model (LLM) learns patterns from large datasets and generates text one token at a time. An app can add search or tools, but generation itself is not a web search.
Read → predict → append → repeat. Step 1 of 8. This fixed sentence demonstrates the loop, not a real model prediction.
Predict first
After the machine adds ‘cat’, what does it use to predict the next token?
Wrong idea: It searches automatically
A chat box does not mean every answer comes from a web search.
Real system: It reads the supplied context
The text becomes token IDs. Search only happens if the app supplies a search tool or retrieved documents.
How does an LLM produce a long answer?
2Tokens: the pieces the model reads
Models receive token IDs representing text pieces: sometimes words, word parts, punctuation, or bytes. Try this simple chunking illustration; exact splits vary by tokenizer.
14 pieces · 52 characters · “·” marks a space that belongs to the token. Illustrative chunks only, not a real BPE tokenizer. Real boundaries depend on the model; Chapter 2 goes deeper.
Predict first
Two models use different tokenizers. Must they split the same sentence identically?
Roughly how many tokens is a 400-character English paragraph?
3Probabilities & the temperature dial
For every step the model gives each possible token a score, turned into percentages. Then it picks one. Temperature decides how adventurous that pick is. Drag it and roll the dice several times.
Five example candidates with fixed scores. The slider changes their chances, not their factual accuracy.
The cat sat on the ___
matsofafloorroofmoonPredict first
At temperature 0 in this fixed-score simulator, can ‘sofa’ beat the top-scored ‘mat’?
Concentrates chances on higher-scored tokens. Often useful for extraction.
More variation for conversation. Test on your actual task.
Flattens chances for varied ideas. Does not add knowledge.
You need the model to extract invoice numbers into JSON reliably. Which temperature?
4Context window & cost
The context window is the model's short-term memory: your prompt + chat history + its answer must fit inside. Many hosted APIs charge for input and output tokens. Try an illustrative budget, including room for the answer.
Teaching scenario: 128K window, ~500 tokens/page, $0.50/M input and $1.50/M output. Not a current provider quote. Local models have hardware costs; not every service charges per token.
Predict first
A window holds 100 tokens. Your prompt and history use 90; you reserve 20 for the answer. Does it fit?
Why might a chatbot ‘forget’ the start of a very long conversation?
5What LLMs are great — and bad — at
Great at
- Writing, summarising, rewriting
- Explaining & generating code
- Turning messy text into structured JSON
- Translation & tone changes
Watch out for
- Hallucinations: confident made-up facts
- New information needs supplied sources or tools
- Exact calculations still need checking
- Your private data it has never seen
Predict first
An order bot answers ‘Order #77777’ when the customer supplied no order number. What is the safest next step?
6Mini-project: fix the runaway order bot
A shop wants an order ID, not a creative reply. Repair this small policy: use temperature 0–0.2, allow 16–64 output tokens, and return null when the ID is missing.
These are local JSON and extraction-policy tests, not a live model call. The missing-ID rule is enforced by code; a prompt or stop sequence alone cannot guarantee it.
🎯 Find and fix the bugs in the code above, then press Run & verify to test your solution.
Need a hint?
Try 0.1, 32, and "return_null". Keep the JSON field names unchanged. The tests include an actual ID, missing information, and an unrelated phone number.
7Chapter challenge
Next-token machine check
Answer all four correctly to complete the chapter.
1Which sentence best describes an LLM?
2What should an extraction bot do when no order ID appears in the input?
3Raising temperature from 0.2 to 1.5 will…
4Your 900-page manual doesn't fit the context window. Best next step?
8Recap
- An LLM is super-powered autocomplete: predict a token, append, repeat.
- Token boundaries depend on the model. Four English characters per token is only a rough estimate.
- Temperature changes sampling, not truth. Verify important outputs even at zero.
- Budget context for input plus the answer. Hosted pricing varies; local software still has hardware costs.
- LLMs can hallucinate — always verify important facts.
Solve the challenge above to complete the chapter, or