Prompt Chaining
Also known as: pipeline, sequential workflow, sequential orchestration
- Who decides
- Code
- Shape
- Chain
Intent
Split one task into a fixed sequence of smaller steps, each an LLM call or plain code, where every step receives the output of the step before it. Your code writes the order and checks the result between steps, so the model never decides what runs next.
Problem
You are building a tool for a company’s legal team, who work in French. Every English supplier contract they receive, about 40 pages long, should come back as a French risk memo in the team’s template.
The first version is one prompt: read the contract, find the risky clauses, rate them, write the memo in French, fill in the template. It works on some contracts. On others it fails in ways nobody sees until a lawyer reads the memo:
- Clauses go missing. The model holds five jobs at once and drops part of one. A risky clause never reaches the memo, and the memo still reads well.
- Clause ids are invented. The memo cites a clause 14.3 that the contract does not have, and there is no intermediate list of clauses to check it against.
- The memo comes out in English. The language rule sits at the end of a long prompt, behind 40 pages of English text, and the model loses it.
When a memo is wrong, nobody can tell which of the five jobs failed. The only thing you can test is the whole call.
Solution
Cut the task where the kind of work changes, give each piece its own short prompt, and run the pieces in an order fixed in code. Between two steps, a check written in code, called a gate, reads the output. When the check fails, the gate retries the step with the error message, stops the run, or hands the case to a person.
For the contract memo, the chain has five steps:
- Extract. A small model lists the contract’s clauses as JSON, each with an id, a title and its text. Gate: the JSON parses and holds at least one clause.
- Rate. A strong model rates each clause low, medium or high against the legal team’s rubric. Gate: every clause id from step 1 is present, no other id appears, and every rating is one of the three values.
- Draft. A model writes the memo in English from the medium and high clauses, with their original wording in its input.
- Translate. A cheap model translates the memo into French. Gate: a language detector says French.
- Format. Plain code puts the text into the legal template. No model call.
Each step is small enough to prompt well and to test on its own, and each runs on the model it needs. Every intermediate result is stored, so when a memo is wrong you open the clause list and the ratings and see which step failed.
The developer fixed the order at design time: the model writes the content of each step and never picks the next one. That is the difference from Plan-and-Execute, where a model writes the list of steps at run time.
Structure
Hover over a number, or tap it, to read what it is.
- Step. One focused job: a prompt, a model with its settings, and a parser that turns the raw text into a typed value. A step can also be a whole tool-using agent.
- Gate. Code, or a cheap model call whose answer code reads, that checks a step’s output against a schema, a count or a business rule. It passes the value on, retries the step with the error, or exits.
- Handoff. What the next step receives: the previous output, plus the original input or an earlier result when that step needs them. Too little loses details; too much makes the prompt long and distracting.
- Code step. Deterministic work such as formatting, a database lookup or running tests, cheaper and easier to test than a model call.
- Early exit. A controlled stop when a gate keeps failing or the input does not fit the chain: an error, a fallback answer or a review by a person.
- Shared state and trace. Every intermediate result stored by name, with one trace entry per step (prompt, output, tokens, time, gate verdict), so a run can be inspected and resumed.
Use it when / Avoid it when
Use it when
- The steps are known in advance. The same sequence fits every input, and the order never depends on what a step finds.
- You need to see intermediate results. Audit, compliance and debugging need the extracted data and the decisions, not only the final text.
- Steps need different models or settings. Extraction and translation can run on a small model while the hard judgement gets the strongest one.
- Some outputs can be checked by code. Schemas, counts and language detection are where the chain’s reliability comes from.
Avoid it when
- One well-written prompt already does the job. Keep a single call, because every extra step adds a round trip and tokens.
- The steps do not depend on each other. Use Parallelization, which runs them at the same time instead of one after another.
- The type of input decides what to do. Put Routing in front of several chains, one per type.
- The steps depend on what is found. Use ReAct, which chooses each next step from the latest result.
How it fails
- Cascading errors
-
An item missed by an early step never comes back, and every later step works on the incomplete data without noticing. The final output still reads well.
Mitigation: put a gate right after the first steps, where an error is cheap to catch and costly to pass on.
- Information loss
-
A later step needs a detail that an earlier one dropped or summarized away, and it fills the gap with something plausible.
Mitigation: pass the original input along to the steps that need ground truth.
- Format drift
-
The model wraps its JSON in prose or leaves out a field, and the next step crashes or carries on with half the data.
Mitigation: use structured output and a schema check, and feed the validation error back on retry.
- Inputs that do not fit
-
An input the steps were not written for goes through the whole chain and comes out as a confident answer about nothing.
Mitigation: add a gate at the start that rejects the input or sends it elsewhere.
Related patterns
- Routing: classifies the input once and picks which path runs; a router often sits in front of several chains.
- Parallelization: runs independent steps at the same time, where a chain runs dependent steps in order; the two combine when one chain runs on many items at once.
- Evaluator–Optimizer: repeats generate and evaluate until a criterion passes; a gate that retries with the error is the smallest step toward it.
- Plan-and-Execute: a model writes the step list at run time and the steps then run, often as a chain; in prompt chaining the developer writes the list.
Try it
List the steps in order, with each step’s job, its output format and the check that runs after it. The generator writes rules for the runner code, then one prompt per model step.
Open the generator on this pattern
Further reading
- Building effective agents (Anthropic, 2024): the definition of prompt chaining and gates, and when a fixed workflow is enough.
- AI Chains: Transparent and Controllable Human-AI Interaction by Chaining LLM Prompts (Wu, Terry and Cai, 2022): an early study of chaining, with people debugging a chain one step at a time.
- Prompt Chaining or Stepwise Prompt? Refinement in Text Summarization (Sun et al., 2024): separate calls compared with one prompt that drafts, critiques and refines.