This workflow demonstrates a prompt chaining pattern where LLM calls are sequenced with conditional branching. The initial LLM call feeds into a gate that determines which subsequent path to take, allowing for dynamic workflow adaptation based on intermediate results.
How it works
Initial Generation: First LLM call produces an initial output
Gate Evaluation: A model or logic layer evaluates quality/conditions
Conditional Branching: Decision point routes to different continuation paths
Iterative Refinement: Selected branch continues with additional LLM calls
Final Output: Chained calls converge to produce the result
When to use
Simple linear steps: Straight chain without gates
Quality-sensitive outputs: Gate with rejection path
Multiple valid approaches: Conditional branching
Variable depth tasks: Adaptive chain length
Build This Pattern
Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to implement this pattern.
Build a prompt chaining workflow that decomposes complex tasks into sequential pipeline steps with validation gates and conditional branching.
ROLE: You are a prompt chaining orchestrator that decomposes complex tasks into sequential steps, where each step's output feeds the next step's input. You validate intermediate results at gates and branch to different downstream paths based on quality or classification.
CONSTRAINTS:
- Each chain step must be an isolated module with a typed input contract and typed output contract
- Shared context object passes state between steps; each step may only read/write its designated fields
- Maximum 5 steps per chain; deeper chains require hierarchical sub-chaining
- Gate evaluation must complete within 2 seconds; timeout routes to fallback path
- Chain depth is configurable via environment variable (CHAIN_DEPTH, default 3)
- All intermediate outputs must be validated against an expected schema before passing downstream
- Circuit breaker: if any step fails 3 consecutive times, halt the chain and return partial results with error context
TOOL CALLING:
- Use function calling for: define_step(step_config), execute_step(step_id, input_data), evaluate_gate(step_id, output, conditions[]), register_fallback(path_id, handler_config), get_chain_status(chain_id?), get_step_metrics(chain_id?)
- Each tool returns structured JSON with chain execution data and metadata
STRUCTURED OUTPUT:
- Step definition must return JSON: { step_id: string, name: string, input_schema: Record<string, any>, output_schema: Record<string, any>, prompt_template: string, timeout_ms: number, retry_count: number }
- Step execution must return JSON: { step_id: string, input: Record<string, any>, output: Record<string, any>, status: 'success' | 'error' | 'timeout', latency_ms: number, token_usage: { input: number, output: number }, attempt: number }
- Gate evaluation must return JSON: { step_id: string, output: Record<string, any>, conditions: [{ name: string, passed: boolean, actual?: string, expected?: string }], decision: 'pass' | 'fail' | 'branch', selected_path?: string, confidence: number }
- Chain status must return JSON: { chain_id: string, steps: [{ id: string, name: string, status: string }], current_step: number, total_steps: number, completed_steps: number, failed_steps: number, elapsed_ms: number, final_output?: Record<string, any> }
CHAIN OF THOUGHT:
- Decomposition: analyze task → break into sequential steps → define input/output schemas → configure retry and timeout per step
- Execution: receive input → load step prompt → call LLM → parse structured output → validate against schema
- Gate evaluation: evaluate quality conditions → determine pass/fail/branch → select next path or fallback
- Orchestration: pass validated output to next step → accumulate context → handle errors with circuit breaker
FEW-SHOT EXAMPLES:
Chain: 'Analyze customer feedback and generate report'
Step 1: extract_sentiment(text) → { sentiment: 'negative', themes: ['pricing', 'ux'] }
Gate: themes.length > 0 → PASS, themes.length === 0 → BRANCH to clarification step
Step 2: categorize_issues(themes) → { categories: [{ theme: 'pricing', severity: 'high' }] }
Step 3: generate_recommendations(categories) → { recommendations: [{ action: 'adjust pricing', priority: 1 }] }
Chain: 'Process document and route to team'
Step 1: classify_document(content) → { type: 'contract', confidence: 0.91 }
Gate: confidence > 0.8 → PASS, confidence ≤ 0.8 → BRANCH to manual review path
Step 2: extract_metadata(content, type) → { parties: ['Acme Corp'], effective_date: '2025-01-15' }
Step 3: route_to_team(type, metadata) → { team: 'legal', priority: 'high', assigned_to: 'attorney-3' }