Summary

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

  1. Initial Generation: First LLM call produces an initial output
  2. Gate Evaluation: A model or logic layer evaluates quality/conditions
  3. Conditional Branching: Decision point routes to different continuation paths
  4. Iterative Refinement: Selected branch continues with additional LLM calls
  5. 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 me a prompt chaining workflow. Architecture: implement each chain step as a separate module with a shared context object passed through the pipeline. Each step receives structured input, processes it, and returns typed output. Use a pipeline builder pattern for composable step definitions. Include a gate evaluation step that checks quality conditions before deciding which subsequent path to take. Support conditional branching so different inputs can take different paths through the chain. Return structured output at each step so the gate can evaluate its quality. Error handling: implement exponential backoff retry up to 2 attempts for transient API failures, with error context passed forward. Use a circuit breaker pattern that stops chains repeatedly failing at the same step. Edge cases: handle empty intermediate outputs by branching to a default fallback path. Validate intermediate outputs against expected schemas before gate evaluation. Set maximum timeouts per step (30s, configurable). Best practices: log each step input/output with correlation IDs. Make chain depth configurable via environment variable (2-5 steps). Include a dry-run mode that prints the intended chain without executing LLM calls. Testing: write unit tests for each step in isolation with mock LLM responses. Integration test full chain execution and each branching path. Use OpenAI SDK and TypeScript.