Skip to main content
PromptQuorumBuilt for humans. Structured for AI.
Home/Prompt Engineering
Techniques

Prompt Chaining: Examples, LangChain Code and Patterns

·9 min readBy · Founder of PromptQuorum · Discovery engine for open-weight & open-source AI

Prompt chaining splits a complex task into a sequence of smaller prompts, where each model call takes the previous step's output as its input. Use it when a task breaks into fixed steps that you want to inspect, test and reuse separately.

Key Takeaways

  • Prompt chaining splits one task into a sequence of prompts; each LLM call processes the output of the previous one.
  • It fits tasks that decompose cleanly into fixed subtasks. Anthropic describes the goal as trading latency for higher accuracy by making each call an easier task.
  • Chain explicitly when you need to inspect intermediate outputs or enforce a pipeline structure; these are the two reasons Claude's prompting guide gives for explicit chaining.
  • Chaining is not chain-of-thought: chain-of-thought is reasoning written out inside one prompt, a chain is several separate calls.
  • In LangChain, load a model with `init_chat_model`, call `invoke` once per step and pass each result into the next prompt; use LangGraph when you need branching or loops.
  • Every extra step adds latency, cost and a new place to fail. Add a step only when it makes its call clearly simpler.
  • A 2022 user study with 20 participants found that chaining improved task outcome quality and users' sense of transparency and controllability (Wu et al., CHI 2022).

Prompt chaining turns one overloaded prompt into a short pipeline of focused model calls. This guide gives the definition, examples, a LangChain code sample and the cases where you should not chain.

Prompt Chaining: Examples, LangChain Code and Patterns

Prompt chaining splits one task into connected prompts

📍 In One Sentence

Prompt chaining breaks a big task into smaller prompts and feeds the output of each step into the next, so every call has one checkable job.

💬 In Plain Terms

It works like an assembly line instead of one giant request: each station does one thing and hands its work along, so when something breaks you can see which station caused it.

Prompt chaining decomposes a task into a sequence of steps, where each LLM call processes the output of the previous one. Instead of asking for everything at once, you run steps such as analyze, plan, draft and review, each with one narrow responsibility.

Each step has a defined input, a defined output format and one job. Between steps your code can validate or edit the output, so the chain behaves like a pipeline you can test rather than a chat you hope goes well.

Diagram of a prompt chain: a complex task is split into sequential LLM calls, and the output of each step feeds the next.
Diagram of a prompt chain: a complex task is split into sequential LLM calls, and the output of each step feeds the next.

Chaining makes each model call easier to check

Chaining helps when a task has too many layers for one prompt to handle reliably. Separating understanding, planning, generation and checking gives you control at every stage.

  • Each call is an easier task, which Anthropic describes as trading latency for higher accuracy.
  • You can inspect and validate intermediate outputs, one of the two reasons Claude's prompting guide gives for chaining explicitly.
  • Steps are reusable: "summarize", "extract entities" or "translate" can be shared across workflows.
  • You can assign a different model to each step, for example a small fast model for extraction and a stronger one for writing.

Four common patterns for prompt chains

Most chains reuse a few recurring shapes. Pick the one that matches your task and adapt the steps.

  • Analyze → Plan → Draft → Refine: articles, reports, strategies.
  • Extract → Transform → Summarize: raw documents, logs, support tickets.
  • Classify → Route → Generate: triage inputs, then send each to a specialized prompt.
  • Draft → Review → Refine: iterative self-correction of copy, code or designs.
Common prompt chaining patterns: sequential, branching and map-reduce workflows.
Common prompt chaining patterns: sequential, branching and map-reduce workflows.

A three-step chain can be easier to check than one long prompt

The gain is easiest to see when one long prompt and a short chain tackle the same job. The task: turn raw release notes into a customer-facing changelog.

Single prompt

"Read these release notes and write a friendly changelog for our users."

Prompt chain

Step 1, extract: "Extract all user-visible changes from the raw release notes and list them as bullets grouped by feature area."

Step 2, classify: "Label each bullet as bug fix, improvement or new feature, and add a one-line note on why it matters."

Step 3, write: "Using the labeled list, write a short changelog email with an intro and 3–6 bullets. Focus on benefits, not internal details."

Each prompt is now simpler, testable on its own and reusable in other workflows.

Worked example: extract changes, classify them, then write the changelog in three LLM steps.
Worked example: extract changes, classify them, then write the changelog in three LLM steps.

Prompt chaining examples for common tasks

These examples show where a chain replaces one overloaded prompt. The first two come from Anthropic's description of the pattern; the review chain is the self-correction pattern from Claude's prompting guide.

Use case
Chain steps
Why chain it
Marketing copyWrite copy → translateCheck the copy before translating
Document writingOutline → check → writeGate the outline before drafting
Support triageClassify → route → replyEach category gets its own prompt
Code or text reviewDraft → review → refineSeparate calls for writing and critique
Log analysisExtract → group → summarizeKeeps each call small and checkable

LangChain prompt chaining example in Python

To chain prompts in LangChain, load a chat model with `init_chat_model`, call `invoke` once per step and pass each step's text into the next prompt. The sample runs the changelog chain from above; it assumes `langchain`, your provider's integration package and its API key are set up.

For advanced needs, LangChain points to LangGraph, its low-level orchestration framework for combining deterministic and agentic workflows (for example branching or loops). Agents are created with `create_agent`.

python
from langchain.chat_models import init_chat_model

model = init_chat_model("claude-sonnet-4-6")  # any supported chat model


def run(prompt: str) -> str:
    return model.invoke(prompt).text


notes = "..."  # raw release notes

changes = run(f"List every user-visible change as bullets:\n{notes}")
labeled = run(f"Label each bullet as bug fix, improvement or new feature:\n{changes}")
email = run(f"Write a short user-facing changelog email from this list:\n{labeled}")
print(email)

Use a chain for fixed stages and a single prompt for small tasks

Use prompt chaining when a task splits into fixed stages that can fail or change independently. A very long prompt full of "if" conditions is usually a sign it needs a chain.

  • Use a chain: content pipelines (research → outline → draft → edit), data pipelines (ingest → clean → extract → summarize), decision support (facts → options → trade-offs → recommendation).
  • Use a single prompt: small one-off tasks where you do not need to inspect intermediate results.
  • Consider an agent instead: open-ended problems where the steps cannot be fixed in advance (Anthropic separates fixed workflows from agents).

Prompt chain vs single prompt vs chain-of-thought

The three approaches differ in where the work happens. A chain splits it across calls; chain-of-thought keeps it inside one answer.

Dimension
Single prompt
Prompt chain
Chain-of-thought
Model callsOneOne per stepOne
Where reasoning happensInside one answerAcross steps, in your codeWritten out in one answer
Inspect mid-wayNoYes, after every stepOnly by reading the text
Cost and latencyLowestHigher: one call per stepSlightly higher: longer output
Best forSimple tasksTasks with fixed stagesLogic-heavy single questions

Frameworks automate chains but the idea stays the same

You can chain by hand with plain API calls, or use a framework for orchestration. The concept is identical; frameworks add tracing, retries and structure.

  • LangChain: `init_chat_model` for models, `create_agent` for agents, LangGraph for low-level orchestration.
  • DSPy (Khattab et al.): compiles declarative language-model calls into pipelines that improve themselves.
  • Anthropic, Building effective agents: prompt chaining and other workflow patterns, with advice on when to use each.
  • Claude prompting guide, "Chain complex prompts": explicit chaining "is still useful when you need to inspect intermediate outputs or enforce a specific pipeline structure".

How to build a prompt chain in five steps

  1. 1
    Split the task into stages with one job each. For a blog post: outline, draft sections, fact-check, format.
  2. 2
    Define the output format of every step. If step 2 expects JSON, tell step 1 to produce exactly that JSON.
  3. 3
    Test each step alone before linking them. Run step 1 on ten real inputs and fix it before you build step 2.
  4. 4
    Add checks between steps. A programmatic gate or a human review after costly steps stops errors from cascading.
  5. 5
    Log intermediate outputs and iterate. When the result is wrong, the logs show which step to fix.

Four prompt chaining mistakes to avoid

❌ Too many steps

Why it hurts: Every step adds latency and another chance for the model to err.

Fix: Start with the fewest steps that make each call simple; merge steps that do not change the result.

❌ Vague handoff format

Why it hurts: Step 2 cannot rely on "a list of ideas" from step 1.

Fix: Specify the exact format, such as JSON keys, and show an example output.

❌ No checks between steps

Why it hurts: A bad outline becomes a bad draft and a worse final text.

Fix: Validate or review the output of the steps where an error would be costly.

❌ Testing only the full chain

Why it hurts: When the final output is wrong you do not know which step failed.

Fix: Test each step in isolation first, then test the chain end to end.

Errors compound across steps

Every step is another point where the model can be wrong. As illustrative arithmetic, not a measurement: if five steps are each right 95% of the time and errors are independent, the whole chain is right about 77% of the time (0.95 to the power of 5).

That is why short chains, per-step tests and checks between steps matter more than adding stages.

Compare models on a single chain step in PromptQuorum

PromptQuorum sends one prompt to several models and shows the answers side by side. For a chain, run one step across models to see which handles it best before you commit that step to a model.

Frequently Asked Questions

What is prompt chaining?

Prompt chaining is a technique that decomposes a task into a sequence of steps, where each LLM call processes the output of the previous one. Each step has one narrow job, and you can check or edit the result between steps.

What does prompt chaining consist of?

It consists of splitting a large task into smaller, connected prompts. The first prompt produces an output in an agreed format, that output becomes part of the second prompt, and so on until the final step produces the result you need.

What is the difference between prompt chaining and chain-of-thought prompting?

Chain-of-thought asks the model to write out its reasoning inside one prompt and one answer. Prompt chaining uses several separate calls, with your code passing results between them. You can combine both: use chain-of-thought inside a step of a chain.

How do you do prompt chaining in LangChain?

Load a model with `init_chat_model`, call `invoke` for the first prompt, read the text of the response and insert it into the second prompt, and repeat. For advanced needs, LangChain points to LangGraph, its low-level orchestration framework for combining deterministic and agentic workflows (for example branching or loops).

When should you not use prompt chaining?

Skip it for small one-off tasks where one prompt works, and when you do not need to inspect intermediate results. For open-ended problems where the steps cannot be fixed in advance, Anthropic suggests an agent rather than a fixed workflow.

How many steps should a prompt chain have?

There is no fixed number. Use the fewest steps that make each call simple, because every step adds latency, cost and another chance for an error. Merge any step whose removal does not change the output.

Is prompt chaining the same as fine-tuning?

No. Chaining changes how you call a model, using several prompts in sequence. Fine-tuning changes the model itself by training it on examples. They can be combined, and the comparison of prompt engineering and fine-tuning explains when each fits.

What is a simple example of prompt chaining?

Anthropic's example: generate marketing copy in one call, then translate it into another language in a second call. Between the two calls you can check the copy before it is translated.

Sources and Further Reading

Apply these techniques with a local LLM or your own API keys — PromptQuorum works with any backend.

Try PromptQuorum free →

← Back to Prompt Engineering

MCP