Contents

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:

When a memo is wrong, nobody can tell which of the five jobs failed. The only thing you can test is the whole call.

A 40-page contract goes into one prompt that must extract, rate, write, translate and format at once; the memo that comes out misses clauses, invents clause ids and is sometimes in English.Contract40 pages, EnglishOne prompt does it allextract clauses, rate risk,write the memo, translate it,fill the templateFrench memo?missed clausesinvented clause idssometimes English
Scroll to see all of it.
Everything happens inside one call, so there is no intermediate result to check and no way to tell which part went wrong.

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:

  1. 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.
  2. 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.
  3. Draft. A model writes the memo in English from the medium and high clauses, with their original wording in its input.
  4. Translate. A cheap model translates the memo into French. Gate: a language detector says French.
  5. 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

Input flows through Step 1, Gate 1, Step 2, Gate 2 and a code step to the output. Each gate can send its step back for a retry with the error message, or, after the retry limit, send the run to an early exit. A shared state store holds every intermediate result.retry with error (max k)retry with error (max k)failfailInputStep 1LLM: extractGate 1check JSONStep 2LLM: rate riskGate 2check idsStep 3code: formatEarly exiterror, fallback answer or human reviewOutputShared state {input, clauses, risks, ...}: written after each step, plus one trace span per step123456
Scroll to see all of it.

Hover over a number, or tap it, to read what it is.

Solid lines are the fixed order written in code; the accent arrows leave a gate whose check passed. Dashed lines are retries that send the gate’s error back to the same step, and red lines lead to an early exit once the retry limit is reached.
  1. 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.
  2. 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.
  3. 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.
  4. Code step. Deterministic work such as formatting, a database lookup or running tests, cheaper and easier to test than a model call.
  5. 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.
  6. 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

Avoid it when

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

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.

Further reading