Prompt Engineering
Master the art and science of crafting effective prompts for large language models. From basics to advanced techniques, learn how to communicate with LLMs to get reliable, structured outputs at scale.
What is prompt engineering?How LLMs workTokens and contextHallucinations and limitationsChoosing a modelSampling parametersTemperature and randomnessTop-k and top-pZero-shot promptingFew-shot promptingChain-of-thought promptingPrompt anatomyStep-back promptingSelf-consistency promptingTree of thoughtsReAct promptingSystem and role promptingStructured output formatsOutput length controlStop sequencesPrompt ensemblingPrompt injection attacksDefending against injectionPrompt debiasingEvaluation and testingPrompt tuningAutomatic prompt engineeringFine-tuning vs. promptingProduction patterns
What is prompt engineering?
- What is prompt engineering?Why the words you choose change what a language model produces, and what separates a disciplined prompt from a lucky guess.
- How LLMs workNext-token prediction, the mechanism behind every capability and every failure mode a large language model has.
- Tokens and contextHow text becomes numbers a model can process, and why the context window is a hard budget on everything it can see at once.
- Hallucinations and limitationsWhy models state falsehoods with total confidence, plus the other hard limits — knowledge cutoffs, no persistent memory, bounded reasoning.
Understanding models and their parameters
- Choosing a modelCapability, context window, latency, and cost are the four questions that decide which model a prompt should target.
- Sampling parametersAn LLM never deterministically picks the next word — sampling parameters control how it rolls the dice over a probability distribution.
- Temperature and randomnessTemperature reshapes a model's output distribution before sampling — the single knob that moves output from deterministic to wildly varied.
- Top-k and top-pTwo ways to truncate a model's output distribution before sampling — a fixed-size shortlist against one that adapts to the model's confidence.
The fundamental prompting strategies
- Zero-shot promptingAsking a model to do a task from a plain instruction alone, with no worked examples, and knowing when that is not enough.
- Few-shot promptingShowing a model worked examples of a task instead of just describing it, and knowing how many examples actually help.
- Chain-of-thought promptingAsking a model to reason step by step before answering, and why that measurably improves accuracy on multi-step problems.
- Prompt anatomyThe structural parts a well-built prompt is made of, and how labeling them clearly keeps a long prompt from confusing the model.
Sophisticated prompting methods
- Step-back promptingAnswer a general question first and a specific one second — abstraction before application reduces reasoning errors.
- Self-consistency promptingSample several independent reasoning paths and take the answer most of them agree on, instead of trusting just one.
- Tree of thoughtsBranch into multiple partial solutions, score them, and search the tree — for problems one straight-line reasoning chain can't solve.
- ReAct promptingInterleave reasoning with real actions and their observations in a loop — the pattern behind most tool-using agents.
- System and role promptingA standing instruction channel for the whole conversation, and a persona that reshapes tone and angle — two distinct, bounded tools.
Structuring and controlling outputs
- Structured output formatsSchema-constrained decoding, JSON mode, and the prompt-parse-retry fallback — three ways to get data back instead of prose.
- Output length controlWhy asking for "a short answer" and setting max_tokens are not the same control, and what happens when a response hits the limit mid-thought.
- Stop sequencesA string that halts generation the instant it appears — for closing structured output cleanly, stopping role drift, and cutting cost.
- Prompt ensemblingRunning several prompts or phrasings for the same task and aggregating their answers, so errors that don't correlate across them wash out.
Making prompts robust and safe
- Prompt injection attacksWhy a model can't tell its own instructions from data it's reading, and what that confusion lets an attacker do.
- Defending against injectionWhy there is no complete fix for prompt injection, and the layered defenses that reduce the risk instead of pretending to remove it.
- Prompt debiasingWhy asking a model nicely to "be unbiased" mostly doesn't work, and the prompting techniques that measurably do better.
- Evaluation and testingHow to know a prompt actually works, and the three eval methods that trade speed and cost against the kinds of mistakes each one catches.
Automating and tuning prompts
- Prompt tuningA parameter-efficient training technique that learns a soft prompt instead of writing one — not a synonym for prompt engineering.
- Automatic prompt engineeringLetting an LLM generate and score candidate prompts against your eval set, and DSPy's compiler-driven version of the same idea.
- Fine-tuning vs. promptingWhen prompting hits its ceiling, what fine-tuning actually buys you, and a decision rule for choosing between them.
- Production patternsVersioning prompts like code, guardrails before a response ships, monitoring for drift, and feeding user feedback back into the eval set.