Learn LLMs
Learn how LLMs work, how to run and prompt them, then call one from code: chat, streaming, tools, embeddings, JSON output, RAG, cost and rate limits.
LLM · Beginner · 32 lessons · 237 min
New to LLMs? Start with the Journey: type a sentence and watch a model process it.
Why does ChatGPT do that?
9 lessons
- 3 minBeginner
Why does it miscount the r’s in "strawberry"? Explain lesson
A token is the unit of text a language model actually reads: a word, part of a word, or punctuation, each turned into a number. Models count, price and limit everything in tokens, and never see the letters inside one.
- 5 minBeginner
How does it know "king" is close to "queen"? Explain lesson
An embedding is the list of numbers a model uses as a word’s position in a space with thousands of dimensions. Words used in similar company end up near each other, and relationships become directions you can add.
- 5 minBeginner
How does it know what "it" refers to? Explain lesson
Self-attention is how a language model decides which other words matter for each word: every word scores every other word, and the scores are learned from text, not written as rules.
- 5 minBeginner
What's inside the box? Explain lesson
A Transformer is a stack of identical layers. Each layer runs attention (who matters to whom) and then a small "think" step on every piece, and the stack repeats that dozens of times before producing odds for the next piece.
- 3 minBeginner
Does it plan what it's going to say? Explain lesson
A language model writes one piece at a time: it produces odds for the next piece, draws one, appends it, and starts over. There is no plan for the sentence, only the next piece, every time.
- 3 minBeginner
Why does the same prompt give different answers? Explain lesson
A language model does not pick the best next word; it rolls a loaded die over all possible next words. Temperature reshapes that die: low makes the top choice win almost always, high gives unlikely words a real chance.
- 8 minBeginner
How did it learn all this? Explain lesson
Training is a loop: the model guesses the next piece of real text, a loss score measures how wrong the guess was, and every number inside is nudged slightly downhill. Billions of small nudges, no rules, no teacher explaining anything.
- 5 minBeginner
Why is it so polite (and so cautious)? Explain lesson
After pretraining, people rank the model’s answers, a second model learns that taste, and the language model is nudged toward answers the taste model scores high. Politeness and caution are learned preferences, not built-in rules.
- 5 minBeginner
Does it learn from my chats? Explain lesson
No model changes while you chat. “Training on your data” means a saved copy of your chat may go into the data for a later model. Chat apps mostly do this unless you opt out; APIs mostly don’t.
Can I run a model on my own machine?
6 lessons
- 5 minBeginner
Why can I run Llama on my laptop but not GPT? Explain lesson
A model is a file of numbers called weights. Open-weights vendors like Meta publish that file, so it runs on your laptop or any host. Closed vendors keep it on their servers and sell answers through an API.
- 5 minBeginner
Why does a 7B model need 16 GB of RAM? Explain lesson
A model must hold every weight in memory: parameters × bytes per parameter, plus a KV cache that grows with the context. A 7B model needs about 14 GB at 16 bits and 4–5 GB at 4 bits.
- 5 minBeginner
How does a 30 GB model fit on my laptop? Explain lesson
Quantization stores each model weight with fewer bits, for example 4 instead of 16, so the file and memory shrink about four times. Quality barely moves down to 4 bits and collapses below that; the context needs memory on top.
- 5 minBeginner
Why is DeepSeek so cheap (and why is a 671B model fast)? Explain lesson
A Mixture of Experts model splits its layers into many experts, and a router sends each token to only a few. DeepSeek-V3 stores 671B parameters but uses 37B per token, so it runs like a far smaller model.
- 5 minBeginner
Why is the mini or flash model the free one? Explain lesson
Distillation trains a small student model to copy a big teacher model: its odds for every next word, not only its answer. The student keeps much of the skill with fewer parameters, so each token costs less to run.
- 5 minBeginner
Why does the free model feel dumber? Explain lesson
Free tiers often run a smaller or older model, with tighter limits. In one model family, the smaller sizes score far lower on knowledge and hard reasoning, while simple jobs like following a format barely change.
How do I get better answers?
9 lessons
- 3 minBeginner
Why does it forget what I said 20 messages ago? Explain lesson
The context window is the fixed number of pieces a model can read at once. Everything it answers from must fit inside it; anything older slides out and was simply never read this turn.
- 5 minBeginner
Why does it ignore the middle of my long document? Explain lesson
Lost in the middle: language models use information at the start or end of a long prompt far better than the same information in the middle. Accuracy draws a U across positions, even when everything fits in the context window.
- 5 minBeginner
Why does the model ignore my instructions? Explain lesson
A system prompt is text an app places before your conversation to set the model’s behaviour. It reaches the model in the same context as your messages; models are trained to rank it higher, which usually but not always wins.
- 5 minBeginner
Why is the second call cheaper? Explain lesson
Prompt caching reuses the work on a prompt’s opening when the next call starts with exactly the same text. Cached tokens are billed far below normal input, but only for long enough prefixes, sent again within minutes.
- 5 minBeginner
Why does the free tier say 429? Explain lesson
A 429 means a rate limit is full. Free APIs meter you several ways at once: requests per minute, tokens per minute, requests per day. Whichever fills first rejects calls until it frees up.
- 5 minBeginner
Why does it break my JSON? Explain lesson
A model writes JSON one token at a time, so nothing stops a token that breaks it. Structured output modes give the API your schema and block every token that cannot fit; values still need checking.
- 5 minBeginner
Why can a web page hijack my AI agent? Explain lesson
Prompt injection is text inside a model’s input that steers it. Instructions, your request and fetched pages arrive as one token stream, so a line in a page can act like a command. Defences help; none is fool-proof.
- 3 minBeginner
Why does it invent sources that don't exist? Explain lesson
A hallucination is a fluent, confident answer that is false. It happens because the model draws the most plausible next piece, and plausible and true are measured on different scales it cannot tell apart.
- 5 minBeginner
Why doesn't it know about last week? Explain lesson
A knowledge cutoff is the date a model’s training data stops. It is released months later and used for years, so anything newer reaches it only through the prompt: text you paste or results from a search tool.
Build with an LLM API
4 lessons
- 14 minBeginner
Call a chat completion API with curl and Go
Send one chat completion request with curl and with Go, read the reply and the token count, and see what a missing API key looks like.
- 14 minBeginner
Stream a model's answer as it is written
Turn on streaming, read the server-sent events with curl and then line by line in Go, and see why the first word arrives long before the last.
- 15 minBeginner
Let a model call your function with tool calling
Describe a function to the model, run it when the model asks, send back the result and read the final answer, step by step in Go.
- 15 minBeginner
Search by meaning with embeddings
Turn texts into vectors with an embeddings API, compare them with cosine similarity and rank help-centre documents for a question in Go.
Ship it
4 lessons
- 16 minBeginner
Get JSON you can parse from a model
Ask for JSON mode and a JSON schema, decode the reply into a Go struct, and check the fields before you trust them.
- 18 minBeginner
Answer from your own documents with a small RAG
Chunk a text, embed it, find the closest chunks to a question and put them into the prompt, so the model answers from your own documents.
- 14 minBeginner
Count tokens, cap the answer and estimate the cost
Read the usage a response reports, cap the answer with max_tokens, estimate the cost and keep a long prompt inside the context window.
- 18 minBeginner
Survive rate limits, timeouts and a provider that is down
Read a 429 and its headers, retry with a wait, stop on errors that will not fix themselves, and fall back to another provider on a timeout.
Free AI API credits to practise with
- AgentRouter Free Credits
Free AI coding credits: $25 per daily check-in and $50 per invite
No cardVerified
- Google Gemini Developer API Free Tier
Free Gemini API tier with rate-limited access to Flash models and Gemini 2.5 Pro
No cardVerified