← Master Index
Vol. 13 Module 13.1 Lecture

Prompt Chaining

Prompting Techniques

How This Lesson Fits the Module & Volume

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.
Definition

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

Extract

Facts → JSON.

Reason

Decide / score.

Generate

User-facing text.

Check

Validate / gate.

ShapeFlowWhen
LinearA → B → CStable pipelines
Map-reduceMany chunks → mergeLong documents
RouterClassify then specialistMixed intents
RefineDraft → critique → reviseQuality-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

import json # Hop 1 — extract structured facts (cheap/fast model) extract_messages = [ {"role": "system", "content": "Return ONLY JSON: {entities: string[], dates: string[], amounts: number[]}"}, {"role": "user", "content": document_text}, ] facts = json.loads(call_llm(extract_messages, model="fast")) assert isinstance(facts.get("entities"), list) # Hop 2 — write the memo from validated facts (stronger model) memo_messages = [ {"role": "system", "content": "Write a 3-paragraph memo in Markdown. Use only provided facts."}, {"role": "user", "content": "Facts JSON:\n" + json.dumps(facts)}, ] memo = call_llm(memo_messages, model="strong") # Optional Hop 3 — policy check / guardrail model or rules engine on memo

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
Common Misconception

“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

  1. Short Answer: What is prompt chaining? Answer: Sequencing multiple prompts so each output feeds the next.
  2. True/False: Chains always cost less than one big prompt. Answer: False—often cost more but raise quality.
  3. Multiple Choice: Extract → reason → generate is a: (a) linear chain, (b) convolution, (c) dropout schedule. Answer: (a).
  4. Short Answer: Why use JSON between hops? Answer: Typed, validatable intermediates reduce ambiguity.
  5. True/False: Map-reduce chains help long documents. Answer: True.
  6. Multiple Choice: Validating only at the end risks: (a) error amplification, (b) free tokens, (c) perfect recall always. Answer: (a).
  7. Short Answer: Name one reason to use different models per hop. Answer: Cost/latency tiering (fast extract, strong write).
  8. Short Answer: What is a refine chain? Answer: Draft then critique/revise loops.
  9. Multiple Choice: Routers in chains typically: (a) classify then call a specialist prompt, (b) train ResNets, (c) flip bits randomly. Answer: (a).
  10. 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.
Trainer’s Guide

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.