Prompt Chaining (also known as the Pipeline Pattern) breaks down complex tasks into a sequence of smaller, focused steps. Each step involves an LLM call or processing logic, using the output of the previous step as input for the next.
This pattern improves the reliability, transparency, and manageability of complex interactions with language models by applying a "divide-and-conquer" approach.
Input → Step 1 (LLM/Logic) → Step 2 (LLM/Logic) → Step 3 (LLM/Logic) → Final Output
↓ ↓ ↓
Intermediate Intermediate Intermediate
Output Output Output
---
title: Prompt Chain — Laptop Spec to JSON
---
%%{init: {'look':'handDrawn','theme':'base','themeVariables':{'background':'#f5ecd9','primaryColor':'#ede0bd','primaryBorderColor':'#6b4423','primaryTextColor':'#3e2723','lineColor':'#6b4423','clusterBkg':'#efe5cd','clusterBorder':'#c5b393','fontFamily':'Caveat, Patrick Hand, cursive'}}}%%
flowchart LR
In([raw text:<br/>'3.5 GHz octa-core,<br/>16GB RAM, 1TB NVMe'])
Out([JSON:<br/>cpu · memory · storage])
subgraph extract ["step 1 — extraction"]
P1[[prompt: spec extractor]]
L1{{ChatOpenAI}}
P1 --> L1
end
Mid[/plain-text specs/]
subgraph transform ["step 2 — transformation"]
P2[[prompt: JSON formatter]]
L2{{ChatOpenAI}}
P2 --> L2
end
In --> P1
L1 --> Mid --> P2
L2 --> Out
LCEL composition mirrors the diagram: prompt | llm | parser for each step, with the first step's output piped in as {"specifications": extraction_chain} to the second step's prompt (src/chain_prompt.py:107-130). The intermediate plain-text extraction is inspectable — when the final JSON is wrong, you can tell whether extraction or formatting broke.
Each step in the chain:
- Receives input from the previous step (or user)
- Processes the input through an LLM call or logic function
- Produces output that becomes input for the next step
- Maintains context by passing relevant information forward
- Focuses the model on one specific operation at a time
- Reduces complexity and potential for errors
- Easier to debug when issues occur
- Clear visibility into each processing step
- Intermediate outputs can be inspected and validated
- Makes the reasoning process explicit
- Fine-tune individual steps independently
- Insert validation logic between steps
- Modify or extend the pipeline without rebuilding from scratch
- Mix LLM calls with traditional processing logic
- Conditional branching based on intermediate results
- Easy to A/B test different step implementations
- Multi-step reasoning: Tasks requiring sequential logical steps
- Document processing: Extract → Analyze → Summarize → Format
- Content generation: Research → Outline → Draft → Edit → Polish
- Data transformation: Parse → Validate → Transform → Enrich → Output
- Complex decision-making: Gather context → Analyze options → Make recommendation
- Quality assurance: Generate → Critique → Revise → Validate
- Simple, single-step tasks that don't benefit from decomposition
- Real-time applications where latency from multiple LLM calls is prohibitive
- Tasks where context from all steps must be processed simultaneously
- Highly iterative workflows better suited for agent-based patterns
Modern frameworks provide robust tools for building prompt chains:
- LangChain/LangGraph: Python-based framework with extensive chain primitives
- Google ADK (Agent Development Kit): Google's agent building toolkit
- Haystack: NLP framework with pipeline support
- Semantic Kernel: Microsoft's SDK for AI orchestration
# Conceptual example
def research_step(topic):
return llm.generate(f"Research key facts about: {topic}")
def outline_step(research):
return llm.generate(f"Create an outline based on: {research}")
def draft_step(outline):
return llm.generate(f"Write a draft following: {outline}")
# Chain execution
topic = "Artificial Intelligence"
research = research_step(topic)
outline = outline_step(research)
final_draft = draft_step(outline)- Stateless chains: Each step is independent (easier to scale)
- Stateful chains: Maintain context across steps (more flexible)
- Implement retry logic for failed LLM calls
- Provide fallback strategies for individual steps
- Validate outputs between steps
- Cache intermediate results where appropriate
- Run independent steps in parallel when possible
- Consider batch processing for similar chains
- Log input/output for each step
- Track execution time per step
- Monitor token usage across the chain
Execute different steps based on intermediate results:
Input → Classify → [Route A: Steps 1-3] or [Route B: Steps 4-6] → Output
Loop back to earlier steps for quality improvement:
Draft → Critique → [Good? → Output] or [Poor? → Revise → Critique]
Execute multiple chains simultaneously and merge results:
Input → [Chain A] → Merge → Output
→ [Chain B] →
→ [Chain C] →
- Keep steps focused: Each step should have a single, clear purpose
- Design for observability: Make intermediate outputs inspectable
- Validate aggressively: Check outputs between steps
- Plan for failure: Implement graceful degradation
- Document the flow: Make the chain's logic explicit and maintainable
- Test incrementally: Validate each step before adding the next
- Consider cost: Balance chain length with API token usage
Prompt chaining provides a foundational pattern for building sophisticated AI systems that go far beyond single-prompt capabilities. By deconstructing complex problems into manageable sub-tasks, this approach:
- Enhances reliability through focused, single-purpose operations
- Improves control over model behavior and output quality
- Enables complexity through composition of simple building blocks
- Supports maintainability with modular, testable components
This "divide-and-conquer" strategy is essential for developing robust, context-aware AI agents capable of multi-step reasoning, tool integration, and state management. Mastering prompt chaining is crucial for building production-grade systems that can execute intricate workflows with consistency and reliability.
- Routing: For conditional branching between different chains
- ReAct: For interleaving reasoning and actions within chains
- Orchestration: For coordinating multiple parallel chains
- Tool Use: For integrating external capabilities into chain steps
Prompt chaining transforms complex AI tasks from monolithic operations into manageable, reliable pipelines.
If you're behind a corporate SSL-inspecting proxy, run examples with:
AGENTIC_DISABLE_SSL=1 bash run.sh