01 / 16

LLM Fundamentals

What a model actually is, how it reads, and why it fails.

0/15 studied
01Tokens & tokenization

Models read, limit and bill text in tokens: subword chunks from a fixed vocabulary, ~4 characters or ~0.75 words of English each. Spanish, code and non-Latin scripts need more tokens per idea.

Core◇ Diagram</> Code
02Embeddings

An embedding model turns text into a fixed-length vector so that similar meanings land close together; compare vectors with cosine similarity to search, cluster, classify or deduplicate by meaning instead of keywords.

Core◇ Diagram</> Code
03Transformer architecture

A decoder-only transformer embeds tokens, runs them through N identical blocks of attention plus MLP joined by residuals, and outputs next-token logits. It trains on all positions in parallel, which is why it beat RNNs.

Core◇ Diagram</> Code
04Self-attention

Each token builds a query, key and value; it scores its query against every earlier token's key, softmaxes the scores and takes a weighted sum of values. Cost grows O(n²) with context length.

Core◇ Diagram</> Code
05Context window

The maximum tokens a model handles in one call, input and output combined: 128k to 1M+ today. Usable quality degrades well before the advertised limit, and every token costs money and latency.

Core◇ Diagram</> Code
06Decoding & sampling (temperature, top-p)

The model scores every token; decoding picks one. Use low temperature for facts, code and tool calls, higher temperature with top-p or min-p for variety. Even temperature 0 isn't bit-for-bit reproducible.

Core◇ Diagram</> Code
07KV cache

During generation, store every token's attention keys and values so each new token reuses them instead of recomputing the prefix; the price is GPU memory that grows with context length × concurrent requests.

Core◇ Diagram</> Code
08How LLMs are trained

Pretraining on trillions of tokens builds knowledge into a base model; SFT teaches it to follow instructions; preference tuning (RLHF/DPO) shapes style and safety; RL with verifiable rewards teaches it to reason on checkable problems.

Core◇ Diagram</> Code
09Scaling laws & Chinchilla

Loss falls predictably as a power law in parameters, data and compute. Chinchilla's compute-optimal mix is ~20 tokens per parameter, but labs overtrain small models because inference, not training, dominates lifetime cost.

Advanced◇ Diagram</> Code
10Mixture of Experts (MoE)

Replace the dense feed-forward block with many expert FFNs and a router that sends each token to only the top-k, so total parameters (capacity) grow while compute per token stays at the active subset.

Advanced◇ Diagram</> Code
11Reasoning models & test-time compute

LLMs trained with RL to think at length before answering. You buy accuracy on hard problems by spending more thinking tokens, billed as output and paid for in seconds of latency.

Core◇ Diagram</> Code
12Hallucinations: why they happen

LLMs predict plausible text, not true text, and training and benchmarks reward guessing over "I don't know". You can't remove hallucinations; you ground, verify, allow abstention and measure the residual rate.

Core◇ Diagram</> Code
13Multimodal models

Images and audio are encoded into token-like vectors the LLM reads alongside text. They cost tokens (~85 to a few thousand per image) and fail on tiny text, counting and precise spatial reasoning.

Core◇ Diagram</> Code
14Encoder vs decoder models (BERT vs GPT)

Encoders (BERT) read text in both directions and output vectors for classification, embeddings and rerankers. Decoders (GPT) predict the next token and generate. Decoder-only won the frontier; small encoders still win cheap, high-volume understanding.

Core◇ Diagram</> Code
15Choosing a model

Pick the cheapest model that clears your quality bar on your own evals, within latency, cost and compliance limits. Prove value with the best model, then route or distill down behind a swappable abstraction.

Advanced◇ Diagram</> Code