Area 01 of 08
LLMs
How large language models read, think and write: tokens, attention, sampling, prompting, fine-tuning and choosing a model.
Printable cheat sheet for this area
- 012
Tokenization
Before a language model can read text, a tokenizer chops it into tokens (whole words, pieces of words or symbols) and turns each one into a number, and those tokens are what models read, write, limit and charge for.
- 013
Embeddings and positional encoding
Inside an LLM, every token ID is swapped for a learned vector (its embedding) that captures meaning, and position information is added so the model knows the order of the words.
- 014
Self-attention
Self-attention lets every token look at the other tokens in the text and pull in information from the ones that matter to it, which is how a model works out that "it" refers to "the animal" and not "the street".
- 015
The transformer architecture
A transformer is a stack of identical blocks, each letting tokens share information through attention and then think individually through a small neural network, and an LLM is a big decoder-only transformer that ends by scoring every possible next token.
- 016
Mixture of experts
A mixture-of-experts model replaces one big feed-forward network in each layer with many smaller "expert" networks plus a router that sends each token to only a few of them, so the model can store far more knowledge while doing much less work per token.
- 017
Logits, decoding, temperature and top-p
At every step an LLM outputs a score (logit) for every possible next token, and a decoding strategy (greedy, beam, top-k, top-p, with a temperature) decides which token actually gets written, which controls how predictable or creative the output is.
CoversLogits, softmax and decoding (greedy, beam, top-k) · Temperature and Top-p
- 018
Context windows and the KV cache
The context window is the maximum number of tokens a model can look at in one request (your input plus its output), and the KV cache is the memory of already-processed tokens that lets it generate each new token quickly instead of re-reading everything.
CoversContext Windows · KV Cache
- 019
Pretraining and post-training (SFT, RLHF, DPO)
An LLM is made in two stages, pretraining on huge amounts of raw text so it learns language and facts by predicting the next token, then post-training (supervised fine-tuning and preference training like RLHF or DPO) so it follows instructions, is helpful and refuses harmful requests.
- 020
Prompt engineering basics and system prompts
Prompt engineering is writing instructions a model can't misunderstand, with a clear role, task, context, constraints and output format, and the system prompt is the standing set of instructions that frames every conversation.
CoversPrompt engineering basics and prompt templates · System Prompts
- 021
Few-shot prompting and chain of thought
Few-shot prompting means showing the model a few worked examples of the task so it copies the pattern, and chain of thought means asking it to reason step by step before answering, which helps on multi-step problems.
CoversFew-shot Prompting · Chain of Thought
- 022
Reasoning models
Reasoning models are LLMs trained to think through a problem internally (planning, trying approaches, checking their work) before answering, which makes them much better at maths, code and multi-step tasks at the cost of more tokens and more time.
- 023
Structured outputs
Structured outputs make a model reply in an exact machine-readable shape (usually JSON matching a schema you define), so your code can use the answer directly instead of trying to parse free text.
- 024
Function and tool calling basics
Tool calling lets a model ask your code to run a function (look up an order, search a database, send an email) by replying with the function's name and arguments; your app runs it and sends the result back so the model can finish its answer.
- 025
Prompt caching and token pricing
LLM APIs charge per million tokens, with output costing several times more than input, and prompt caching lets the provider reuse the work for a repeated beginning of your prompt, cutting the price and latency of those tokens dramatically.
CoversToken Pricing · Prompt Caching
- 026
Hallucinations
A hallucination is when a model states something false or made up with full confidence, because it generates likely-sounding text rather than looking facts up; you reduce it by grounding answers in real sources, allowing "I don't know", and checking outputs.
- 027
Multimodal inputs
Multimodal models can take images, PDFs, audio and sometimes video as input alongside text, by turning each into tokens the model can attend to, which lets you build features like reading receipts, describing screenshots or answering questions about charts.
- 028
Fine-tuning with LoRA and QLoRA
Fine-tuning adapts an existing model to your task with your own examples; LoRA does it cheaply by freezing the original weights and training tiny add-on matrices, and QLoRA goes further by loading the frozen model in 4-bit so big models can be tuned on a single GPU.
CoversFine-tuning with LoRA · QLoRA
- 029
Quantization and distillation
Quantization shrinks a model by storing its weights with fewer bits (16 → 8 → 4), and distillation trains a small "student" model to copy a big "teacher" model; both make models cheaper and faster to run with only a small loss in quality.
CoversQuantization · Distillation
- 030
Open vs closed models, small vs large
Closed models are used only through their maker's API and usually lead on quality, open-weights models can be downloaded and run anywhere you like, and small models are cheaper and faster while large ones are smarter; most products end up mixing them.
CoversOpen vs Closed Models · Small vs large models
- 031
Benchmarks and model leaderboards
Benchmarks are standard test sets that score models on skills like knowledge, maths or coding, and leaderboards rank models by those scores or by human votes; they're useful for a shortlist, but only your own evals tell you which model is best for your product.