Prompt chaining: a complete guide
When you cram a multi-step task into one giant prompt, the model rushes the middle and the errors compound silently. Prompt chaining flips that: you break the job into a sequence of focused prompts, and each prompt's output becomes the next one's input. In the AI Chains study (Wu, Terry, and Cai, CHI 2022), this single move improved task quality and made the whole process far more transparent and controllable than one opaque call.
See it work
Say you want a polished executive summary of a messy support-ticket dump. The one-shot version asks for everything at once.
Prompt (single call):
Read these 40 support tickets and write a one-paragraph executive
summary for leadership, highlighting the top 3 issues, their impact,
and a recommended fix for each.
Output:
"Customers reported various issues this week including login problems
and slow pages. The team should look into performance. Overall
sentiment was mixed and we recommend monitoring the situation."
Vague, generic, and it quietly skipped "impact" and "a fix for each." Now chain it: extract, then rank, then write.
Link 1 — Extract: "List every distinct issue in these tickets as
{issue, ticket_count, example_quote}."
Link 2 — Rank: "Given this issue list, pick the top 3 by ticket_count
and estimate business impact for each."
Link 3 — Write: "Given these 3 ranked issues, write a one-paragraph
exec summary: issue, impact, recommended fix."
Output (Link 3):
"This week's top pain points were checkout failures (14 tickets, blocking
revenue), OAuth login errors (9 tickets, locking out enterprise users),
and dashboard latency (7 tickets, hurting daily-active usage). Fixes:
roll back the payment SDK, patch the token-refresh bug, and add caching
to the metrics query."
Same model, same tickets. The chain hits every requirement because each link does one job well and hands clean structure to the next.
The mental model
Think of a kitchen line. You don't ask one cook to chop, sear, plate, and garnish a hundred covers at once — you set up stations, and each cook perfects one task before passing the plate down. Prompt chaining gives the model the same mise en place: small, verifiable steps instead of one heroic effort.
Prompt chaining trades one hard problem the model often fumbles for several easy problems it rarely does.
How it works
A chain is a directed pipeline. Each link takes the prior link's structured output, does a narrow transformation, and emits structured output for the next.
- Decompose. Split the task into sub-tasks that each have one clear job and a checkable output.
- Define the contract. Decide the shape each link emits (JSON, a list, a labeled block) so the next link can parse it.
- Run sequentially. Feed link N's output into link N+1's prompt. Each call is fresh and focused.
- Inspect between links. Intermediate outputs are visible, so you can validate, edit, or branch before errors travel downstream.
- Assemble. The last link produces the final answer, built on clean, already-verified material.
Why it works
The gains come from a few compounding factors, roughly in order of impact.
| Factor | Why it helps |
|---|---|
| Focused attention per step | The model reasons about one sub-task, so it doesn't drop requirements mid-task. |
| Error isolation | A bad output is caught at the link that produced it, not blamed on the whole pipeline. |
| Structured handoffs | Clean intermediate data removes ambiguity the next step would otherwise guess through. |
| Inspectability | You can read, test, and edit each link's output — the core finding of the AI Chains study. |
| Reuse | Stable links (extract, classify) get reused across chains and cached. |
Where it shines
Prompt chaining earns its keep whenever a task has natural stages or needs verification partway through.
- Multi-stage generation — draft, then critique, then refine. Sun et al. (Findings of ACL 2024) found that running drafting, critiquing, and refining as a three-prompt chain produced better summaries than a single "stepwise" prompt that bundled all three phases, which tended to simulate refinement rather than actually do it.
- Structured extraction pipelines — pull entities, then normalize, then aggregate, so each pass stays simple.
- Domain QA over nuanced text — Roegiest and Chitta (2024) used a two-stage chain for contract question answering and found it more effective than simple prompts on subtle legal clauses.
- Retrieval and tool workflows — generate a query, retrieve, then synthesize an answer grounded in what came back.
- Classification then routing — label the input first, then send it down a branch tailored to that label.
When to use it (and when not)
Reach for chaining when:
- The task has clear sub-steps (extract then summarize, plan then write).
- You need to inspect or correct intermediate results.
- A single prompt keeps ignoring some of your requirements.
- Different steps want different settings (low temperature to extract, higher to draft).
Skip it when:
- The task is genuinely one step — chaining just adds latency and cost.
- Sub-steps are tightly coupled and can't be cleanly separated.
- Latency is critical and a single good prompt already passes your bar.
Cost scales with links. A three-link chain means three round-trips: roughly three times the latency and, because you re-send context to each link, often more than three times the tokens of one call. Chain to win quality you can't get otherwise, not by reflex — and keep each link's prompt lean.
Model fit. Chaining works on any capable instruction-following model and actually lets smaller or cheaper models punch above their weight, since each link is an easier ask. Stronger models need fewer links because they hold more steps in one pass.
Escalation. If even well-scoped links stay unreliable, the bottleneck link may need few-shot examples or chain-of-thought inside it; if the whole flow needs dynamic looping and tool calls, graduate to an agent framework.
| Approach | Best when | Trade-off vs chaining |
|---|---|---|
| Single prompt | Task is one step | Cheaper and faster, but fumbles multi-step jobs |
| Chain-of-thought | Reasoning fits one call | One round-trip, but no inspectable intermediates |
| Prompt chaining | Distinct, verifiable stages | More control; more latency and tokens |
| Agents / graphs | Steps are dynamic, looping, tool-driven | More power; much harder to debug and bound |
Structure and components
A well-formed link has four parts:
- Inbound contract — the exact shape it expects from the previous link.
- Single instruction — one job, stated plainly.
- Outbound contract — the structured shape it must emit (a schema, list, or delimited block).
- Guardrail — what to do with empty or malformed input, so failures surface instead of cascading.
A reusable link template keeps the handoffs clean:
You are stage {n} of a pipeline.
INPUT (from previous stage): {structured_input}
TASK: {one specific instruction}
OUTPUT FORMAT: {strict schema, e.g. JSON list of {field: type}}
If INPUT is empty or invalid, return {"error": "<reason>"} and nothing else.
The core algorithm
The mechanism is a fold over a list of prompt builders — each takes the running state and returns the next prompt.
def run_chain(links, initial_input, call_model):
"""links: list of (name, build_prompt) where build_prompt(state) -> str."""
state = initial_input
trace = []
for name, build_prompt in links:
prompt = build_prompt(state)
output = call_model(prompt) # one focused LLM call
trace.append({"link": name, "prompt": prompt, "output": output})
state = parse(output) # validate before handing off
if state is None:
raise ChainError(f"Link '{name}' produced unparseable output")
return state, trace # trace makes every step inspectable
Keep the trace: it's what turns a chain from a black box into something you can unit-test link by link.
Configuration
Tune per link, not globally — different stages want different behavior.
| Parameter | Extraction / classification links | Generation links |
|---|---|---|
temperature | 0–0.2 (deterministic) | 0.5–0.8 (fluent) |
max_tokens | Just enough for the schema | Sized to the final artifact |
| Output format | Strict JSON or labeled fields | Prose or markdown |
| Retries | Re-ask on parse failure | Re-ask on guardrail trip |
Implementation workflow
- Write the task end-to-end as one prompt first; note where it fails.
- Cut it at those seams into links, each with one job.
- Pin an output schema for every link except the last.
- Build and test each link in isolation on real inputs.
- Wire them together; log the full trace.
- Add validation and retries at the brittle links.
- Measure against the single-prompt baseline before shipping.
Do
- Validate and parse each output before passing it on.
- Keep intermediate formats machine-readable.
- Log every link's input and output for debugging.
Don't
- Re-send the entire conversation to every link; pass only what the next step needs.
- Let one link do two jobs because it "seems efficient."
- Trust downstream quality when an upstream link is shaky — fix the source.
Debugging
| Symptom | Likely cause | Fix |
|---|---|---|
| Final output ignores a requirement | The link that owns it is overloaded | Split that link or tighten its instruction |
| Parse errors mid-chain | Loose output contract | Pin a strict schema and add a re-ask retry |
| Garbage out of nowhere | An upstream link failed quietly | Read the trace; validate at the source link |
| Whole chain drifts off-topic | Context bloat across links | Pass only the minimal structured handoff |
| Costs ballooned | Too many links or fat prompts | Merge trivial links; trim re-sent context |
Testing and proving it
Treat each link like a function with unit tests, and prove the chain beats the baseline on held-out inputs.
def compare(dataset, single_prompt, chain_links, call_model, score):
base = chained = 0
for item in dataset:
base += score(call_model(single_prompt(item)), item.gold)
out, _ = run_chain(chain_links, item, call_model)
chained += score(out, item.gold)
n = len(dataset)
return {"single": base / n, "chain": chained / n} # ship only if chain wins
Limitations
- Latency and cost stack up — every link is another round-trip, so a long chain feels slow and bills more.
- Errors still propagate — chaining isolates errors but doesn't erase them; a wrong early output can poison everything downstream if you skip validation.
- More moving parts — schemas, retries, and traces are real engineering you now own and maintain.
- Diminishing returns — past a handful of links, coordination overhead can outweigh the quality you gain.
Advanced patterns
- Branching. Route on a classification link so different input types take different downstream paths.
- Parallel fan-out. When sub-tasks are independent, run them concurrently and merge — cuts latency without losing decomposition.
- Self-critique link. Insert a "find the flaws in this draft" link before a "fix them" link; this draft → critique → refine shape is exactly what the ACL 2024 summarization result rewarded.
- Map-reduce over long input. Chunk a huge document, run a per-chunk link, then a reduce link to combine — the standard way past context limits.
Pair it, don't replace it. Chaining composes cleanly with other techniques: put few-shot examples or chain-of-thought inside a stubborn link, and feed a retrieval link's results into a synthesis link. The chain is the skeleton; the other techniques are muscle on individual bones.
Risk and ethics
Inputs flow link to link — so do attacks. If a user-controlled string reaches an early link, a prompt injection can hijack instructions for every link downstream. Treat each link's input as untrusted: keep system instructions out of the data channel, and validate structured outputs before they become the next prompt.
Chaining also adds an audit benefit worth naming: because every intermediate is logged, a chain is more explainable than one opaque call — you can show which step made which decision. That transparency was a headline finding of the original AI Chains work.
Ecosystem
Most orchestration frameworks are, at heart, prompt chainers. LangChain's LCEL pipes outputs between steps, LlamaIndex chains query and synthesis stages, DSPy compiles multi-stage pipelines, and Anthropic's own guidance recommends chaining complex prompts into focused subtasks. You rarely need a framework to start, though — the fold in the core algorithm above is the whole idea.
Transitions. Coming from a single bloated prompt, chaining is the natural next step once that prompt starts dropping requirements. Going the other way, when your chain needs loops, conditionals, and live tool use that a fixed pipeline can't express, that's your cue to move to an agent or graph runtime.
Future directions
Two threads are active: automatic decomposition, where the model proposes its own chain structure for a task, and learned routing, where a controller picks which links to run per input. Both push chaining from a hand-built pipeline toward something the system assembles on demand — closer to agents, but keeping the inspectable, debuggable spine that makes chaining trustworthy.
Real-world payoff. The AI Chains study (Wu, Terry, and Cai, CHI 2022) ran a 20-person user study and found that chaining LLM prompts not only improved task-output quality but significantly raised users' sense of transparency, controllability, and collaboration — turning an opaque generation into a workflow people could understand and steer.
Summary
- Prompt chaining breaks a complex task into a sequence of focused prompts, passing each output into the next.
- Show it first: where one mega-prompt drops requirements, a short extract → rank → write chain nails them.
- It works by giving the model focused attention per step, isolating errors, and exposing inspectable intermediates.
- Reach for it when a task has distinct, verifiable stages; skip it when the job is truly one step.
- Cost is the catch — each link adds a round-trip and re-sent tokens, so chain for quality you can't otherwise get.
- Validate every handoff, keep traces, and prove the chain beats the single-prompt baseline before shipping.
- Evidence: AI Chains (CHI 2022) for quality and control, Sun et al. (ACL 2024) for chained-over-stepwise refinement, Roegiest and Chitta (2024) for legal QA.
Read Next
Start reading to get personalized recommendations
Explore Unread
Great job! You've read all available articles