Chain multiple AI steps into one reliable pipeline. Use when your AI task is too complex for one prompt, you need to break AI logic into stages, combine classification then generation, do multi-step reasoning, build a compound AI system, orchestrate multiple models, or wire AI components together. Also used for LangChain LCEL alternative, how to chain LLM calls together, one prompt is not enough, multi-step AI workflow, AI pipeline that actually works in production, prompt chaining keeps breaking, DAG of LLM calls, extract then classify then generate, compound AI system design, how to combine multiple AI steps without spaghetti code.
Instrucciones de origen · Vista previa de solo lectura
name
ai-building-pipelines
description
Chain multiple AI steps into one reliable pipeline. Use when your AI task is too complex for one prompt, you need to break AI logic into stages, combine classification then generation, do multi-step reasoning, build a compound AI system, orchestrate multiple models, or wire AI components together. Also used for LangChain LCEL alternative, how to chain LLM calls together, one prompt is not enough, multi-step AI workflow, AI pipeline that actually works in production, prompt chaining keeps breaking, DAG of LLM calls, extract then classify then generate, compound AI system design, how to combine multiple AI steps without spaghetti code.
Build a Multi-Step AI Pipeline
Guide the user through breaking a complex AI task into multiple steps that feed into each other. One prompt can't do everything — compound AI systems dramatically outperform single calls by decomposing problems.
Step 1: Understand the pipeline
Ask the user:
What's the end-to-end task? (e.g., "read a support ticket, classify it, draft a response")
What are the natural stages? (classification, retrieval, generation, verification?)
Does any step need special tools? (search, database, calculator?)
Does data flow linearly, or do steps branch/loop?
Step 2: Design the stages
The core pattern — compose DSPy modules
Every stage is a DSPy module. Wire them together in forward():
Not every stage needs the same model. Use cheap models for simple steps:
expensive_lm = dspy.LM("openai/gpt-4o") # or "anthropic/claude-sonnet-4-5-20250929", etc.
cheap_lm = dspy.LM("openai/gpt-4o-mini") # or "anthropic/claude-haiku-4-5-20251001", etc.
pipeline = SupportPipeline()
# Cheap model for classification (simple task)
pipeline.classify.lm = cheap_lm
# Expensive model for drafting (needs quality)
pipeline.draft.lm = expensive_lm
See /ai-cutting-costs for more cost optimization strategies.
Step 5: Test and optimize the full pipeline
The beauty of DSPy pipelines: you optimize the whole thing end-to-end, not each step separately.
defpipeline_metric(example, prediction, trace=None):
# Score the final output qualityreturn prediction.response.lower().strip() == example.response.lower().strip()
# Optimizes prompts for ALL stages together
optimizer = dspy.MIPROv2(metric=pipeline_metric, auto="medium")
optimized = optimizer.compile(pipeline, trainset=trainset)
Key patterns
Decompose the problem — if a task has distinct phases (understand, retrieve, generate, verify), make each one a module
Each stage gets its own signature — clear inputs and outputs make the pipeline debuggable
Wire in forward() — the forward method is your orchestration logic
Optimize end-to-end — DSPy optimizers tune all stages together to maximize the final metric
Debug stage by stage — use dspy.inspect_history() to see what each step did
Assign models per stage — cheap models for simple tasks, expensive for complex ones
When to use LangGraph instead
DSPy pipelines are great for stateless, linear-ish flows. But some problems need more:
This gives you LangGraph's state management and routing with DSPy's optimizable prompts. For more, see /ai-building-chatbots (stateful conversations) and /ai-coordinating-agents (multi-agent systems).
Gotchas
Optimize the full pipeline, not individual modules — optimizing modules in isolation then composing them gives worse results than optimizing the whole pipeline end-to-end with dspy.BootstrapFewShot or dspy.MIPROv2. A single MIPROv2(auto="medium") call on the full pipeline typically improves accuracy 15-25% over unoptimized baselines.
Error propagation is silent — if an early module returns garbage, later modules process it without complaint. Use dspy.Refine around key stages to catch bad intermediate outputs with a reward function.
Do not overuse ChainOfThought — not every module in a pipeline needs reasoning. Use dspy.Predict for simple steps (extraction, formatting) and reserve ChainOfThought for steps that actually benefit from reasoning. Unnecessary reasoning adds latency and cost.
Pipeline order affects optimization — DSPy optimizers trace through your forward() method. If module A's output feeds module B, the optimizer sees this dependency. Reordering modules or adding conditional logic changes what the optimizer can learn.
Test intermediate outputs, not just final output — add metrics that check each stage's output independently. A pipeline can produce correct final output for wrong reasons, which breaks when inputs change.
Additional resources
For worked examples (minimal pipeline, routing, Refine, production content moderation), see examples.md
For DSPy API quick-reference (Module, Predict, ChainOfThought, Refine, BestOfN, MIPROv2, save/load), see reference.md
Cross-references
Install any skill: npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill <name>
Verification between stages — see /ai-checking-outputs
Assign different models per stage — see /ai-cutting-costs
Identify where to split your task — see /ai-decomposing-tasks
Content generation pipelines — see /ai-writing-content
Complex reasoning patterns — see /ai-reasoning
Measure and improve pipeline accuracy — see /ai-improving-accuracy
Composing DSPy modules — see /dspy-modules
Iterative refinement with feedback — see /dspy-refine
Install /ai-do if you do not have it — it routes any AI problem to the right skill and is the fastest way to work: npx skills add lebsral/DSPy-Programming-not-prompting-LMs-skills --skill ai-do