Tokens & Context
Explain how LLMs generate text one token at a time, and reason about tokenization, sampling, context window limits and token budgets when designing prompts and conversations.
4 objectives 6 steps about 105 min
Learn it step by step
Read the lessons in order, try the practice checks, then prove the skill with the knowledge check.
This skill page is a public outline. Its lessons and knowledge check appear here as subject-matter experts approve them.
Learning objectives
- Count tokens for a prompt and response with a tokenizer and estimate the cost of a request.
- Allocate a context budget across instructions, history, retrieved content and output, and enforce it in code.
- Choose a strategy for long inputs (truncation, summarization or retrieval) and explain its quality trade-off, including position effects.
- Explain how a pretrained, instruction-tuned LLM generates text by sampling one token at a time, how temperature, top-p and top-k change that sampling, and why this produces hallucinations, knowledge cutoffs and run-to-run variation.
Lesson plan
6 steps, about 105 min · 5 of 5 lessons available so far
- How an LLM Writes: One Sampled Token at a Time Concept · 20 min
- Tokenizers in Practice: Counting Tokens and Cost Worked example · 15 min
- Context Engineering: What Goes In and How Much Concept · 15 min
- Lab: Trim a Conversation to Fit a Budget Lab · 25 min
- Long Inputs and Context Failure Modes Concept · 15 min
- Check: Generation and Context Budget Scenarios Knowledge check · 8 questions
Practise it
Write a function that assembles a chat request from a system prompt, past turns and retrieved passages under a fixed token budget, dropping or summarizing the oldest turns first; test it with a real tokenizer on short, medium and very long conversations. Then run one open-ended prompt 10 times at two temperature settings and report how many distinct answers each setting produced.
Build it from scratch
Practice builds that exercise this skill by re-creating a real technology.
- Build your own Large Language ModelAdvanced · 30–60 h
References
Hard prerequisites
None.
Unlocks
On these roadmaps
Progress and evidence for this skill are shared across all of them.