Prompting & Context
Prompt fundamentals, few-shot, chain/tree of thought, context engineering, structured output and injection defense.
- lessons
- 8
- steps
- 67
- time
- 90 min
Prompt Engineering Fundamentals
Treat a prompt like code: state the intent, fence the data, respect the instruction hierarchy, and get consistency from the system rather than from temperature.
- Write prompts as intent plus constraint
- Explain the instruction hierarchy and why it is not a security boundary
- Use roles for priorities and delimiters to separate data from instructions
- Get reproducible outputs now that APIs reject temperature
Few-Shot and In-Context Learning
Teach a task with examples in the prompt instead of training: how many, which ones, in what order, and when many-shot beats fine-tuning.
- Build a good example: input, optional rationale, gold output
- Pick the number of examples by model size
- Select examples per query and keep them out of the cached prefix
- Avoid label and recency bias
Chain-of-Thought
Intermediate reasoning steps make hard problems tractable. Reasoning models now do this internally, so you control them with effort, not with prompts.
- Explain why intermediate steps help on multi-step problems
- Choose between zero-shot, few-shot and programmatic CoT
- Control reasoning models with effort, and avoid reasoning-extraction refusals
- Monitor actions rather than chains of thought
Tree-of-Thought
Explore several reasoning paths, score them, prune dead ends and backtrack. Know when an explicit search beats one long reasoning call.
- See why a single chain can't recover from an early mistake
- Run the propose → evaluate → search loop
- Explain why the evaluator decides whether ToT works
- Weigh ToT's cost against a reasoning model's internal search
Context Engineering
A 1M-token window is a budget, not free space. Curate the smallest high-signal set of tokens on every turn, and keep the prefix cacheable and append-only.
- Explain context rot and budget each part of the window
- Apply compaction, just-in-time loading and sub-agent isolation
- Order a prompt for attention and for the cache
- Keep the harness append-only so reasoning and caches survive
Structured Generation
Get JSON that always parses by constraining decoding at the engine, not by asking nicely, then validate the content and retry with the error.
- Explain how a grammar mask makes invalid output impossible
- Constrain self-hosted models with regex and grammars
- Replace forced tool calls and prefill on the newest Claude models
- Split hard extractions and recover from validation errors
Prompt Optimization with DSPy
Stop hand-tuning prompt text. Declare what each step does, define a metric, and let an optimizer compile the prompts, and even the program shape, for each model.
- See why hand-tuned prompts break when the model changes
- Separate logic (signatures, modules) from prompt text
- Design the metric the optimizer will chase
- Tell MIPROv2, GEPA, Flex and ReAnchor apart
Prompt Injection and Defense
Some injections will always get through, so design the system so that a successful injection has nothing dangerous to do.
- Tell direct from indirect injection
- Read current attack-success baselines and what 1% means at scale
- Separate privileges with the dual-LLM pattern
- Break the lethal trifecta and bind approvals to actions