Last Updated: May 1, 2025
Chain Pattern Taxonomy
| Pattern | Structure | Best For | Failure Mode |
|---|---|---|---|
| Sequential Chain | A → B → C (linear pipeline) | Data extraction → transformation → formatting | Garbage in, garbage out — errors cascade |
| Parallel Branching | A → [B, C, D] → aggregate | Multi-perspective analysis, voting ensembles | Cost multiplies by branch count |
| Conditional Routing | A → if X then B else C | Classification-based workflows | Wrong routing = wrong output |
| Loop Chain | Repeat B until condition met | Self-refinement, iterative improvement | Infinite loops if condition never met |
| Map-Reduce | Map over items → reduce results | Processing lists, batch operations | Large inputs = many parallel calls |
| Graph Workflows | DAG of prompts with dependencies | Complex multi-step reasoning | Hard to debug, race conditions |
| Human-in-Loop | A → human review → B | High-stakes decisions | Bottleneck if human is slow |
| Fallback Chain | Try A → if fails, try B → if fails, try C | Robustness, error handling | Higher latency on failure paths |
Sequential Chains
Step 1: ExtractPrompt: 'Extract all named entities (people, orgs, dates) from: {input}' — structured output only
Step 2: EnrichUse extracted entities to query knowledge base — attach context to each entity
Step 3: SynthesizePrompt: 'Given entities and context: {enriched}, write a summary connecting all parties'
Step 4: ValidatePrompt: 'Check if the summary mentions all {n} entities. Return PASS or FAIL with explanation'
Step 5: FormatIf PASS → format into final output. If FAIL → return to Step 3 with error context
Error PropagationStore intermediate outputs at each step for debugging — when chain fails, you know exactly where
Context WindowEach step adds tokens. Use summary compression between steps for long chains (>5 steps)
Parallel Branching & Voting
| Item | Description |
|---|---|
Multi-Perspective Analysis | Run the same prompt with different system instructions simultaneously: 'Analyze as a security expert', 'Analyze as a UX designer', 'Analyze as a lawyer' |
Ensemble Voting | Run 3+ models on same task → majority vote on output. Reduces hallucination and increases factual accuracy |
Fact-Checking Branch | Main chain produces answer → parallel branch verifies each factual claim against sources → merge corrections |
Cost-Aware Routing | Run cheap model (GPT-4o-mini) first. If confidence is low OR task is flagged as complex, escalate to expensive model (GPT-4o) |
Pareto Aggregation | Collect outputs from parallel branches → use a 'judge' LLM to synthesize a single best response |
Parallel Tool Calls | Execute independent tool calls simultaneously: search_web + query_database + check_calendar → merge results |
Asynchronous Pattern | Fire all parallel chains, wait for ALL to complete (or timeout) before aggregating |
Conditional Routing & Loop Chains
| Item | Description |
|---|---|
Classifier Gate | Step 1: Classify intent (greeting/tech_support/complaint/purchase). Step 2: Route to specialized chain per intent. |
Confidence Threshold | If LLM outputs with low confidence score (<0.7), route to human review or retry with more context |
Language Detection | Detect input language → route to language-specific prompt → translate output back if needed |
Retry-on-Error | Validate output → if invalid, append error to prompt and retry (max 3 retries) — self-correction pattern |
Self-Refinement Loop | Generate → Self-critique → Refine based on critique → Repeat until quality threshold or max iterations |
Tool-Use Loop | LLM selects tool → execute → observe → decide next tool or finish → loop until Task Complete signal |
Convergence Detection | Track output changes between iterations. If delta < threshold (e.g., <5% token change), terminate loop |
Max Iteration Guard | ALWAYS set max_iterations (default 10). Log warning at 80% of max — helps catch near-infinite loops early |
Pro Tip: Every chain needs an exit condition. LLMs can loop forever on ambiguous or adversarial inputs. Set max_iterations, add timeout guards, and validate intermediate outputs at each step.