Chapter 10 · Iterate and chain prompts#

The big idea#

Your first prompt is a draft. Look at the answer, find what is wrong, and change one thing. For big jobs, do not write one giant prompt: split the work into small steps where each prompt does one job and passes a short result to the next. This is prompt chaining.

Everyday example: A kitchen with one cook doing everything is slow and messy. A line with a chopper, a cook and a plater is fast, and you can taste after each station. Each station gets a simple, clear instruction.

In plain words

Check the output of each step (is it valid? is it short?) before it feeds the next one. A mistake caught at step 1 is cheap; at step 3 it is a mess.

A picture#

A prompt chain Each step does one job and hands a small, checked result to the next prompt. 1 Extract pull facts from the email 2 Decide pick the right team 3 Draft write the reply One big prompt vs three small ones Check the output of each step before the next one runs.
Each step does one job and hands a small, checked result to the next prompt.

Before and after#

Replies below are typical examples to show the difference. Your model will word things differently, so run the prompts yourself.

Example 1 · Iterating one prompt#

Before

Write a LinkedIn post about our new feature.

Typical reply

Excited to announce our new feature!!! We are thrilled to share an amazing update that will revolutionise the way you work. #innovation #growth #excited

After

Version 3 of the prompt: "Write a LinkedIn post announcing our new CSV export for a small-business accountant audience. Start with the problem (exporting takes 20 clicks), then the fix (one button), then one line asking for feedback. Max 80 words. No hashtags."

Typical reply

Exporting a month of invoices used to take 20 clicks. Now it takes one. Our new CSV export is live today: click Export, open it in Excel, done. If you do this every month, tell us what you'd want next.

Prompt checker (code/promptcheck.py, columns T C A F L E D):

before   1/7  [Y . . . . . .]  missing: context, audience, format, limits, example, delimiters
after    4/7  [. Y Y Y Y . .]  missing: task, example, delimiters

Why it works. Version 1 was vague, so the post was generic. Each round added one fix: audience, then structure, then limits. Keep each version so you can see which change helped.

Example 2 · Chaining three prompts#

Before

Read this customer email, work out what they want, decide which team handles it and write a reply.

Typical reply

Dear customer, thank you for your email. We will look into it and get back to you. (Which team? What urgency? No idea.)

After

Step 1: "Extract from the email inside <email>: customer name, problem, urgency (low/medium/high). Return JSON."
Step 2: "Given this JSON, choose one team: billing, access or tech. Reply with the team name only."
Step 3: "Write a 60-word reply to the customer, written by the {team} team, acknowledging the problem."

Typical reply

Step 1 -> {"name": "Priya", "problem": "charged twice", "urgency": "high"}
Step 2 -> billing
Step 3 -> Hi Priya, this is the billing team. I'm sorry you were charged twice. We are checking it now and will refund any duplicate within 24 hours.

Prompt checker (code/promptcheck.py, columns T C A F L E D):

before   2/7  [. Y Y . . . .]  missing: task, format, limits, example, delimiters
after    5/7  [. Y Y Y Y . Y]  missing: task, example

Why it works. Each step is short, testable and can be fixed alone. The one giant prompt hides which part failed.

A chain in code#

The prompts are plain strings, so a chain is just functions. Here ask stands in for a model call:

def ask(prompt):
    # replace with a real model call; canned answers keep this runnable
    canned = {"extract": '{"name": "Priya", "problem": "charged twice"}',
              "route": "billing",
              "reply": "Hi Priya, this is billing. We are refunding the duplicate charge."}
    return canned[prompt.split(":")[0]]

facts = ask("extract: <email>I was charged twice</email>")
team = ask("route: " + facts)
print(ask("reply: " + team))
Hi Priya, this is billing. We are refunding the duplicate charge.

When to iterate and when to chain. Iterate when one prompt nearly works. Chain when the task has clearly different stages, when you want to validate in between, or when one prompt is getting too long to reason about.

Rules of thumb#

  • Change one thing per iteration and keep every version.

  • Give each chain step one job and a small, checkable output.

  • Validate between steps (valid JSON? allowed label?) before continuing.

Common mistakes#

  • Rewriting the whole prompt every time, so you cannot tell what helped.

  • Passing long free text between steps instead of short structured values.

  • Chaining when one clear prompt would do; each step adds cost and delay.

Try it yourself#

  1. Split “Read this resume, rate it, and write feedback” into a chain.

  2. What should you check between steps 1 and 2 of the email chain?

  3. Your post prompt keeps giving hashtags. What is the smallest change?

Answers
    1. Extract skills and years as JSON. 2) Compare against the job’s requirements and score 1 to 5. 3) Write 3 feedback bullets using the score and gaps.

  1. That step 1 returned valid JSON with the expected keys and an allowed urgency value.

  2. Add “No hashtags.” to the limits. Change only that, then re-run.

Next

Models can invent facts. Next: ground the answer.