Prompt Engineering Tutorial
Learn Prompt Engineering step by step: 38 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Prompt Engineering? Learn what prompt engineering is, why the way you phrase a request changes a language model's answer, and how this course teaches it with runnable Python.
- How Language Models Read Prompts Understand tokens, context windows and next-token prediction, the mental model that explains why prompts behave the way they do. Includes runnable Python.
- Model Settings: Temperature, Top-p and Max Tokens Learn what temperature, top-p, max tokens and stop sequences do to a model's output, with numpy simulations that show how sampling really works.
- The Anatomy of a Prompt Break a prompt into parts: role, task, context, constraints, examples and output format. Build and test a reusable prompt builder in Python.
Prompting Fundamentals
- Writing Clear, Specific Instructions Turn vague requests into specific, testable instructions: say what to do, how much, for whom and what to avoid. Includes a runnable specificity checker.
- Giving Context and Naming the Audience Learn how background, purpose and audience change a model's answer, how much context is enough, and how to inject context from data with Python.
- Controlling Output Format and Length Ask for lists, tables, headings or exact word counts and verify the result in code. Learn format instructions, length control and post-processing checks.
- Delimiters and Prompt Templates Use delimiters, XML-style tags and templates to separate instructions from data, keep prompts maintainable and avoid confusion. Includes Python templating.
- Iterating and Versioning Prompts Improve prompts the disciplined way: change one thing at a time, test on a fixed set of inputs, diff versions and keep a changelog. Runnable Python included.
- Common Prompt Mistakes and How to Fix Them Ten frequent prompt-writing mistakes, from vague tasks and buried instructions to conflicting rules and fake certainty, each with a fix and a runnable checker.
Techniques: Few-shot, Chain-of-Thought and Roles
- Zero-Shot and Few-Shot Prompting Learn when to give a model no examples (zero-shot) or a few (few-shot), how many to use, and how to build few-shot prompts programmatically in Python.
- Choosing Good Few-Shot Examples Pick few-shot examples that work: cover the label space, include edge cases, balance classes, watch ordering bias and select examples dynamically in Python.
- Chain-of-Thought Prompting Learn chain-of-thought prompting: asking a model to reason step by step before answering. When it helps, when it does not, and how to separate reasoning from the answer.
- Self-Consistency and Prompt Chaining Combine several model runs by majority vote (self-consistency) and split big jobs into a chain of small prompts. Runnable Python simulations included.
- Roles, Personas and System Prompts Learn how roles and personas shape tone and focus, how system, user and assistant messages differ, and how to write and test a system prompt in Python.
Structured Output and Tool Use
- Getting Structured Output (JSON) from a Model Ask a model for JSON that your program can parse: schemas, field descriptions, examples, enums and strict output modes. Includes runnable Python schema tools.
- Validating and Repairing Model JSON Handle messy model output: extract JSON from chatter, fix common syntax slips, validate against a schema and retry with error feedback. Runnable Python.
- Function Calling and Tool Use Learn how tool use works: describing functions to a model, how it requests calls, writing good tool descriptions and dispatching calls safely in Python.
- The Tool-Calling Loop Build the loop behind assistants that use tools: send messages, run requested calls, feed results back and stop safely. Runnable simulation with a scripted model.
Retrieval and Context Engineering
- The Context Window and Token Budgets Learn what a context window is, how tokens are counted, and how to split a prompt budget between instructions, context, history and the answer.
- Retrieval-Augmented Generation (RAG) Basics Understand retrieval-augmented generation: find relevant text first, then ask the model to answer only from it. Build a tiny retriever and grounded prompt.
- Chunking Documents for Prompts Learn how to split documents into chunks for retrieval: fixed size, sentence and heading-based splitting, overlap, metadata and how to choose sizes.
- Ranking, Deduplicating and Assembling Context Turn raw retrieval hits into a clean prompt: pick top-k, remove near-duplicates, balance relevance with diversity, order passages and fit the token budget.
- Conversation Memory and Summarization Manage chat history in prompts: sliding windows, rolling summaries, pinned facts and long-term memory stores, with runnable examples and trade-offs.
Evaluation and Testing Prompts
- Building a Prompt Test Set Stop judging prompts by gut feeling. Build a small, representative test set with expected answers, edge cases and tags, and run it against any prompt.
- Metrics for Prompt Outputs Choose the right metric for each prompt: exact match, accuracy, precision and recall, token F1, rule checks, format validity and latency, with runnable code.
- Regression Testing and Comparing Prompts Compare two prompt versions case by case, spot regressions, handle non-deterministic outputs with repeated runs, and decide when a gain is real or noise.
- LLM-as-Judge and Scoring Rubrics Use a model to grade open-ended outputs: write clear rubrics, request structured verdicts, avoid judge bias and calibrate the judge against human labels.
Safety, Injection and Guardrails
- Prompt Injection: How Attacks Work Understand prompt injection and jailbreaks: why models cannot separate instructions from data, direct and indirect attacks, risks for agents, and how to threat-model.
- Defending Against Prompt Injection Learn layered defenses against prompt injection: delimiting untrusted text, least privilege for tools, human approval, output checks and tests that prove it holds.
- Guardrails and Output Checking Build guardrails around a language model: input checks, output validators, topic limits, moderation, safe fallbacks, and how to measure false positives and misses.
- Privacy and PII Handling in Prompts Keep personal data out of prompts and logs: detect and redact PII, use reversible placeholders, minimize context, and set retention rules for LLM applications.
Prompt Patterns in Production
- Prompt Libraries in Production Move prompts out of scattered strings into a versioned registry: templates with typed variables, release labels, safe rollouts, rollbacks and tests in your pipeline.
- Cost, Latency and Caching for LLM Apps Estimate and cut what a prompt costs: count tokens, shorten prompts, cache repeated work, route easy tasks to small models, stream output and batch background jobs.
- Monitoring and Feedback Loops for Prompts Watch a prompt after launch: structured logs, quality and cost metrics, user feedback, drift detection, alerts, and turning production failures into new test cases.
Projects and Practice
- Project: A Support Ticket Classifier Build a support ticket classifier end to end: write the prompt, validate JSON output, add a fallback, build a labelled test set, measure accuracy and fix the failures.
- Project: Grounded Question Answering Build a small retrieval-augmented question answerer: chunk and index documents, retrieve the best passages, assemble a cited prompt, check grounding and handle unanswerable questions.
- Practice: Prompt Review Challenges Practice finding and fixing weak prompts: ambiguous instructions, missing formats, leaky templates, unsafe tools and untestable goals, with a self-check linter and answer notes.