Prompt Chaining Pattern
One big task, broken into small steps
Imagine writing an essay. You don't write the perfect final version in one go. First you jot an outline , then you write a rough draft , then you polish it. Each step builds on the one before. Prompt Chaining does exactly this with an LLM: it splits a task into a fixed line of steps , and each step's output becomes the next step's input.
Key points
- Split one hard task into a fixed sequence of easy LLM calls.
- Each step's output is the next step's input (like an assembly line).
- You can add a 'gate' check between steps to catch problems early.
The one-line definition
Prompt Chaining is a workflow where a task is broken into a fixed, ordered series of LLM steps. The result of step 1 is passed into step 2, step 2's result into step 3, and so on, until the final output is produced.
Note: Output of one prompt = input of the next. Like a relay race, baton after baton.
The chain: each step feeds the next
INPUT (a topic) │ ▼ ┌─────────────┐ │ STEP 1 │ make an OUTLINE │ LLM call │ └──────┬──────┘ │ outline ▼ ┌─────────────┐ │ STEP 2 │ write a DRAFT from the outline │ LLM call │ └──────┬──────┘ │ draft ▼ ┌─────────────┐ │ STEP 3 │ POLISH the draft │ LLM call │ └──────┬──────┘ │ final text ▼ ✅ FINISHED ESSAY
Adding a 'gate' check between steps
┌─────────┐ ┌──────────────┐ ┌─────────┐ │ STEP 1 │──► │ GATE 🚦 │──► │ STEP 2 │ │ outline │ │ Is the │ ok │ draft │ └─────────┘ │ outline │ └─────────┘ │ good enough? │ └──────┬───────┘ │ not ok ▼ ⛔ STOP / RETRY (don't waste the next expensive LLM call)
A tiny code example (read it like English)
Notice there is no clever loop here. It is just three LLM calls in a row, with the result of each passed straight into the next.
def write_essay(topic):
# STEP 1: outline
outline = llm(f"Write a 3-point outline about: {topic}")
# GATE: cheap safety check before the costly steps
if "1." not in outline:
return "Outline failed, stopping early."
# STEP 2: draft (uses STEP 1's output)
draft = llm(f"Write an essay using this outline:\n{outline}")
# STEP 3: polish (uses STEP 2's output)
final = llm(f"Fix grammar and improve flow:\n{draft}")
return final
When should you reach for prompt chaining?
| Scenario | Recommendation | Why |
|---|---|---|
| The task naturally splits into clear, ordered sub-steps | ✅ Use chaining | Each smaller step is easier and more accurate for the LLM. |
| You always do the same steps in the same order | ✅ Use chaining | The order is fixed and known ahead of time. |
| The next step changes depending on the input type | ❌ Use Routing instead | Chaining is a straight line; routing picks a branch. |
| A single prompt already gives a great answer | ❌ Just one call | Extra steps add cost and latency for no gain. |
Chaining mistakes beginners make
| Mistake | Consequence | Fix |
|---|---|---|
| Making the chain too long with tiny pointless steps. | Slow and expensive — every step is another LLM call you pay for. | Only split where it genuinely improves accuracy or control. |
| Not checking a step's output before feeding it forward. | A bad early output corrupts every later step (garbage in, garbage out). | Add a cheap gate check between expensive steps to fail fast. |
| Using chaining when the path should branch. | You force every input through the same steps even when it doesn't fit. | If the steps differ by input type, use the Routing pattern. |
Remember these lines
- Chaining = a fixed assembly line of LLM calls.
- Output of one step is the input of the next.
- Put a cheap gate before expensive steps to fail fast.
Key takeaways
- Prompt Chaining breaks a task into a fixed, ordered sequence of LLM steps.
- Each step's output is passed as the input to the next step.
- Optional gate checks between steps catch errors early and save cost.
- Use it when steps are fixed and ordered; use Routing when the path branches.
Frequently Asked Questions
What is Prompt Chaining Pattern?
Imagine writing an essay. You don't write the perfect final version in one go.
How does Prompt Chaining Pattern work?
Prompt Chaining is a workflow where a task is broken into a fixed, ordered series of LLM steps . The result of step 1 is passed into step 2, step 2's result into step 3, and so on, until the final output is produced.
What are the key takeaways about Prompt Chaining Pattern?
Prompt Chaining breaks a task into a fixed, ordered sequence of LLM steps. Each step's output is passed as the input to the next step. Optional gate checks between steps catch errors early and save cost. Use it when steps are fixed and ordered; use Routing when the path branches.
Related topics
Practice this on DevInterviewMaster
Read the full Prompt Chaining Pattern breakdown with interactive demos, quizzes, and Hinglish notes.
800+ system-design, LLD, coding, and design-pattern topics. Unlock everything with Pro (₹499, one-time) or Ultimate (₹999, one-time) — lifetime access, no subscription.