05. Design Patterns
Prompt Chaining
Prompt chaining splits a hard job into a sequence of LLM calls where each step’s structured output feeds the next prompt. It is a foundational design pattern — simpler than a full agent loop, and often embedded inside one.
What you will learn
- Define prompt chaining and when to use it
- Contrast chains with ReAct-style agent loops
- Design step contracts (schemas) between prompts
- Spot failure modes like error amplification
- Answer interview questions with short, solid answers
Definition
Prompt chaining is a sequential pattern: run prompt 1, take its output (ideally structured), inject it into prompt 2, and so on until a final result. Each hop has a narrow job — extract, classify, outline, draft, critique, format.
It is the LLM version of a pipeline. In Agentic AI curricula it sits among design patterns because many agents still use chains for stable subpaths even when the outer loop is dynamic.
Introduction
This lesson opens Module 05 (Design Patterns). Module 04 covered workflows; patterns are reusable shapes you drop into those workflows.
Next: Routing and Parallelization — how chains branch and fan out.
Why chain prompts?
- One mega-prompt is hard to eval and debug
- Narrow steps raise accuracy on structured subtasks
- You can swap or cache individual hops
- Cheaper/smaller models can run early hops
Chain vs agent loop
Do not market every chain as an “autonomous agent.”
- Chain - predetermined LLM hops; limited live tool adaptivity
- Agent loop - chooses actions from observations (ReAct, tools)
- Hybrid - agent routes into a chain for a stable sub-workflow
- Workflow graphs often embed chains as sequential LLM nodes
Anatomy of a good chain
- Step goal - one verb (extract, score, draft)
- Input contract - schema or fields required
- Output contract - JSON/schema the next hop trusts
- Validator - code checks between hops
- Failure path - retry hop, skip, or escalate — not silent garbage-in
Example: research brief chain
Keep artifacts structured early so later prose cannot invent claims.
- Extract claims as JSON from sources
- Outline sections from claims only
- Draft prose constrained by outline
- Critique against a rubric; loop or ship
Common chain shapes
- Linear - A → B → C
- Map-reduce - many map prompts, then a reduce prompt
- Generate–critique - draft then critic (may iterate)
- Gate - classifier hop chooses which chain continues
- Tool sandwich - LLM → tool → LLM (edge of agent territory)
Failure modes
- Error amplification - bad hop 1 poisons all later hops
- Format drift - free text handoffs break parsers
- Latency/cost stack - N model calls per user request
- Hidden state - burying critical fields only in prose
- Over-chaining - more hops than the task needs
Program / implementation notes
Prefer structured outputs between hops. Version each prompt template. Eval hops independently with fixtures before end-to-end tests.
- Name chain steps in traces (chain.extract, chain.draft)
- Fail closed when validation fails — do not feed invalid JSON forward
- Cache stable early hops when inputs repeat
- Next: Routing and Parallelization Patterns
Advantages and limitations
- Advantage: clearer debugging than one giant prompt
- Advantage: mix model sizes per hop for cost control
- Advantage: easy to place validators and HITL between hops
- Limitation: not enough when the path must adapt via tools
- Limitation: cumulative latency
- Limitation: brittle without schemas and tests
Key points
- Prompt chaining = sequenced LLM steps with handoffs.
- Use narrow jobs and structured contracts between hops.
- Validate outputs before feeding the next prompt.
- Chains ≠ full agents; agents may embed chains.
- Watch error amplification, format drift, and cost stacking.
- Eval each hop, then the whole chain.
FAQs
Is LangChain “a chain” the same as this pattern?
LangChain popularized chain abstractions; the design pattern exists independently — sequenced LLM (and tool) steps with pass-through state.
Should every hop use the strongest model?
No. Early extract/classify hops often work with smaller/cheaper models; reserve stronger models for hard reasoning or final drafting.
When should I upgrade a chain to an agent?
When success depends on unknown tool results, branching under uncertainty, or multi-step recovery that a fixed sequence cannot express.
How do I stop garbage from propagating?
Schema-validate each hop’s output in code; on failure retry that hop or stop — never pass invalid artifacts downstream.
Interview questions
- What is prompt chaining?
Answer A pattern that splits a task into sequential LLM calls where each step’s output becomes input to the next, usually with structured handoffs.
- Why use prompt chaining instead of one big prompt?
Answer Narrow steps are easier to evaluate, debug, and optimize, and often improve reliability on multi-part tasks.
- Prompt chaining vs an AI agent?
Answer Chaining follows a mostly fixed LLM pipeline; an agent dynamically chooses tools/actions from observations. Agents may contain chains.
- What should pass between chain steps?
Answer Prefer validated structured data (JSON/schema fields) over unconstrained prose so later hops cannot invent missing facts.
- Name a failure mode of prompt chaining.
Answer Error amplification: a bad early extraction silently corrupts every downstream step if you do not validate handoffs.
- Give a practical example.
Answer Support macros: classify intent → extract slots → draft reply → tone check — each a separate prompt with validators between them.
- How do chains fit agentic workflows?
Answer As sequential LLM nodes inside a larger graph that may also route, parallelize, call tools, and apply HITL gates.
Related topics
Practice task
Complete each numbered practice step in a notebook or doc before opening the next lesson.
- Design a 4-hop chain for “turn a meeting transcript into action items.”
- Write the JSON schema for hop 1 and hop 2 outputs.
- List three validators you would run between hops.
- Explain in five lines when this should become a tool-using agent instead.
- Answer interview questions 1, 3, and 5 in two sentences each.