Some jobs are several jobs at once. Summing up a week of café reviews means reading each review, deciding what it complains about, counting, and writing a note for the manager. You can ask a language model for all of that in one prompt, and you’ll get an answer, just not one you can check. Prompt chaining splits the job into steps, each a small prompt or a few lines of ordinary code, with each step’s output feeding the next. Here a real model does the same job both ways. Chain the steps so you can see, check and fix each one.
One prompt for everything#
Bean There is a made-up café with twelve new reviews. We read them and noted which of six complaints each one makes: slow service, cold food or drinks, noise, prices, cleanliness, staff attitude. That gives the true counts: 3, 3, 2, 2, 2 and 2. Some reviews praise exactly the things others complain about (“Quick service even at the morning rush”), and one complains about something that isn’t on the list (“I just wish they opened before 8”).
The obvious approach is a single prompt: here are the twelve reviews, count the complaints in each category. The model is Qwen3-1.7B, small enough to run on a laptop.
Predict first
Shuffle the same twelve reviews into a different order and ask again. What happens to the counts?
The single prompt overcounts in every order, finding 15 to 32 complaints where there are 14, and nothing in its reply says which reviews it counted, so there’s no way to tell what went wrong or what to fix. Defining each category didn’t help: off by 6, 12 and 5, and in the third order the model listed review numbers instead of counts, so its reply can’t be read at all. When one prompt does everything, there’s nowhere to look when it goes wrong.
The same job as a chain#
Now split it into three steps:
- Label. For each review on its own, ask the model which categories it complains about, as a JSON list. Twelve small calls instead of one big one.
- Count. Add up the lists in code. Counting is not a job for a language model.
- Write. Give the counts to the model and ask for a two-sentence note to the manager.
Between the steps sits a check, also code. Every reply must parse as a JSON list, and every label must be one of the six categories. When a check fails, the chain can repair the reply, ask again, or stop and hand over to a person.
The checks earned their place straight away. All twelve labelling replies came wrapped in Markdown code fences, though the prompt said “nothing else”, so the check strips them before parsing. One reply used “cold food”, which isn’t a category; the chain sent it back with the list of real ones, and the second answer was right.
The result: off by 3 in total, in every order, because each review is read on its own and the order can’t matter. And when it’s wrong, you can see where. Eight of the twelve reviews are labelled exactly right. Of the other four, one misses “Sticky tables and an overflowing bin” as a cleanliness complaint, one files a barista rolling his eyes under slow service, and two miss a long wait sitting next to another complaint: “Twenty minutes for a pot of tea”, “a queue that barely moved”. That’s a to-do list for step 1 alone. Add examples of exactly those cases to its prompt (few-shot prompting), or give that one step a bigger model, and rerun it without touching the rest. Definitions alone didn’t do it: with them, the eye-rolling review got no label at all.
Give code the jobs code does well#
Step 3 is the simplest of all: here are the counts, write a note about the biggest problem.
Predict first
Step 3 gets the counts: cold food or drinks 3, slow service 2, everything else 2 or fewer. What does its note say is the biggest problem?
Picking the largest number is not a language task, so don’t give it to a language model. In the second note, code picks the biggest problem and the model only writes the suggestion, and it names cold food or drinks. The same goes for every step of a chain: anything that can be counted, compared, looked up or checked with a rule belongs in code, where it’s exact, instant and free. If a step can be done by code, let code do it.
What a chain costs#
Fourteen model calls instead of one: twelve labels, one retry and the note. About four times the tokens (1,766 against 410) and four times the time on our CPU, 42 seconds against 10, though the twelve labelling calls don’t depend on each other and could all run at once. Chaining is worth it when:
- the job has distinct stages, such as extract, then decide, then write;
- you need to check or fix the middle, because a wrong count or label costs something;
- a step can be code, or needs a different model or settings from the others;
- the input is long, and one item at a time keeps each prompt small.
For a short, single-skill task that one prompt does well, one prompt is the right answer.
In code#
The three steps and the check, with the same small model, free and local (pip install transformers torch), on five of the reviews:
import json, re
from collections import Counter
from transformers import AutoModelForCausalLM, AutoTokenizer
name = "Qwen/Qwen3-1.7B"
tok = AutoTokenizer.from_pretrained(name)
model = AutoModelForCausalLM.from_pretrained(name)
def ask(prompt, history=()):
messages = [*history, {"role": "user", "content": prompt}]
text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=False)
inputs = tok(text, return_tensors="pt")
out = model.generate(**inputs, max_new_tokens=60, do_sample=False)
return tok.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
CATS = ["slow service", "cold food or drinks", "noise", "prices", "cleanliness", "staff attitude"]
reviews = [
"Lovely flat white and the staff were sweet, but we waited 25 minutes for two toasties.",
"My latte was lukewarm by the time it reached the table, and £4.80 for a small one is steep.",
"So loud at lunchtime I couldn't hear my friend across the table. My soup was cold, too.",
"Prices have gone up again: £3.90 for a filter coffee. Still tastes great.",
"Crumbs all over the seats and the toilet hadn't been cleaned. Won't be back.",
]
def step1(review):
"""One review in, a checked list of categories out."""
prompt = (f"Which of these complaint categories does this review complain about: {', '.join(CATS)}? "
"Praise doesn't count. Reply with a JSON list of the categories, or [] if none, and nothing else."
f"\n\nReview: {review}")
history = []
for attempt in range(2):
reply = ask(prompt, history)
labels = json.loads(re.sub(r"^```(?:json)?\s*|\s*```$", "", reply.strip())) # check: strip fences, parse
unknown = [label for label in labels if label not in CATS] # check: real categories only
if not unknown:
return labels
history = [{"role": "user", "content": prompt}, {"role": "assistant", "content": reply}]
prompt = f"Your list used {unknown}, which aren't categories. Reply again using only: {', '.join(CATS)}."
return [label for label in labels if label in CATS]
lists = [step1(review) for review in reviews] # step 1: the model, one review at a time
counts = Counter(c for labels in lists for c in labels) # step 2: plain code
top, n = counts.most_common(1)[0]
note = ask(f"The most common complaint in this week's reviews was {top} ({n} reviews). " # step 3: the model again
"Write one sentence to the café manager with one practical thing to do about it.")
for review, labels in zip(reviews, lists):
print(f"{review[:45]}... -> {labels}")
print(dict(counts))
print(note)
# Lovely flat white and the staff were sweet, b... -> ['slow service']
# My latte was lukewarm by the time it reached ... -> ['prices', 'cold food or drinks']
# So loud at lunchtime I couldn't hear my frien... -> ['noise', 'cold food or drinks']
# Prices have gone up again: £3.90 for a filter... -> ['prices']
# Crumbs all over the seats and the toilet hadn... -> ['cleanliness']
# {'slow service': 1, 'prices': 2, 'cold food or drinks': 2, 'noise': 1, 'cleanliness': 1}
# Please check if the prices are accurate and consider adjusting them to be more competitive to attract customers.All five reviews come back labelled exactly right this time, and step 2 is a one-lineCounter. Prices and cold drinks tie on two; most_common picks the one it counted first, a rule you’d want to choose on purpose in a real report. Every step can be tested on its own, with its own examples, which is the whole point.
What to remember
- Prompt chaining splits a job into steps, each a small prompt or plain code, so every result in the middle can be read, checked and fixed.
- Here one big prompt’s counts were off by anything from 3 to 20 depending on the order of the reviews; the chain was off by 3 in every order, and showed exactly which reviews it misread.
- Put checks between the steps and give code the counting and comparing; the price is more calls, 14 instead of 1 here.