Skip to Content

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

Prompt chaining
  1. Extract
  2. Decide
  3. Draft
  4. Check
  5. Send
FIG. PE-PROMPT-CHAINING / 07 PARTSCustomerrefund emailYour codebooking desk appOne big prompt1 call, 5 jobs1 Extractmodel call2 Decidemodel call3 Draftmodel call4 Checkmodel call

Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.

00/05
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 words2 min read

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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 deeper

When to chain, and when not to

1 min read

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 deeper

The checking step

1 min read

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:

  1. Give the checker a narrow question. "Does the draft agree with this policy and this decision?" is checkable. "Is this good?" is not.
  2. Ask for a fixed first word and a reason. In chain.py the checker replies PASS, or FAIL and what is wrong. Your code branches on the first word, and the reason goes to the rewrite step.
  3. 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.
  4. Put a limit on the loop. In chain.py a draft gets two tries. After that the function returns NEEDS_HUMAN and 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 deeper

Other shapes of a chain

1 min read

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 deeper

Counting the cost

1 min read

Before you ship a chain, count three things for a typical request:

CountBig promptChain in this lesson
Model calls14, or 6 with one rewrite
Waiting timeOne callThe calls run one after another, so their times add up
TokensPolicy and email sent onceSome 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 deeper

Words to know

1 min read
WordMeaning in simple words
Prompt chainingSplitting one task into several small prompts that run one after another
StepOne prompt in the chain, with one job, sent as one call to the model
Intermediate outputWhat a step in the middle returns. You can log it and test it
Checking stepA prompt whose only job is to compare a draft with the rules and say pass or fail
Self-correctionDraft, review, then rewrite using the review
LatencyThe time you wait for the final result

Check your understanding

1. One prompt extracts facts, applies a policy, writes a reply and adds a summary. The refund amount in the reply is wrong. Why is this hard to fix?
2. In a chain, how does step 2 know what step 1 found?
3. A user types a simple one-line question in a live chat and waits for the answer. Should you run a four-step chain with a checker?
Sources: the official documentation2 links

Something wrong or unclear in this lesson? Tell us, it gets fixed.