Prompt chaining
One prompt with five jobs returns a polished reply with a wrong refund, and no way to see why. Watch the same task as a chain of four small prompts that pass their outputs forward and check the result.
10 minFree
- Extract
- Decide
- Draft
- Check
- Send
Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.
You are the booking desk assistant at Safar Travels.
Read the customer's email. Find the booking details. Check our refund
policy and work out the refund. Answer every complaint in the email.
Write a short, polite reply with the refund amount. At the end, add a
one-line summary for our records.
Policy:
- Cancelled 48 hours or more before departure: refund the fare
minus Rs 100 per ticket.
- Cancelled later than that: refund half the fare.
- The Rs 150 service fee is never refunded.
- Refunds reach the customer within 7 working days.
Email:
I booked 3 tickets from Chennai to Madurai for 12 December at your desk
(booking SF-2291, fare Rs 2,400 plus the Rs 150 fee). My mother is unwell,
so I cancelled on 9 December. Please refund my money. Also, your agent
was rude to me on the phone.What each call returned
Nothing yet. Press Play.
Background: the idea in plain words
As a prompt (the text you send to a model) grows, it tends to collect jobs. Read this email. And find the booking details. And apply the policy. And write a reply. And add a summary. Each job is easy. All of them in one prompt is where things start to slip.
There are two problems with such an overloaded prompt. First, the jobs depend on each other, so one small slip early on (three tickets read as one) flows silently into everything after it. Second, you get only one output: the final text. When it is wrong, you cannot see which job failed.
Prompt chaining is the fix. You split the big prompt into a chain of small prompts, and each small prompt has one job. Your code calls the model once per step. It takes the output of each step and pastes it into the prompt of the next step. Google's prompting guide puts it in one line: the output of one prompt in the sequence becomes the input of the next.
A tiny example. Step 1 reads a customer's email and returns a short list of facts. Step 2 gets those facts and the refund policy, and returns a decision. Step 3 gets the facts and the decision, and writes the reply. No step has to do two things at once.
A chain also gives you a natural place for a checking step: one more prompt whose only job is to compare the draft with the rules and say PASS or FAIL. If it says FAIL, your code can send the draft back to be fixed before any customer sees it.
Nothing is free. A chain means more calls to the model, so more waiting time and more tokens (the small pieces of text you pay for). This lesson shows both sides, so you can judge when a chain is worth it.
The running example is the booking desk of Safar Travels, a small agency that books train tickets. A customer cancelled three tickets and wants a refund. Press Play to watch one big prompt get it wrong. Then switch Try it to After: a chain of four small prompts, and finally to The checking step catches a mistake.
What to notice
- The big prompt failed quietly. The reply was polite and complete, and the amount was wrong. With one call you get one output, so you cannot tell whether the facts, the rule or the writing went wrong.
- Each step in the chain has one job. Extract, then decide, then write, then check. A step that only extracts gives the email its full attention. A step that only decides sees clean facts, not a long story.
- Your code carries the outputs forward. The model does not remember step 1 when you call it for step 2. Your code pastes the earlier output into the next prompt, inside tags such as
<facts>, so the parts stay apart. - Every step leaves an output you can inspect. You can log it, test it and see exactly where a failure started. Anthropic's guide gives this as the main reason to chain today.
- The price is calls and time. One call became four, and six when the checker asked for a rewrite. Chain when the task has clear stages and mistakes are costly. Keep one prompt when the task is simple.
Go deeperWhen to chain, and when not to
One prompt
One call, so it is the fastest and cheapest. Good for a single job, or for a few small jobs where a slip does not cost much. Hard to debug when it does several things at once.
A chain
One call per step, so it is slower and uses more tokens. Good when the task has clear stages, when you need to see or test the output of each stage, or when a wrong answer is expensive.
Signs that a prompt wants to be a chain:
- It contains several "and then" instructions that depend on each other.
- It fails in different ways on different days, and you cannot say which part failed.
- One part needs strict output (facts, a decision) and another needs free writing (a friendly reply).
- You want a rule check before anything reaches a customer.
Modern models change this advice a little. Anthropic's guide says that with built-in thinking, a model handles most multi-step reasoning internally, and that explicit chaining is still useful when you need to inspect intermediate outputs or enforce a specific pipeline. So do not chain only to help the model "think". For that, see Giving the model room to think. Chain because you want control points.
Go deeperThe checking step
Anthropic's guide calls the most common chain self-correction: generate a draft, review it against criteria, refine it based on the review. Each step is a separate call, so you can log, evaluate or branch at any point. A checking step works well when you follow four habits:
- Give the checker a narrow question. "Does the draft agree with this policy and this decision?" is checkable. "Is this good?" is not.
- Ask for a fixed first word and a reason. In
chain.pythe checker replies PASS, or FAIL and what is wrong. Your code branches on the first word, and the reason goes to the rewrite step. - Give the checker the source, not only the draft. It can only compare the draft with the policy if the policy is in its prompt.
- Put a limit on the loop. In
chain.pya draft gets two tries. After that the function returnsNEEDS_HUMANand a person takes over.
A checker is a model too The checking step can miss a mistake, or reject a draft that was fine. It lowers the risk. It does not remove it. For anything that can be checked with ordinary code, use code: the refund amount here is simple arithmetic, so a few lines of code could confirm Rs 2,100 with certainty. Use a model as checker for things code cannot judge, such as tone or whether every complaint was answered.
Go deeperOther shapes of a chain
The chain in this lesson is a straight line. Google's guide describes two more ways to break a prompt down:
- Choose a prompt by input. Instead of one prompt with many instructions, keep one prompt per instruction and let your code pick which one to run based on what the user sent. A refund email goes to the refund prompt. A new booking goes to another.
- Split and aggregate. When different parts of the data need different work, run those tasks in parallel on separate portions and then combine the results into the final output. Steps that do not depend on each other do not have to wait for each other, which wins back some of the time a chain costs.
Go deeperCounting the cost
Before you ship a chain, count three things for a typical request:
| Count | Big prompt | Chain in this lesson |
|---|---|---|
| Model calls | 1 | 4, or 6 with one rewrite |
| Waiting time | One call | The calls run one after another, so their times add up |
| Tokens | Policy and email sent once | Some text is sent again: the policy goes to step 2 and to step 4 |
Two things keep the cost down. Pass forward only what the next step needs: step 2 gets the short list of facts, not the whole email. And do not add a step unless you can say which failure it prevents. The lesson Tokens, cost and prompt caching covers the money side in detail.
Go deeperWords to know
| Word | Meaning in simple words |
|---|---|
| Prompt chaining | Splitting one task into several small prompts that run one after another |
| Step | One prompt in the chain, with one job, sent as one call to the model |
| Intermediate output | What a step in the middle returns. You can log it and test it |
| Checking step | A prompt whose only job is to compare a draft with the rules and say pass or fail |
| Self-correction | Draft, review, then rewrite using the review |
| Latency | The time you wait for the final result |
Check your understanding
Sources: the official documentation
This lesson is based on the official documentation. Check it for the latest details.
Something wrong or unclear in this lesson? Tell us, it gets fixed.