Lesson 10: Prompt Chaining & Sequential Processing
Connect multiple prompts to build complex workflows where each output feeds into the next.
The Power of Prompt Chaining
Single prompts have limitations — they can only process so much information and produce so much output. Prompt chaining breaks complex tasks into sequential steps, where each step's output becomes the next step's input.
Why Chain Prompts?
| Benefit | Description |
|---|---|
| Complexity Management | Handle tasks too large for a single prompt |
| Quality Control | Review and refine at each stage |
| Error Isolation | Fix issues at specific steps without starting over |
| Specialization | Each prompt can be optimized for its specific task |
| Debugging | Easier to identify where things go wrong |
Mental model: Think of prompt chaining like an assembly line — each station does one thing well, and the product gets refined as it moves down the line.
Basic Chaining Patterns
Pattern 1: Linear Chain
The simplest pattern — output of A feeds into B, B into C, etc.
Step 1: Research → Generate outline
Step 2: Outline → Write first draft
Step 3: Draft → Edit and refine
Step 4: Refined → Add formatting and polish
Example: Blog Post Creation
Chain Step 1:
"Research the topic: 'AI in Healthcare 2026'
Provide: 5 key trends, supporting data, notable examples"
[Output: Research findings]
Chain Step 2:
"Based on this research: [Step 1 output]
Create a blog post outline with:
- 5 main sections based on the trends
- Key points for each section
- Suggested headlines"
[Output: Structured outline]
Chain Step 3:
"Using this outline: [Step 2 output]
Write a complete 1500-word blog post.
Include: Introduction, detailed sections, conclusion with CTA"
[Output: First draft]
Chain Step 4:
"Edit this draft for:
- Grammar and clarity
- Engagement and flow
- SEO optimization
- Consistency in tone
Draft: [Step 3 output]"
[Output: Final polished post]
Pattern 2: Branching Chain
Split into parallel paths, then merge results.
Step 1: Analyze input → Identify components
Step 2a: Process component A
Step 2b: Process component B
Step 2c: Process component C
Step 3: Merge all results → Final output
Example: Competitive Analysis
Step 1: "List the top 5 competitors for [product]"
Step 2a: "Analyze Competitor 1: [name]"
Step 2b: "Analyze Competitor 2: [name]"
Step 2c: "Analyze Competitor 3: [name]"
(Run in parallel)
Step 3: "Based on these competitor analyses:
[Results from 2a, 2b, 2c]
Create a comparative analysis with:
- Feature comparison table
- Strengths/weaknesses matrix
- Market positioning map
- Strategic recommendations"
Pattern 3: Iterative Refinement
Repeat a step until quality criteria are met.
Step 1: Generate initial output
Step 2: Evaluate against criteria
Step 3: If quality < threshold, refine and repeat Step 2
Step 4: Output final result
Example: Code Generation
Iteration 1:
"Write a Python function that [specification]"
Iteration 2:
"Review this code for:
- Correctness
- Edge cases
- Performance
- Readability
Code: [Iteration 1 output]
Identify issues and provide improved version."
Iteration 3:
"Final review:
- Are all edge cases handled?
- Is error handling robust?
- Is the code production-ready?
Code: [Iteration 2 output]
If issues remain, fix them. Otherwise, confirm it's ready."
Chain Design Principles
1. Define Clear Interfaces
Each step should have clear inputs and outputs:
Step 1 Output Format:
{
"findings": ["list of insights"],
"confidence": "high/medium/low",
"sources": ["list of sources"]
}
Step 2 Input Requirements:
- findings: list of strings
- confidence: string
- sources: list of strings (optional)
2. Minimize Dependencies
When possible, make steps independent so they can run in parallel:
# Bad: Each step depends on previous
Step 1 → Step 2 → Step 3 → Step 4
# Better: Independent steps can parallelize
Step 1 → Step 2a ↘
Step 1 → Step 2b → Step 3
Step 1 → Step 2c ↗
3. Include Validation Checkpoints
Add quality gates between steps:
Step 1: Generate outline
[VALIDATION: Does outline cover all requirements?]
Step 2: Write content
[VALIDATION: Is content accurate and complete?]
Step 3: Edit and polish
[VALIDATION: Does final meet quality standards?]
4. Handle Errors Gracefully
Plan for failures at each step:
Step 1: Generate content
If Step 1 fails:
- Log the error
- Retry once with simplified requirements
- If still fails, output partial results with explanation
Complex Workflow Example: Content Pipeline
INPUT: Blog topic + target audience
Step 1: Research
- Generate 5 key points
- Find supporting data
- Identify examples
OUTPUT: Research brief
Step 2: Outline
- Create section structure
- Assign key points to sections
- Write section headers
OUTPUT: Detailed outline
Step 3: Draft (parallel per section)
- Section 1 draft
- Section 2 draft
- Section 3 draft
OUTPUT: Raw sections
Step 4: Integrate
- Combine sections
- Add transitions
- Ensure flow
OUTPUT: Complete draft
Step 5: Optimize
- SEO optimization
- Readability check
- Engagement enhancement
OUTPUT: Optimized draft
Step 6: Polish
- Grammar check
- Formatting
- Final review
OUTPUT: Publication-ready post
Common Chaining Mistakes
- Mistake: Chains too long (10+ steps) — Fix: Consolidate steps or break into sub-chains.
- Mistake: No validation between steps — Fix: Add quality checkpoints.
- Mistake: Tight coupling between steps — Fix: Define clear interfaces.
- Mistake: Not handling failures — Fix: Plan error recovery for each step.
- Mistake: Passing too much context — Fix: Summarize between steps when possible.
Professional Tips & Tricks
- Start with simple 2-3 step chains, then expand as needed.
- Document your chains — they become reusable workflows.
- Use version control for prompt chains — track what works.
- Test chains with edge cases, not just happy paths.
Key Takeaways
- Prompt chaining breaks complex tasks into manageable steps.
- Linear, branching, and iterative are the main chain patterns.
- Define clear interfaces between steps for maintainability.
- Include validation checkpoints to catch issues early.
- Plan for errors at each step of the chain.
Next up: Orchestrating multiple AI agents and parallel processing.
# Prompt Chaining Patterns
## Linear Chain
Step 1 → Step 2 → Step 3 → Output
Example:
1. Research topic
2. Create outline
3. Write draft
4. Edit and polish
## Branching Chain
Step 1 → Step 2a ↘
Step 1 → Step 2b → Step 3
Step 1 → Step 2c ↗
Example:
1. List competitors
2a. Analyze Competitor A
2b. Analyze Competitor B
2c. Analyze Competitor C
3. Comparative analysis
## Iterative Refinement
Step 1 → Evaluate → [if quality < threshold] → Refine → Evaluate → Output
Example:
1. Generate code
2. Review for issues
3. Fix issues
4. Final review
## Chain Interface Template
Step N Output:
{
"result": "output data",
"metadata": {
"confidence": "high/medium/low",
"completeness": "percentage"
}
}
Step N+1 Input:
- Requires: result from Step N
- Optional: metadata for validation
## Validation Checkpoints
Step 1: Generate → [CHECK: Meets requirements?] → Step 2
Step 2: Process → [CHECK: Accurate?] → Step 3
Step 3: Refine → [CHECK: Quality standard?] → OutputLesson Code (Python)
# Prompt Chaining Patterns
## Linear Chain
Step 1 → Step 2 → Step 3 → Output
Example:
1. Research topic
2. Create outline
3. Write draft
4. Edit and polish
## Branching Chain
Step 1 → Step 2a ↘
Step 1 → Step 2b → Step 3
Step 1 → Step 2c ↗
Example:
1. List competitors
2a. Analyze Competitor A
2b. Analyze Competitor B
2c. Analyze Competitor C
3. Comparative analysis
## Iterative Refinement
Step 1 → Evaluate → [if quality < threshold] → Refine → Evaluate → Output
Example:
1. Generate code
2. Review for issues
3. Fix issues
4. Final review
## Chain Interface Template
Step N Output:
{
"result": "output data",
"metadata": {
"confidence": "high/medium/low",
"completeness": "percentage"
}
}
Step N+1 Input:
- Requires: result from Step N
- Optional: metadata for validation
## Validation Checkpoints
Step 1: Generate → [CHECK: Meets requirements?] → Step 2
Step 2: Process → [CHECK: Accurate?] → Step 3
Step 3: Refine → [CHECK: Quality standard?] → OutputConsole Output
Prompt Chaining Patterns
## Linear Chain
Step 1 → Step 2 → Step 3 → Output
Example:
1. Research topic
2. Create outline
3. Write draft
4. Edit and polish
## Branching Chain
Step 1 → Step 2a ↘
Step 1 → Step 2b → Step 3
Step 1 → Step 2c ↗
Example:
1. List competitors
2a. Analyze Competitor A
2b. Analyze Competitor B
2c. Analyze Competitor C
3. Comparative analysis
## Iterative Refinement
Step 1 → Evaluate → [if quality < threshold] → Refine → Evaluate → Output
Example:
1. Generate code
2. Review for issues
3. Fix issues
4. Final review
## Chain Interface Template
Step N Output:
{
"result": "output data",
"metadata": {
"confidence": "high/medium/low",
"completeness": "percentage"
}
}
Step N+1 Input:
- Requires: result from Step N
- Optional: metadata for validation
## Validation Checkpoints
Step 1: Generate → [CHECK: Meets requirements?] → Step 2
Step 2: Process → [CHECK: Accurate?] → Step 3
Step 3: Refine → [CHECK: Quality standard?] → OutputCode Visualization Tips
- Draw flowcharts for each chaining pattern (linear, branching, iterative).
- Create a decision tree for choosing the right chain pattern.
- Map out a complete workflow with validation checkpoints.
Professional Tips & Tricks
- Start with simple 2-3 step chains, then expand as needed.
- Document your chains — they become reusable workflows.
- Test chains with edge cases, not just happy paths.
Python Code Judge & Practice Arena
LeetCode StyleRun real Python 3.12 WebAssembly code directly in your browser against automated test suites.
Chain Design Exercise
Test Your Knowledge
Instant feedbackQuick Check: Prompt Chaining & Sequential Processing
Up next · Continue learning
Multi-Agent Orchestration
Coordinate multiple AI agents to work together on complex tasks, each specializing in different aspects.