Prompt chaining means splitting a job into a sequence of prompts, where the output of one prompt becomes part of the input to the next. Instead of asking a model to read fifty reviews, find the problems, rank them and write a report in one go, you ask for each of those in turn. Each prompt has one job, and between steps you (or your code) can look at the result, fix it, or stop.
The block below is a three-step chain that turns customer reviews into a note for a product team. Each tab is one step. The input of step 2 is the reply to step 1, pasted in, and the input of step 3 is the reply to step 2.
[
{"review": 1, "complaint": "Battery died after 4 hours"},
{"review": 1, "complaint": "Charging cable too short"},
{"review": 2, "complaint": "App keeps logging out"},
{"review": 3, "complaint": "Battery does not last a commute"},
{"review": 4, "complaint": "App crashed while pairing"},
{"review": 4, "complaint": "Battery drains fast"},
{"review": 5, "complaint": "Cable too short to use while charging"}
]
Step 1 only reads and extracts. Step 2 never sees the reviews, only the list, so it cannot drift back into summarizing them. Step 3 only writes. If step 3 comes back in the wrong tone, you rerun step 3 alone; the extraction and grouping stay as they were.
Notice what step 3 does not say: that one battery died after 4 hours, or that the app problems were logouts and crashes. Step 3 only received the theme names, so those details were not in its input. Deciding what each step receives is the main design choice in a chain, and a detail you want in the final answer has to be passed all the way down.
Why chain instead of writing one big prompt
Each step has one job and one standard. A single prompt that asks for extraction, analysis and polished writing has three goals to balance, and nothing tells you which one it cut short. A prompt with one job can state exactly what a good result looks like for that job, and you can judge the reply against it.
You can check the middle. In a single prompt, a missed complaint disappears into a nicely written paragraph and you never see it. In a chain, the list from step 1 is right there. Checking it takes seconds, and fixing it before step 2 is cheaper than finding the error in the final report.
Each step can use its own settings. Extraction and grouping want a low temperature and structured output that code can parse. The writing step can be freer. In code you can even send different steps to different models, such as a small fast one for extraction.
Failures are local. When a chain breaks, you know which step broke, and you rerun only that one.
The costs are real too. A chain makes more calls, so it takes longer and costs more than one prompt. And a mistake that slips through an early step is carried into every later one, which is why the steps between are worth checking.
How to design a chain
- Write down the steps you would take by hand. If you would first make a list, then sort it, then write it up, that is three prompts. Steps that need different kinds of thinking are the natural places to split.
- Decide the output format of each step before you write its prompt. Whatever the next step consumes should be easy to read and easy to check: a JSON array, a numbered list, a table. Free prose is fine only for the last step.
- Pass only what the next step needs. Step 2 above receives the complaint list, not the reviews. Less input means less to get distracted by, and it makes each step testable on its own.
- Add a check between steps. In code, parse the JSON and confirm the fields exist. For judgement calls, a check can itself be a prompt: "Does this list contain every complaint in these reviews? Answer yes or no, then list any that are missing."
- Mark pasted material clearly. Each step's input is text produced by an earlier step, and delimiters keep the model from reading it as new instructions.
Here is a check step for the chain above, run on a version of step 1's output that missed a complaint. A check like this is a separate prompt with one narrow question, so it is easier to get right than the extraction itself.
Incomplete
- Review 4: the battery drains fast
If the check says "Incomplete", rerun step 1 or add the missing item by hand before step 2 runs.
A chain in code
In code, a chain is a sequence of calls in which each prompt is built from the previous answer. This version uses the Anthropic Python SDK; the same shape works with any provider.
import json
import anthropic
client = anthropic.Anthropic()
MODEL = "your-model-id" # e.g. from your provider's model list
def ask(prompt):
response = client.messages.create(
model=MODEL,
max_tokens=1024,
messages=[{"role": "user", "content": prompt}],
)
return response.content[0].text
reviews = open("reviews.txt", encoding="utf-8").read()
complaints = ask(
"List every complaint in the reviews inside the <reviews> tags. Ignore praise. "
'Return a JSON array of objects with the keys "review" and "complaint". '
"Return only the JSON, with no code fence.\n\n"
f"<reviews>\n{reviews}\n</reviews>"
)
json.loads(complaints) # raises an error here if step 1 did not return valid JSON
themes = ask(
"Group these complaints into themes. Return only a JSON array with the keys "
'"theme", "count" and "reviews", sorted by count, highest first.\n\n'
f"<complaints>\n{complaints}\n</complaints>"
)
note = ask(
"Write a note of at most four sentences to the product team based on these "
f"themes. Lead with the most common one.\n\n<themes>\n{themes}\n</themes>"
)
print(note)
The json.loads line is the simplest possible check: if step 1 answered with prose, the chain stops there instead of passing bad input along. A production chain would also check the fields, retry a failed step once, and log each intermediate result so a bad final answer can be traced back to the step that caused it.
Chaining in a chat app
You do not need code to chain prompts. Run step 1, read the answer, correct anything that is wrong, and paste it into the step 2 prompt. Doing each step in a new chat keeps the earlier conversation out of the context, so an instruction written for one step does not affect the next. Staying in one chat is quicker and works when the steps are closely related, but the whole history travels along with every new message.
Chaining compared with related techniques
Chain of thought is reasoning written out inside one reply; a prompt chain is several replies with you in control of what passes between them. The two combine well: any step of a chain can ask for step by step reasoning.
In a chain, the steps are fixed in advance by you. When the model itself decides which step comes next, such as searching, reading a result and then choosing another action, the pattern is an agent loop instead, described in ReAct prompting. Fixed chains are easier to test and predict, so they are the better starting point whenever you already know the steps.
Frequently Asked Questions
What is prompt chaining?
Prompt chaining is a technique where you break a task into several prompts that run one after another, and the output of each prompt is inserted into the next. For example, one prompt extracts complaints from reviews, a second groups them into themes, and a third writes a report from the themes.
What is the difference between prompt chaining and chain of thought?
Chain of thought happens inside one response: the model writes out its reasoning before the answer. Prompt chaining uses several separate calls, and you or your code pass the result of one to the next. You can inspect, fix or rerun any single step of a chain, which you cannot do with reasoning inside one reply.
Can I do prompt chaining in ChatGPT or Claude without code?
Yes. Run the first prompt, check the answer, then paste it into the next prompt, either in the same chat or in a new one. Starting a new chat for each step keeps earlier drafts and instructions from influencing the later steps.
When should I use prompt chaining instead of one prompt?
Use a chain when one prompt asks for several different kinds of work, such as extracting, analyzing and writing, or when you need to check or reuse an intermediate result. If a single prompt already gives a reliable answer, a chain only adds calls, cost and waiting time.