Single prompts struggle with multi-stage work. Prompt chaining decomposes a workflow into sequenced LLM (and tool) calls—extract, then reason, then format—passing structured intermediates between steps. It builds on structured outputs and clean system/user roles.
Chaining is a lightweight alternative to full agents: fixed graphs you can test step-by-step with prompt evaluation and harden with guardrails.
Learning Objectives
By the end of this lesson, students should be able to:
- Define prompt chaining and contrast it with one-shot mega-prompts.
- Design a linear chain with typed intermediates (often JSON).
- Decide when to branch, retry, or stop early.
- Control cost/latency across multiple model calls.
- Log and evaluate each hop independently.
- Avoid error amplification by validating between steps.
Prompt chaining is a technique that splits a complex task into an ordered sequence of prompts (and optional tools), where each step’s output becomes input to the next, improving reliability versus asking one prompt to do everything.
Canonical Chain Shapes
Facts → JSON.
Decide / score.
User-facing text.
Validate / gate.
| Shape | Flow | When |
|---|---|---|
| Linear | A → B → C | Stable pipelines |
| Map-reduce | Many chunks → merge | Long documents |
| Router | Classify then specialist | Mixed intents |
| Refine | Draft → critique → revise | Quality-sensitive writing |
Chain when
- Multiple skills required
- Need inspectable intermediates
- Different models per hop
One-shot when
- Simple single skill
- Latency budget tiny
- Format already easy
Watch outs
- Cascading errors
- Multiplied token cost
- Opaque hop contracts
Practical Two-Hop Chain
Strengths
- Higher accuracy on hard tasks
- Debuggable intermediates
- Model tiering per step
Tradeoffs
- More latency and spend
- More moving parts to evaluate
- Bad hops poison later hops
“Chaining means never validating until the end.” Validate after each structured hop. A wrong JSON field in step 1 becomes confident nonsense in step 3. Pair chains with schema checks and the evaluation practices in prompt evaluation.
Knowledge Check
- Short Answer: What is prompt chaining? Answer: Sequencing multiple prompts so each output feeds the next.
- True/False: Chains always cost less than one big prompt. Answer: False—often cost more but raise quality.
- Multiple Choice: Extract → reason → generate is a: (a) linear chain, (b) convolution, (c) dropout schedule. Answer: (a).
- Short Answer: Why use JSON between hops? Answer: Typed, validatable intermediates reduce ambiguity.
- True/False: Map-reduce chains help long documents. Answer: True.
- Multiple Choice: Validating only at the end risks: (a) error amplification, (b) free tokens, (c) perfect recall always. Answer: (a).
- Short Answer: Name one reason to use different models per hop. Answer: Cost/latency tiering (fast extract, strong write).
- Short Answer: What is a refine chain? Answer: Draft then critique/revise loops.
- Multiple Choice: Routers in chains typically: (a) classify then call a specialist prompt, (b) train ResNets, (c) flip bits randomly. Answer: (a).
- True/False: Sibling guardrails can gate a chain before user delivery. Answer: True.
Key Takeaways
- Chains decompose hard tasks into testable hops.
- Pass structured intermediates; validate every step.
- Trade latency/cost for reliability and debuggability.
- Next: Guardrails.
Hands-on: Build extract→summarize for a messy PDF page; compare vs. one mega-prompt on faithfulness.
Discussion: When does a fixed chain become an agent with dynamic planning?
Recap: Prompt chaining sequences specialized calls with checked handoffs. Continue with Guardrails.