Prompt Chaining and Pipeline Design

A single prompt has a ceiling. Beyond it, reliability falls apart — not because the model is incapable, but because you're asking it to do too many things at once. This lesson covers how to decompose complex tasks into chains of focused prompts, which architecture to choose for each problem type, how to manage state between steps, and how to keep errors from compounding into cascades.

Quick Answer

Prompt chaining is the practice of breaking a complex task into a sequence of simpler sub-tasks, each handled by a dedicated prompt, where the output of one step becomes part of the input of the next — so every model call works on a scope where it can be excellent rather than merely adequate.

  • Monolithic prompts fail in predictable ways at scale: instruction interference, attention dilution, format drift, and compounding errors.
  • Four architectures cover most pipelines — sequential, branching, looped, and map-reduce — with a router in front for heterogeneous inputs.
  • Every step shares the same anatomy: input contract, single-responsibility system prompt, structured output, and validation before hand-off.
  • Chain only when needed: start with one well-engineered prompt, measure reliability, then chain out only the failing sub-task.

You've written a good system prompt. You've added chain-of-thought.#CoT You're getting reasonable outputs on simple tasks. Then a real requirement lands: analyse a 40-page document, identify the three most critical risks, cross-reference them against a compliance checklist, draft a remediation summary, and format everything as a structured report. You try it in a single prompt. The model produces something that looks plausible — and is wrong in ways that are easy to miss.

This is not a model capability problem. It is a task decomposition problem. Prompt chaining is the principled answer to it.


1. What is prompt chaining?

Prompt chaining is the practice of breaking a complex task into a sequence of simpler sub-tasks, each handled by a dedicated prompt, where the output of one step becomes part of the input of the next. The model never sees the whole problem at once; it only sees the sub-problem it is currently responsible for.

The concept is explicit in Wu et al. (2022), who define a prompt chain as "a sequence of prompts where the output of one prompt is used as the input to the next."#CHI Their AI Chains framing made these sequences transparent and controllable — an accurate mental model that foreshadowed today's DAG-based agent frameworks like LangGraph.#LangChain

The core principle

A model that does one thing reliably is more valuable than a model that tries to do ten things at once. Chaining is not about the model's ability — it is about constraining each call to a scope where the model can be excellent rather than merely adequate.

The anatomy of a single chain step is always the same regardless of how elaborate the overall pipeline becomes:

Input
The output of the previous step (text, JSON, a list) plus any additional context needed for this step specifically. // Never the full upstream history unless it is truly required.
System Prompt
A tight, single-responsibility instruction. The step's role, output format, what to do if the input is malformed. // No multi-role prompts. One job per step.
Output
A structured, predictable artifact — JSON, a list, a passage — that the next step can consume without ambiguity. // Define the contract before writing the prompt.
Validation
An optional assertion layer that confirms the output matches its schema before it is passed downstream. // Cheap insurance against error propagation.

Dohan et al. (2022) formalise this further under the term language model cascades — probabilistic programs where each step conditions on the outputs of previous steps and each transition can incorporate deterministic logic.#Cascade That framing is useful: a chain is not just a sequence of LLM calls, it is a program in which LLM calls are the operations and string-formatted state is the memory.


2. Why monolithic prompts break at scale

Before reaching for a chain, it is worth understanding precisely why a single prompt fails on complex tasks. The failure is not random — it follows predictable patterns.

Attention diffusion

Transformer attention is distributed across all tokens in the context window. When a prompt contains a task description, a long document, several examples, formatting rules, and output constraints simultaneously, the model's effective attention on any individual component decreases. Instructions near the middle of a long prompt are consistently under-weighted relative to those at the beginning and end — a finding confirmed across multiple benchmarks.#Middle A chain localises each step's attention to only the tokens that matter for that step.

Format contract conflicts

Complex tasks often require the model to simultaneously reason, retrieve, classify, and format. These demands impose conflicting constraints. A model asked to "analyse this contract, identify liability clauses, check each against a rubric, and output a JSON report with a supporting narrative" is being asked to write in three registers — analytical prose, structured data, and evaluative commentary — inside a single generation pass. Reliability degrades because the model cannot fully commit to any single format.

Error compounding

In a monolithic prompt, an incorrect inference in the first half of the response tends to propagate. Because the model generates autoregressively, a flawed claim becomes context for everything that follows. In a chain, errors are local to the step in which they occur and can be caught at the boundary before they contaminate downstream steps.

The ceiling metaphor

Think of each prompt as having a reliability ceiling — a complexity level above which output quality becomes unpredictable. Chaining does not raise the ceiling; it ensures you never hit it by keeping every individual prompt well below it.


3. The four chain architectures

Four architectures cover the majority of real-world chaining needs. They are not mutually exclusive — production pipelines frequently combine all four — but it is useful to understand each in isolation first.

Sequential

Steps execute one after another in a fixed order. Output of step N is input to step N+1. The simplest and most common pattern.

Branching

Output of a step determines which path the pipeline takes next. A classification step gates different downstream specialised chains.

Looped / Iterative

A step's output is fed back in as input until a termination condition is met. Used for generation–critique–revision cycles.

Map-Reduce

A large input is split into chunks processed independently (map) then merged by a final aggregation step (reduce).


Sequential chains

The sequential chain is the canonical pattern: a fixed, ordered pipeline where each step has one job. It is the right default for any task that can be expressed as a series of transformations on text.

A practical example: producing a competitive analysis from a set of raw research notes. The pipeline might be: (1) extract structured facts from each source, (2) normalise entities across sources, (3) identify contradictions, (4) rank findings by relevance, (5) draft the analysis. Each step is small, verifiable, and independently debuggable.

Sequential — Step 1 of 4 · Extraction Step prompt
SYSTEM You are a structured extraction engine. Your only job is to extract factual claims from the provided text. Output a JSON array. Each element must have exactly three fields: "claim" — the factual statement (string) "subject" — the entity the claim is about (string) "source_sentence" — the verbatim sentence the claim was drawn from (string) Rules: - Extract only explicit claims. Do not infer. - If the text contains no extractable claims, return an empty array: [] - Do not add commentary, preamble, or explanation outside the JSON. USER [raw research note text] // Step 2 receives the JSON array. Step 3 receives the merged arrays from all notes. // Each step only sees what it needs.

Branching chains

A branching chain uses a classification or routing step whose output determines which sub-chain runs next. This is the correct architecture when the same input type can require qualitatively different handling depending on its content.

The critical requirement is that the classification step produces a finite, deterministic set of labels that map cleanly to downstream chains. If the label space is fuzzy, the branch logic becomes unreliable. Constrain the classifier to a fixed vocabulary and use structured output to enforce it.

Looped / iterative chains

A looped chain passes the output of a step back to the same step (or to a preceding step) until a stopping condition is met. The canonical use case is the generate–critique–revise loop: one prompt drafts content, a second evaluates it against a rubric, and the draft is revised until it passes or a maximum iteration count is reached.

Madaan et al. (2023) formalise this as Self-Refine — a model iteratively improves its own output using feedback generated by itself.#Refine The key finding is that a model acting as critic and reviser in separate, dedicated prompts outperforms a model asked to produce a final answer in a single pass, even when total token budget is held constant.

Looped — Step 2 of 2 · Critique (termination check) Loop condition
SYSTEM You are a senior technical editor. Review the draft against the rubric below. Respond with a JSON object: "pass": true | false "score": integer 1–10 "issues": array of strings (empty if pass is true) "revised_draft": string (only present if pass is false) Rubric: - All claims must be supported by a cited source. - No sentence may exceed 30 words. - The conclusion must restate the central thesis explicitly. USER [draft from Step 1 or previous iteration] // Orchestrator logic: if response.pass === true → exit loop, emit revised_draft. // If response.pass === false and iterations < MAX_ITER → loop back to Step 1 with issues. // MAX_ITER=3 is a common starting point; tune per task.

Define the termination condition before you write the loop. The three standard options are: a pass/fail gate (a validator step returns pass: true), a fixed iteration cap (run at most N times regardless of quality), or a diminishing-returns threshold (stop when the score delta between iterations falls below ε). In production, always combine a quality gate with a hard cap — an infinite loop is a live incident waiting to happen.

Map-reduce chains

When the input exceeds what a single context window can process reliably — or when independent parallel processing is desirable — the map-reduce pattern applies. The input is split into chunks, each chunk is processed by the same prompt (the map step), and a final prompt merges the results (the reduce step).

The reduce step is the hardest part to get right. A naive "summarise all summaries" reduce prompt produces flattened, generic output. A better approach is to have the reduce step perform a specific aggregation operation — ranking, contradiction detection, theme extraction — rather than free-form merging.

Architecture Best for Key risk Termination
Sequential Fixed transformation pipelines, document processing Error propagation if no validation at boundaries Fixed — last step completes
Branching Query routing, content triage, multi-domain tasks Label ambiguity causing misrouting Fixed — branch terminates normally
Looped Drafting, code generation, quality-gated outputs Infinite loops; diminishing returns after 2–3 passes Conditional — gate or cap
Map-Reduce Long documents, batch summarisation, parallel extraction Reduce step loses nuance; chunk boundary artifacts Fixed — reduce completes

4. The router and dispatcher pattern

The router pattern deserves its own section because it appears at the entry point of virtually every non-trivial multi-chain system and its quality determines the reliability of everything downstream. A router is a prompt (or a lightweight function wrapping a prompt) that takes an incoming input and decides which chain should handle it.

The temptation is to write a router that tries to understand the input deeply. Resist it. A router's only job is classification into a finite set of categories. The categories should be mutually exclusive, and as collectively exhaustive as possible — with an explicit catch-all for inputs that fall outside any defined category — and mapped directly to named chains in your orchestration layer.

Router prompt — structured output enforcement Production pattern
SYSTEM You are a request classifier. Categorise the user's request into exactly one of these types: "data_extraction" — user wants structured data pulled from a document "summarisation" — user wants a shorter version of content "question_answer" — user has a specific factual question "code_generation" — user wants code written or modified "unknown" — the request does not fit any category above Respond with a JSON object and nothing else: { "type": "", "confidence": <0.0–1.0> } Do not explain your choice. Do not add keys. Do not wrap in markdown. USER [incoming user request] // Orchestrator: if confidence < 0.6 → fallback to a clarification step. // if type==="unknown" → route to a human escalation queue or a general-purpose chain. // Never trust a router output without a confidence threshold check.

This decision-then-act pattern foreshadows the ReAct architecture (Yao et al., 2023), covered in the agent design lesson.#ReAct

Two engineering decisions matter more than the prompt text itself. First, structured output: force the router to produce a JSON object with a fixed schema. A router that outputs prose like "this looks like a summarisation request" requires fragile string parsing. One that outputs {"type": "summarisation", "confidence": 0.91} can be processed with a single JSON.parse call. Second, a confidence threshold: any router call with confidence below a configurable threshold should not route — it should trigger a clarification or fallback path. A wrong route is significantly more expensive than a delayed route.

Dispatcher vs. router

A router classifies. A dispatcher acts on that classification. Keep these as separate concerns. The router should be a pure function — input in, label out. The dispatcher is orchestration logic — it owns the mapping from label to chain, the fallback rules, and the confidence thresholds. Mixing the two produces prompts that are hard to test and hard to change.


5. State and context across steps

Every chain step receives some state and produces some state. Managing that state carefully is what separates a production pipeline from a proof of concept that falls apart on edge cases.

What to pass forward

The default instinct is to pass the full output of every previous step into every subsequent step. This instinct is wrong. It leads to context windows that balloon with every step, increases token cost, and — crucially — degrades output quality by forcing the model to parse through irrelevant prior content to find what it actually needs.

The correct principle is minimum necessary context: each step receives only the output of its direct predecessor plus any global context that is genuinely required (e.g., the original user intent, a shared rubric, a fixed set of constraints). If a later step needs information from three steps back, that is a signal the intermediate steps should have preserved it explicitly in their structured output.

The state schema pattern

Define a shared state schema at the start of pipeline design, before writing any individual step prompt. The schema is a JSON object that accumulates data as it passes through the chain. Each step reads from it, makes its contribution, and writes back. Steps do not communicate via prose — they communicate via fields.

Initial State
{ "user_request": "...", "source_document": "..." } // Populated by the entry handler before any LLM call.
After Step 1
{ ...initial, "extracted_facts": [...] } // Step 1 appends its output. Does not modify prior fields.
After Step 2
{ ...prev, "ranked_facts": [...], "contradictions": [...] } // Step 2 adds its fields. Source document may now be dropped.
After Step 3
{ ...prev, "draft": "...", "draft_score": 8 } // Source document and raw extracted_facts are no longer forwarded.
Final State
{ "final_report": "...", "metadata": { "steps": 3, "iterations": 2 } } // Only the fields the consumer needs.

Context compression

For long pipelines processing long documents, even minimum-necessary-context passing can produce states that approach token limits. The solution is a compression step — a dedicated prompt whose only job is to summarise the accumulated state into a compact form that preserves all information required by downstream steps while discarding everything else. Khattab et al. (2023) address this systematically in DSPy, which compiles declarative LM pipelines by automatically optimising prompts and few-shot examples across steps — removing the need for hand-crafted inter-step instructions.#DSPy


6. Failure modes and error propagation

A chain is only as reliable as its weakest boundary. Failure modes in chained systems are different from — and often more insidious than — failures in single-prompt systems. They tend to be silent, delayed, and hard to attribute.

Format contract violations

Step N promises to return JSON. Step N+1 calls JSON.parse on the output. If the model wraps its output in a markdown code fence, adds an apologetic preamble, or returns malformed JSON, the parse fails and the entire chain crashes — not at step N where the error originated, but at step N+1 where it is consumed. Always validate the output schema at every step boundary before passing it downstream. Libraries like Zod (TypeScript) or Pydantic (Python) make this trivial. If validation fails, route to a repair prompt that attempts to re-format the malformed output before re-raising.

Repair step — malformed JSON recovery Defensive pattern
SYSTEM The following text was supposed to be a JSON array but is malformed. Extract and return only the valid JSON array. Remove any markdown fencing, prose commentary, or incomplete entries that would prevent parsing. If valid JSON cannot be recovered, return exactly: {"error": "unrecoverable"} USER [malformed output from previous step] // This step runs only when JSON.parse throws. It adds ~1 LLM call per failure. // Log all repair invocations — a high repair rate signals a failing step prompt.

Hallucination inheritance

A factual error generated at step 2 does not get corrected at step 3 unless step 3 explicitly validates against a ground-truth source. In most pipelines it does not — it gets treated as authoritative input and built upon. A wrong entity name in an extraction step becomes a wrong entity name in a summary, which becomes a wrong entity name in a final report. Insert grounding checks at any step that produces factual claims: have the step cite the specific sentence from the source document that supports each claim. Unsupported claims fail the boundary validation and trigger a retry or a flagged output.

Error amplification in loops

Looped chains are particularly susceptible to amplification. If the critique step in a generate–critique–revise loop is poorly calibrated — too lenient, too harsh, or inconsistent — the revision step acts on flawed feedback and may produce output that is worse than what it replaced. Always evaluate the critic prompt independently before wiring it into a loop. Run it against a suite of known-good and known-bad drafts to verify its accuracy before trusting it as a termination gate.

The logging imperative

Log the full input and output of every chain step in production. A pipeline that fails at step 5 is only debuggable if you can inspect what steps 1 through 4 produced. Logging LLM calls is not optional overhead — it is the primary debugging tool for agentic systems.


7. When not to chain

Chaining is not universally better than a single well-crafted prompt. It has real costs — latency, token expense, engineering complexity, and more failure surfaces — and those costs are not worth paying for tasks that a single prompt handles reliably.

Do not build a chain when:

  • The task fits cleanly in one prompt. Summarise this email. Translate this sentence. Classify this support ticket. These are single-step tasks. Adding a chain adds complexity without adding reliability.
  • Latency is the primary constraint. Every step in a chain is at minimum one additional LLM round-trip. For a latency budget of under two seconds, a two-step chain is often infeasible at typical inference speeds. A single, carefully crafted prompt will always be faster.
  • The output structure is simple and the model is consistent. If you have verified empirically that a single prompt returns the exact schema you need on 99% of inputs, adding a formatting correction step is premature optimisation.
  • You cannot maintain the pipeline. A chain of five steps with no logging, no schema validation, and no documented step contracts is harder to fix than a single monolithic prompt. If your team cannot maintain the added complexity, the chain will degrade silently over time as models update and prompt semantics shift.

Use a chain when

The task has multiple qualitatively different sub-tasks. The output of one step is verifiable independently. Reliability on the monolith is below your quality threshold. Error localisation matters.

Stay monolithic when

The task is a single transformation. Latency is critical. The model is already consistent. The team cannot support the additional complexity of a multi-step system.

The practical decision rule: attempt the task with a single, well-engineered prompt first. Measure its reliability over a representative sample of inputs. If it meets your quality bar, ship it. If it doesn't, identify specifically which sub-task is failing — then chain only that sub-task out into its own step. Build the minimum viable chain, not the maximum imaginable one.

Chaining is a tool for managing complexity at scale, not a default architecture. The best chain is the shortest chain that meets your reliability requirement.


Ready to test yourself?

10 questions on prompt engineering fundamentals. No time limit. Share your score with your network.

Take the Quiz →

Frequently Asked Questions

What is prompt chaining?

Prompt chaining breaks a complex task into a sequence of simpler sub-tasks, each handled by a dedicated prompt, where the output of one step becomes part of the input of the next. The model never sees the whole problem at once — each call is constrained to a scope where it can be excellent rather than merely adequate.

Why do monolithic prompts break on complex tasks?

The failure is predictable rather than random: instructions interfere with each other, attention dilutes across competing objectives, output format drifts, and an early mistake contaminates every downstream requirement. Decomposing the task isolates each responsibility so failures stay local and diagnosable.

What are the four chain architectures?

Sequential chains pass each step's output to the next; branching chains run independent sub-tasks in parallel and merge the results; looped chains iterate generate–critique–revise cycles until a quality bar is met; and map-reduce chains apply the same prompt to many segments before aggregating. A router in front can classify each input and dispatch it to the right pipeline.

How do you stop errors from propagating through a chain?

Define a structured output contract for every step and validate it before the result is passed downstream. A cheap assertion layer between steps catches malformed output early — without it, format breaks cascade and hallucinated details get restated as fact by later steps.

When should you not use prompt chaining?

When the task fits cleanly in one prompt, when latency is the primary constraint (every step adds a full LLM round-trip), when a single prompt already returns the correct schema on nearly all inputs, or when the team cannot maintain the added complexity. The practical rule: build the minimum viable chain, not the maximum imaginable one.


References

  1. #CHI Wu, T., Terry, M., & Cai, C. J. (2022). AI Chains: Transparent and Controllable Human-AI Interaction by Chaining Large Language Model Prompts. CHI 2022. arXiv:2110.01691
  2. #Cascade Dohan, D., Xu, W., Lewkowycz, A., Austin, J., Bieber, D., Lopes, R. G., Wu, Y., Michalewski, H., Saurous, R. A., Sohl-Dickstein, J., Murphy, K., & Sutton, C. (2022). Language Model Cascades. arXiv preprint arXiv:2207.10342. arxiv.org
  3. #Middle Liu, N. F., Lin, K., Hewitt, J., Paranjape, A., Bevilacqua, M., Petroni, F., & Liang, P. (2023). Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics, 12, 157–173. arxiv.org
  4. #Refine Madaan, A., Tandon, N., Gupta, P., Hallinan, S., Gao, L., Wiegreffe, S., Alon, U., Dziri, N., Prabhumoye, S., Yang, Y., Gupta, S., Majumder, B. P., Hermann, K., Welleck, S., Yazdanbakhsh, A., & Clark, P. (2023). Self-Refine: Iterative Refinement with Self-Feedback. NeurIPS 2023. arxiv.org
  5. #DSPy Khattab, O., Singhvi, A., Maheshwari, P., Zhang, Z., Shrivastava, K., Sharma, S., Colby Dodds, Z., Strubell, E., Naous, T., Gupta, R., Lerer, A., Zaharia, M., & Potts, C. (2023). DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines. arXiv preprint arXiv:2310.03714. arxiv.org
  6. #ReAct Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., & Cao, Y. (2023). ReAct: Synergizing Reasoning and Acting in Language Models. ICLR 2023. arxiv.org
  7. #CoT Wei, J., Wang, X., Schuurmans, D., Bosma, M., Ichter, B., Xia, F., Chi, E. H., Le, Q. V., & Zhou, D. (2022). Chain-of-Thought Prompting Elicits Reasoning in Large Language Models. NeurIPS 2022. arxiv.org
  8. #LangChain Chase, H. (2022). LangChain [software]. GitHub. github.com/langchain-ai/langchain