---
title: "Learn LLMs"
slug: "learn-llm"
description: "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."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-llm"
---

# 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 · 237 min

## 1. Why does ChatGPT do that?

- [Why does it miscount the r’s in "strawberry"?](https://aifree.dev/explain/tokens) (3 min, 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.
- [How does it know "king" is close to "queen"?](https://aifree.dev/explain/embeddings) (5 min, 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.
- [How does it know what "it" refers to?](https://aifree.dev/explain/self-attention) (5 min, 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.
- [What's inside the box?](https://aifree.dev/explain/transformer) (5 min, 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.
- [Does it plan what it's going to say?](https://aifree.dev/explain/next-token) (3 min, 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.
- [Why does the same prompt give different answers?](https://aifree.dev/explain/temperature) (3 min, 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.
- [How did it learn all this?](https://aifree.dev/explain/training) (8 min, 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.
- [Why is it so polite (and so cautious)?](https://aifree.dev/explain/rlhf) (5 min, 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.
- [Does it learn from my chats?](https://aifree.dev/explain/training-on-your-data) (5 min, 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.

## 2. Can I run a model on my own machine?

- [Why can I run Llama on my laptop but not GPT?](https://aifree.dev/explain/open-weights) (5 min, 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.
- [Why does a 7B model need 16 GB of RAM?](https://aifree.dev/explain/vram) (5 min, 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.
- [How does a 30 GB model fit on my laptop?](https://aifree.dev/explain/quantization) (5 min, 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.
- [Why is DeepSeek so cheap (and why is a 671B model fast)?](https://aifree.dev/explain/mixture-of-experts) (5 min, 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.
- [Why is the mini or flash model the free one?](https://aifree.dev/explain/distillation) (5 min, 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.
- [Why does the free model feel dumber?](https://aifree.dev/explain/free-vs-paid-models) (5 min, 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.

## 3. How do I get better answers?

- [Why does it forget what I said 20 messages ago?](https://aifree.dev/explain/context-window) (3 min, 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.
- [Why does it ignore the middle of my long document?](https://aifree.dev/explain/lost-in-the-middle) (5 min, 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.
- [Why does the model ignore my instructions?](https://aifree.dev/explain/system-prompt) (5 min, 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.
- [Why is the second call cheaper?](https://aifree.dev/explain/prompt-caching) (5 min, 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.
- [Why does the free tier say 429?](https://aifree.dev/explain/rate-limits) (5 min, 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.
- [Why does it break my JSON?](https://aifree.dev/explain/structured-output) (5 min, 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.
- [Why can a web page hijack my AI agent?](https://aifree.dev/explain/prompt-injection) (5 min, 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.
- [Why does it invent sources that don't exist?](https://aifree.dev/explain/hallucination) (3 min, 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.
- [Why doesn't it know about last week?](https://aifree.dev/explain/knowledge-cutoff) (5 min, 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.

## 4. Build with an LLM API

- [Call a chat completion API with curl and Go](https://aifree.dev/courses/learn-llm/chat-completion.md) (14 min): 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.
- [Stream a model's answer as it is written](https://aifree.dev/courses/learn-llm/streaming-responses.md) (14 min): 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.
- [Let a model call your function with tool calling](https://aifree.dev/courses/learn-llm/tool-calling.md) (15 min): 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.
- [Search by meaning with embeddings](https://aifree.dev/courses/learn-llm/embeddings-search.md) (15 min): Turn texts into vectors with an embeddings API, compare them with cosine similarity and rank help-centre documents for a question in Go.

## 5. Ship it

- [Get JSON you can parse from a model](https://aifree.dev/courses/learn-llm/json-output.md) (16 min): Ask for JSON mode and a JSON schema, decode the reply into a Go struct, and check the fields before you trust them.
- [Answer from your own documents with a small RAG](https://aifree.dev/courses/learn-llm/rag-basics.md) (18 min): 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.
- [Count tokens, cap the answer and estimate the cost](https://aifree.dev/courses/learn-llm/tokens-and-cost.md) (14 min): Read the usage a response reports, cap the answer with max_tokens, estimate the cost and keep a long prompt inside the context window.
- [Survive rate limits, timeouts and a provider that is down](https://aifree.dev/courses/learn-llm/errors-and-rate-limits.md) (18 min): 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

1. [AgentRouter Free Credits](https://aifree.dev/offer/agentrouter-free-credits.md) : Free AI coding credits: $25 per daily check-in and $50 per invite. No card. Verified 2026-11-28. Claim: https://agentrouter.org/register?aff=N5wg
2. [Google Gemini Developer API Free Tier](https://aifree.dev/offer/google-gemini-developer-api-free-tier.md) : Free Gemini API tier with rate-limited access to Flash models and Gemini 2.5 Pro. No card. Verified 2026-11-28. Claim: https://ai.google.dev/gemini-api/docs/pricing?ref=aifree.dev
3. [Groq Free Tier](https://aifree.dev/offer/groq-free-tier.md) : Free rate-limited Groq API access to hosted open models, no card. No card. Verified 2026-11-29. Claim: https://console.groq.com/?ref=aifree.dev

[All offers](https://aifree.dev/free-api-credits.md)
