Lesson 1 · 7 topics · 20 min · intermediate
Anatomy of a Prompt: Roles, Templates and Instruction Hierarchy
Start lessonYou will be able to
- Name the parts of a prompt (task description, examples, the task) and identify each in a real prompt.
- Explain system, user and assistant messages, and why the model receives them as one sequence.
- Build the exact token sequence a chat template produces from a list of messages, and count what the template adds.
- Recognise common template mistakes (extra newline, missing generation prompt, doubled begin-of-text, markers encoded as text, another model's format) and check for them by comparing token IDs.
- Apply the instruction hierarchy: which instruction wins when system, user and tool or retrieved text disagree, and why that is trained behaviour rather than a guarantee.
- Write clear, explicit instructions with format, markers and personas, and keep prompts short and static-first because every token costs.
Topics
Your review
Sign in to keep your quick-check answers. The ones you miss come back for review after 1, 3 and 7 days, until they stick.
Sign in freeSources
AI Engineering · Chip Huyen, 2025
Chip Huyen. O'Reilly. Used: Ch. 5 "Prompt Engineering": "Introduction to Prompting", "System Prompt and User Prompt", "Prompt Engineering Best Practices" > "Write Clear and Explicit Instructions" ("Ask the model to adopt a persona", "Provide examples", "Specify the output format"), "Defensive Prompt Engineering" > "Indirect prompt injection" and "Model-level defense".
Hands-On Large Language Models · Jay Alammar & Maarten Grootendorst, 2024
Jay Alammar & Maarten Grootendorst. O'Reilly. Used: Ch. 6 "Prompt Engineering": "The Basic Ingredients of a Prompt", "Instruction-Based Prompting", "The Potential Complexity of a Prompt".
The Instruction Hierarchy: Training LLMs to Prioritize Privileged Instructions · Eric Wallace et al., 2024
Eric Wallace, Kai Xiao, Reimar Leike, Lilian Weng, Johannes Heidecke, Alex Beutel. arXiv:2404.13208. Used: Fetched 2026-10-21: abstract; §1 Introduction (priority order, misaligned instructions ignored, 63% on system-prompt extraction); §3.1 (aligned vs misaligned). Open source
Meta Llama 3 model card and prompt formats · Meta
Meta. Llama documentation. Used: Fetched 2026-10-21: special tokens, multi-turn example, "Newlines (0x0A) are part of the prompt format". Open source
llama3: llama/tokenizer.py (ChatFormat) · Meta
Meta. GitHub. Used: Commit a0940f9, fetched 2026-10-21: encode_header, encode_message, encode_dialog_prompt; special-token list; pat_str. Open source
Chat templates · Hugging Face
Hugging Face. Transformers documentation. Used: Fetched 2026-10-21: Zephyr template output, add_generation_prompt, warning about duplicated special tokens. Open source