Prompt Chaining
Split one overloaded prompt into a short chain of focused steps, where each step's output becomes the next step's input, so a failure points at one step.
Why Does This Exist?
One prompt asked to do five jobs does none of them well. Tell a model to clean up a review, pull out the product name, judge the sentiment, translate the result, and return strict JSON, and on a good day you get four out of five. The dropped constraint changes from run to run, which makes the failure hard to reproduce and harder to fix.
Prompt chaining answers by refusing to multitask. You cut the job into steps, give each step one prompt with one responsibility, and run them in order: the output of step one becomes part of the input to step two. When the chain breaks, the intermediate outputs tell you exactly which step to blame.
This page covers straight-line chains, fixed sequences where every step always runs. Loops that call tools and replan belong to the ReAct pattern, and splitting one question into parallel searches belongs to query decomposition. Both build on the idea here.
The one idea you need first: a prompt is text in, text out. A chain works because the text out of one call can be pasted into the text in of the next by ordinary code. If prompts still feel mysterious, start with prompt anatomy, then come back.
Think of It Like This
A sandwich shop line, not one cook doing everything
One cook taking the order, grilling, slicing, bagging, and working the register drops things. A sandwich line does not ask anyone to multitask: the first station grills, hands the tray down, the next station adds toppings, hands it down, the last station wraps. Each person sees only what the previous station handed them, and a bad sandwich can be traced to the station that touched it last.
That is a chain. Each LLM step is a station, and the tray is the text passed along.
Where it stops: sandwich stations are deterministic, and LLM steps are not. A grill cook never hallucinates a topping. An early LLM step can invent a product name with total confidence, and every step after it will faithfully process that fiction. That is why chains check each handoff before passing the tray, and sandwich lines do not have to.
How It Actually Works
The shape of a chain
Call the raw input , plain text the user provided. Call each step , where one step means one focused prompt plus one model call plus the small bit of code around it. The chain computes , then , and so on until , the final answer. In plain words: each is the output text of step , and the code pastes it into the prompt of step .
Nothing about this needs a framework. A chain is a few function calls in sequence, and the "framework" part is just discipline: one job per prompt, and the handoff between steps kept visible so you can inspect it.
A worked pass with small numbers
Take a 46 word customer review written half in Spanish. You want a 2 key JSON record: sentiment plus product name.
Step 1 cleans. Its prompt says only: translate to English and fix typos, output nothing else. Out comes , 38 English words. Say it takes 1.1 s.
Step 2 extracts. Its prompt sees only and asks for exactly two lines, sentiment and product. Out comes : negative, iphone 15 pro. Say it takes 0.8 s.
Step 3 formats. Its prompt sees only and asks for JSON with the two keys. Out comes , the record. Say it takes 0.5 s.
The serial total is s. A single mega prompt doing all three jobs might answer in roughly one step's time, but its failure mode is dropping a constraint at random. The chain is slower and its failures are located: if the JSON is fine but the product is wrong, shows you the extraction step misread , and steps 1 and 3 are cleared.
Why splitting wins
Specialization comes first. The extraction step is the hard thinking, so give it the stronger model; formatting JSON is clerical, so give it the smaller cheaper one. A mega prompt forces one model choice on every job at once.
Debugging comes second. Each is inspectable, so you fix the guilty prompt instead of rewriting a giant one and praying.
Typed handoffs come third. When a step must emit fields the next step parses, constrain its output shape with structured outputs rather than asking nicely in prose.
The trade is time for reliability: every extra step adds its full waiting time to the total. Skip chaining when one focused prompt already does the job, when your latency budget cannot absorb serial calls, or when the sub tasks are independent. Independent work should fan out in parallel, not queue up.
The whole design rests on one assumption: early outputs are good enough to build on. When they are not, small early slips grow into confident final errors, which is the first pitfall below.
Code
The point of this snippet is the plumbing, not the prompts. Three plain functions stand in for three model calls so you can see the one rule that defines a chain: each step consumes the previous step's output and nothing else.
import json
def step_clean(raw: str) -> str: """Stand-in for prompt 1: normalize the raw review.""" return " ".join(raw.lower().split())
def step_extract(clean: str) -> dict[str, str]: """Stand-in for prompt 2: pull exactly two fields.""" sentiment = "negative" if "terrible" in clean else "positive" product = "iphone 15 pro" if "iphone" in clean else "unknown" return {"sentiment": sentiment, "product": product}
def step_format(data: dict[str, str]) -> str: """Stand-in for prompt 3: shape the fields as JSON.""" return json.dumps(data, sort_keys=True)
raw = " Battery TERRIBLE, my new iPhone dies in two hours "x1 = step_clean(raw)x2 = step_extract(x1)x3 = step_format(x2)print(x1) # -> battery terrible, my new iphone dies in two hoursprint(x2) # -> {'sentiment': 'negative', 'product': 'iphone 15 pro'}print(x3) # -> {"product": "iphone 15 pro", "sentiment": "negative"}Swap each stand-in for a real model call and the structure stays identical. Notice carries only two fields: step 3 never sees the review, so it cannot be distracted by it. That blindness is the feature.
Watch Out For
An early slip poisons every step after it
The symptom is a clean miss: the final JSON parses, the sentiment looks sane, but the product name is wrong. You will interrogate step 3 for an hour before noticing step 2 invented the name and step 3 formatted the fiction perfectly.
Later steps trust their input completely, so an error at step 1 of 4 has three steps of room to grow. Validate each handoff before passing it on: check the keys exist, check values against an allowed set, and fail fast with the guilty attached instead of letting the chain finish. Keep each step so narrow that its output is checkable in a line or two of code.
Serial calls sum their waiting time
The symptom is a slow app with fast steps. Three steps at 1.1 s, 0.8 s and 0.5 s sound quick, but the user waits the full 2.4 s because nothing overlaps. Five-step chains regularly cross what chat users tolerate.
Fix it structurally, not by wishing models were faster. Run independent steps in parallel instead of chaining them, cap the depth at what the budget allows, and stream partial results where the interface permits. Pin the behavior of whatever remains with prompt regression tests, because every edit to an early prompt now re-times and re-shapes the whole chain.
The Quick Version
- Prompt chaining splits one overloaded multi-job prompt into focused steps, where each step's output becomes the next step's input.
- One step means one prompt with one responsibility, plus the small bit of code that pastes its output into the next prompt.
- Intermediate outputs are the payoff: when the chain breaks, they name the guilty step instead of leaving you guessing.
- It trades time for reliability, since serial steps sum their waiting time, as in 1.1 plus 0.8 plus 0.5 equals 2.4 seconds.
- Validate each handoff and fail fast, because later steps trust their input and an early slip grows downstream.
- Skip it for single focused tasks, tight latency budgets, or independent work that should run in parallel.