Prompt Chaining
Sequential task decomposition
The Problem: Some tasks are too complex for a single prompt. How can we break them into stages and pass results between steps?
The Solution: Build a Production Line
Prompt chaining is the technique of breaking a complex task into a sequence of smaller, single-purpose prompts, where the output of one step becomes the input of the next. Instead of asking the model to research, structure, write, and edit an article all in one giant instruction, you run four focused calls in a row. It's like an assembly line in a factory: each station does one job well, and the half-finished product moves forward through clearly defined stages until it's complete.
How it works
Each link in the chain is a normal prompt — what makes it a chain is that your code, not the model, decides the order and passes data between steps. To keep the handoffs reliable, ask each step to return structured output (for example JSON) so the next prompt can read specific fields instead of parsing free-form prose. When a step needs real data or an action, function calling lets the model trigger a tool and feed the result into the chain. Because each call is independent, you can even use a different model or temperature per stage — a creative high-temperature model for brainstorming, then a precise low-temperature model for fact-checking.
When to use it — and the tradeoffs
Reach for chaining when a single prompt produces vague or inconsistent results, when the task has genuinely distinct phases (extract, then analyze, then summarize), or when intermediate results need validation before continuing. The big wins are reliability and debuggability: each step is short, easy to test, and easy to fix in isolation. The costs are real, though — more calls mean higher latency and price, and errors can cascade, since a mistake or hallucination early on poisons every step that follows. As a concrete example, to turn a customer review into a support ticket you might chain: (1) extract entities and the complaint as JSON, (2) classify sentiment and urgency from that JSON, then (3) draft a reply that uses both. Each step is trivial to verify on its own, whereas one mega-prompt doing all three at once is far harder to trust or correct.
Think of it like a factory assembly line:
- 1. Station 1: Extract key entities from the document
- 2. Station 2: Analyze sentiment for each entity
- 3. Station 3: Generate summary with sentiment context
- 4. Final product: Rich, structured analysis
Where Is This Used?
- Content Pipelines: Research → Draft → Edit → Format
- Data Processing: Extract → Transform → Validate → Load
- Analysis Workflows: Classify → Summarize → Recommend
- Code Generation: Plan → Implement → Review → Test
Fun Fact: Prompt chaining lets you use different models or temperatures for different stages! You might use a creative model for brainstorming and a precise model for fact-checking in the same pipeline.
Try It Yourself!
Use the interactive example below to see how chaining multiple prompts creates more sophisticated outputs than a single prompt could achieve.
Prompt Chaining
Break complex tasks into a sequence of simpler steps
Breaking down content creation into research → outline → draft → edit
Benefits of Prompt Chaining
- • Better quality control at each step
- • Easier to debug and improve
- • Ability to use different models per step
- • Overcome context limitations
// Prompt Chaining Example
async function createBlogPost(topic: string) {
// Step 1: Research
const research = await llm.complete({
prompt: `List 5 key facts about ${topic}`
});
// Step 2: Create outline using research
const outline = await llm.complete({
prompt: `Create a blog outline using: ${research}`
});
// Step 3: Write draft based on outline
const draft = await llm.complete({
prompt: `Write introduction based on: ${outline}`
});
// Step 4: Edit and polish
const final = await llm.complete({
prompt: `Improve for clarity: ${draft}`
});
return final;
}- • Content creation: research → outline → draft → edit
- • Code review: security → performance → style → report
- • Data analysis: collection → cleaning → analysis → visualization
- • Documentation: structure → sections → examples → final version
Frequently asked questions
What is prompt chaining and when should I use it?
Prompt chaining breaks a complex task into sequential LLM calls, where each step's output feeds into the next. Use it when a single prompt cannot reliably handle the full task — for example, research → outline → draft → edit for content creation.
How do I pass context between chained prompts?
Include the previous step's output directly in the next prompt, often with a clear label like 'Based on the following research: [output]'. Keep only relevant parts to avoid hitting context limits. Summarize long outputs before passing them forward.
What are the disadvantages of prompt chaining?
Main downsides: higher latency (multiple API calls), higher cost (more tokens), error propagation (mistakes in early steps compound), and complexity in error handling. Use single prompts when they produce acceptable results.
How many steps should a prompt chain have?
Most effective chains have 2-5 steps. Each step should have a clear, single purpose. If a chain exceeds 5 steps, consider whether some steps can be combined or if you need a different architecture like an agent loop.
Try it yourself
Interactive demo of this technique
Write a blog post about the benefits of morning walks
Morning walks are good for health. They help you wake up, improve mood, and boost immunity. Walking in the morning is good for the heart. Fresh air fills the body with oxygen. Walks also help with weight control. Try starting with 15 minutes a day.
The Simplest Life Hack You Keep Ignoring
Every morning you have a free tool that reduces stress by 25%, improves sleep, and burns 150 calories. You keep ignoring it — it is a simple walk.
What Happens in 30 Minutes
Within 5 minutes of walking, cortisol levels start dropping. By minute 15, your brain releases endorphins. By minute 30, blood flow to the brain increases by 15% — a Stanford study showed this directly boosts creativity.
Walking as an Antidepressant
A 2023 meta-analysis (British Journal of Sports Medicine) confirmed that 30 minutes of walking reduces anxiety symptoms on par with medication. Morning sunlight calibrates circadian rhythms, improving sleep quality by 42%.
How to Start (and Not Quit)
- Place your sneakers by the door the night before
- Start with 10 minutes, add 5 minutes each week
- Anchor to an existing habit: coffee → walk
Your Challenge: 7 Days
Tomorrow morning — 10 minutes. No excuses. In a week, your body will thank you.
The "outline → draft → edit" chain produces results impossible to achieve with a single prompt. Each step focuses on one task, passing the result forward — like an assembly line in a factory.
Create a free account to solve challenges
3 AI-verified challenges for this lesson
This lesson is part of a structured LLM course.
My Learning Path