Before you start
You are about to run four prompts in a row and watch a short quarterly report become a sorted markdown table, with each step printed as it finishes. Every prompt gets the reply to the one before it as its input. That hand-off is the whole technique, and its name is prompt chaining.
You need Python 3, the anthropic package and an API key, set up as in the Claude API quickstart. The step prompts are plain instructions. If writing a clear one is still new to you, the prompt engineering basics lesson comes first.
Run a four-step chain from Anthropic's cookbook
The code is Anthropic's own, from the cookbook notebook on basic multi-LLM workflows. chain takes a starting text and a list of prompts. It sends the first prompt with that text, then sends each later prompt with the reply it just got. The notebook calls its samples demonstrations of core concepts, not production code.
from util import llm_call # util.py sits beside the notebook in the cookbook
def chain(input: str, prompts: list[str]) -> str:
"""Chain multiple LLM calls sequentially, passing results between steps."""
result = input
for i, prompt in enumerate(prompts, 1):
print(f"\nStep {i}:")
result = llm_call(f"{prompt}\nInput: {result}")
print(result)
return result
data_processing_steps = [
"""Extract only the numerical values and their associated metrics from the text.
Format each as 'value: metric' on a new line.
Example format:
92: customer satisfaction
45%: revenue growth""",
"""Convert all numerical values to percentages where possible.
If not a percentage or points, convert to decimal (e.g., 92 points -> 92%).
Keep one number per line.
Example format:
92%: customer satisfaction
45%: revenue growth""",
"""Sort all lines in descending order by numerical value.
Keep the format 'value: metric' on each line.
Example:
92%: customer satisfaction
87%: employee satisfaction""",
"""Format the sorted data as a markdown table with columns:
| Metric | Value |
|:--|--:|
| Customer Satisfaction | 92% |""",
]
# Three of the cookbook's eight report lines
report = """
Q3 Performance Summary:
Our customer satisfaction score rose to 92 points this quarter.
Revenue grew by 45% compared to last year.
Employee satisfaction is at 87 points.
"""
formatted_result = chain(report, data_processing_steps)- Copy util.py from the same cookbook folder into your project. Its
llm_callsends one user message and returns the reply's text, onclaude-sonnet-5-5unless you pass another model. - Set
ANTHROPIC_API_KEYin your shell, becauseutil.pyreads the key from the environment. - Save the code beside
util.py. Run it. - Read the output under "Step 1" first. Every later step works only from that text, so a value missing there never comes back.
- Compare it with "Step 4", the markdown table the last prompt asks for.
What each call sees
The line inside the loop is the one to study: llm_call(f"{prompt}\nInput: {result}"). Each call gets one instruction and one block of text. Nothing else. Step 3 never sees the report, only the list that step 2 returned.
That narrow view cuts both ways. A step cannot be thrown by text it never receives, but it cannot recover a fact an earlier step dropped. When step 4 needs something from the original input, pass it along on purpose, in its own XML tags so the model can tell it apart from the instruction. Each step prompt is also a small template, a fixed instruction with one slot, and the next lesson builds templates with several named slots.
Anthropic's interactive tutorial chains prompts the other way in its appendix on chaining. It keeps one conversation going: the first reply goes back in as an assistant turn, followed by the next request, so every call sees all the turns before it.
| Aspect | Pass only the output | Pass the whole conversation |
|---|---|---|
| Where Anthropic shows it | The cookbook's chain function | The tutorial's chaining appendix |
| What one step sees | Its instruction and the last reply | Every earlier request and reply |
| Input size per call | About one step's worth | Grows with every step |
| Fits | Turning data into a new shape, step by step | Asking the model to revise its own last answer |
Add a gate between steps
Step 1 asks for lines shaped value: metric. If the model adds a heading or bullet points, step 2 still runs, and the damage shows up three calls later. By then it is hard to trace. Anthropic's guide Building effective agents names the fix: programmatic checks on any intermediate step, drawn as a "gate", to make sure the process is still on track.
The version below wraps the cookbook's chain in this lesson's own code. It drops the prints and runs a check after each step you name. A failed check raises an error, so a broken step stops the run instead of feeding the next prompt.
A gate can also be a prompt. The tutorial follows a request for ten words ending in "ab" with a review: "Please find replacements for all 'words' that are not real words." Its notebook notes that Claude still occasionally changed words that were already correct. The fix was to give the reviewer an out by adding "If all the words are real words, return the original list." A review step without that sentence has no way to report that the work is fine.
# Paste this below the first sample: it reuses report and data_processing_steps.
import re
from util import llm_call
LINE = re.compile(r"^\S+: .+") # the 'value: metric' shape steps 1 to 3 ask for
def check_lines(text: str) -> None:
"""A gate: stop the chain when a step breaks the line format."""
lines = [line.strip() for line in text.strip().splitlines() if line.strip()]
bad = [line for line in lines if not LINE.match(line)]
if not lines or bad:
raise ValueError(f"Gate failed. First bad lines: {bad[:3]}")
def chain_with_gates(input: str, prompts: list[str], gates: dict) -> str:
"""The cookbook's chain, with a check after the steps named in gates."""
result = input
for i, prompt in enumerate(prompts, 1):
result = llm_call(f"{prompt}\nInput: {result}")
check = gates.get(i)
if check:
check(result) # raises, so a bad step never reaches the next prompt
return result
table = chain_with_gates(
report, data_processing_steps, {1: check_lines, 2: check_lines, 3: check_lines}
)When a chain beats one long prompt
Anthropic's rule is short: chain when the task can be cleanly split into fixed subtasks. You give up latency to gain accuracy, because each call has an easier job. Its two examples are marketing copy that is then translated, and an outline that is checked against criteria before the document is written from it. A 2024 Anthropic post adds a tax question answered in three calls: list the related tax codes, find the relevant sections in each document, then answer from what was gathered.
In every one of those, the order is known before the first call. That is the test. When the right steps depend on the input, a fixed chain is the wrong shape. A support queue mixing refund requests and technical problems needs a router that picks the next prompt, or an agent that picks its own steps, as the AI agent lesson shows. One clear prompt that already works needs no chain at all. Keep it.
Research points the same way, on models that are now old. In 2022 the least-to-most prompting paper had GPT-3's code-davinci-002 first break a problem into simpler subproblems, then solve them in order, each answer feeding the next. On SCAN, a test of handling commands harder than the examples, it reported at least 99 percent accuracy against 16 percent for chain-of-thought prompting.
Two later studies kept the pattern. Self-Refine in 2023 had GPT-3.5, ChatGPT and GPT-4 draft an output, give feedback on it and refine it. Across seven tasks it reported an average gain of about 20 percent absolute over one-step generation. A 2024 summarization study ran draft, critique and refine as three prompts and as one. The three-prompt chain came out ahead, and the authors suggest the single prompt might only simulate the refinement.
Source: Least-to-Most Prompting (Zhou et al., 2022) on Hugging Face Papers (read November 6, 2026)
Anthropic's workflows-of-agents talk and the AI Chains study
In October 2025 Anthropic posted a conversation between Alex Albert of Claude Relations and Erik, who works on multi-agent research and co-wrote Building effective agents. One chapter, at 6:40, is titled "The evolution of workflows and agents (workflows of agents)". That title describes where a chain like this one can grow. Keep the order of steps in your code, and let a single step become an agent with tools when it has to search or call an API.
Chaining predates the agent wave. In the 2021 paper AI Chains, Tongshuang Wu, Michael Terry and Carrie J. Cai ran a 20-person user study of an interactive tool for building chains. Chaining improved the quality of task outcomes, and users started to debug unexpected model outputs by "unit-testing" sub-components of a Chain. You can do the same today: save step 2's output to a file and rerun step 3 alone until it behaves.
the output of one step becomes the input for the next, thus aggregating the gains per step

Common mistakes
- Chaining a task that one clear prompt handles, and paying for four calls and four waits.
- Letting step 1 drop a fact that a later step needs, when each call sees only the text it is handed.
- Running every step even after one returns the wrong shape, so the error surfaces three calls later.
- Adding a review step with no way to say the work is already right.
- Debugging from the final output instead of from the first step whose output looks wrong.
- Fixing the order in code for a task whose steps change from one input to the next.
Try it yourself
- Delete the employee satisfaction line from the report and run the chain again. Which step shows the change first?
- Run
chain_with_gatesand break step 1 on purpose by adding "Start with a short heading." to its prompt. Which gate stops the run, and what does its error list? - Add a fifth step that writes one sentence about the largest value, and hand it the original report in an XML tag named
report, along with the table. - Write the outline example from Anthropic's guide as three prompts with one gate. What exactly does your gate check?
Check yourself
In the cookbook's chain function, what does step 3 receive?
Show answer
B. Its own prompt and step 2's output Each call is built from one prompt plus the result of the call before it.
What is a gate in a prompt chain?
Show answer
A. A check between steps that can stop the run Anthropic's guide describes gates as programmatic checks on intermediate steps that keep the process on track.
Which task fits a fixed prompt chain best?
Show answer
C. Drafting an outline, checking it, then writing the document Its subtasks are known in advance and run in the same order every time, which is the case Anthropic names for chaining.
A review step keeps changing words that were already right. What does Anthropic's tutorial add to the review prompt?
Show answer
D. A line telling it to return the original list when nothing is wrong Giving the reviewer an out lets it leave correct work alone instead of inventing a fix.
Frequently asked questions
What is prompt chaining?
Prompt chaining is splitting one task into several prompts that run in a fixed order, each one receiving the previous output. Anthropic lists it first among the workflows in its agent guide, with optional checks between the steps.
What is the difference between prompt chaining and chain of thought?
Chain of thought asks for reasoning steps inside one reply, while chaining makes each step its own model call. Separate calls let you check one step, or rerun it alone, which a single reply does not allow.
Is a prompt chain an AI agent?
No. In Anthropic's terms a chain is a workflow, where models and tools follow predefined code paths. An agent directs its own process and tool use. A chain can still hand one of its steps to an agent.
How many steps should a prompt chain have?
As few as the task needs. Every step adds a call and a wait, and Anthropic advises the simplest solution possible, adding complexity only when needed. Split where a check between two steps would catch a real mistake.
Can the steps of a chain run in parallel?
Only the independent ones. A dependent step has to wait for the output before it. The same cookbook notebook has a parallel function that sends one prompt over several inputs at once, which Anthropic's guide treats as its own workflow, parallelization.
Sources
- Anthropic Engineering: Building effective agents https://www.anthropic.com/engineering/building-effective-agents
- Anthropic cookbook: Basic multi-LLM workflows (notebook) https://github.com/anthropics/claude-cookbooks/blob/main/patterns/agents/basic_workflows.ipynb
- Anthropic cookbook: util.py for the agent patterns https://github.com/anthropics/claude-cookbooks/blob/main/patterns/agents/util.py
- Anthropic's prompt engineering tutorial: Appendix 10.1, Chaining Prompts https://github.com/anthropics/prompt-eng-interactive-tutorial/blob/master/Anthropic%201P/10.1_Appendix_Chaining%20Prompts.ipynb
- Anthropic: Prompt engineering for business performance (29 February 2024) https://www.anthropic.com/news/prompt-engineering-for-business-performance
- Least-to-Most Prompting (Zhou et al., 2022) on Hugging Face Papers https://huggingface.co/papers/2205.10625
- Self-Refine (Madaan et al., 2023) on Hugging Face Papers https://huggingface.co/papers/2303.17651
- Prompt Chaining or Stepwise Prompt? (Sun et al., 2024) on Hugging Face Papers https://huggingface.co/papers/2406.00507
- AI Chains (Wu, Terry and Cai, 2021) on Hugging Face Papers https://huggingface.co/papers/2110.01691
- YouTube: Building more effective AI agents (Anthropic, 17 October 2025) https://www.youtube.com/watch?v=uhJJgc-0iTQ
