Prompt Chaining
Prompt chaining splits a big task into several smaller prompts, where the output of one becomes the input of the next. Each step is simpler, easier to test and easier to fix than one giant prompt.
Example: From Reviews to an Action Plan
Step 1 prompt:
List every distinct complaint in these 50 reviews as short bullet points.
<reviews>...</reviews>
Step 2 prompt:
Group these complaints into at most 5 themes and count how many fall in each.
<complaints>{output of step 1}</complaints>
Step 3 prompt:
For the top 2 themes, suggest one concrete fix each for the product team.
<themes>{output of step 2}</themes>Sample final response: 1. Battery life (18 complaints): ship a firmware update that dims the screen after 30 seconds idle. 2. Confusing setup (11 complaints): add a 3-step setup video linked from the box.
Chaining in Code
Here is the idea in model-agnostic Python, where call_llm is whatever client you use:
def call_llm(prompt: str) -> str:
... # send prompt to your model and return the text
def summarise_then_translate(article: str) -> str:
summary = call_llm(f"Summarise in 3 sentences:\n<text>{article}</text>")
return call_llm(f"Translate to Urdu, keep it natural:\n<text>{summary}</text>")(returns an Urdu summary of the article)
Benefits
- Each prompt can use the best model or temperature for its step.
- You can validate intermediate results and stop early on errors.
- Easier debugging: you can see which step failed.
- Steps that do not depend on each other can run in parallel.
Tips
- Pass structured data (JSON) between steps where possible.
- Keep a log of every step's input and output.
- Do not over-split: each extra call adds latency and cost.
Frequently asked questions
What is the difference between prompt chaining and an agent?
In a chain, you fix the steps in code. An agent lets the model decide which steps or tools to use next.
When should I use prompt chaining?
When a single prompt gives inconsistent results because it is doing too many different things at once.
Does chaining cost more?
It uses more calls, but each is smaller, and you can use cheaper models for easy steps, so total cost can be similar.