Lesson 41.1 · 1 of 5 · 6 min read · Reviewed 24 Nov 2026
Prompt chaining
By the end, you can
- Describe prompt chaining and where code gates go.
- Decide when a fixed chain is the right pattern.
Prompt chainingPrompt chaining: Splitting a task into steps where each model call handles one step and passes its output to the next. breaks a task into a fixed sequence of steps, where each LLMLarge language model (LLM): A neural network trained on large amounts of text to predict the next token, which lets it generate and transform language. call works on the output of the one before.
Example
Write a product announcement:
- Generate an outline from the brief.
- Gate: code checks the outline has every required section and stays under the word budget. If not, stop or retry.
- Write the full draft from the approved outline.
- Translate the draft into three languages.
Why it works
Each call has one clear job, so its prompt can be short and specific, and its output is easier to check. Failures are easier to locate: you can see exactly which step went wrong.
When to use it
- The task divides cleanly into steps you can name in advance.
- You can check intermediate results with simple code.
- Extra latencyLatency: The time from sending a request until you get the response, or until the first token arrives when streaming. from sequential calls is acceptable.
When not to
If the steps depend on what the input turns out to be, the path cannot be fixed in advance. Look at routing (33.2) or orchestrator-workersOrchestrator-workers: A pattern where a lead model breaks a task into subtasks, hands them to worker calls and combines the results. (33.4).
Key points
- Chaining splits a task into fixed, sequential LLM calls.
- Code gates between steps catch problems early.
- It trades latency for accuracy on each simpler step.
Chain three steps with a code gate
Build a prompt chain where plain code checks the outline before the expensive draft step runs.
You will have: A chain of outline, gate, draft and translate steps that stops on a bad outline and completes on a good one.
Steps
- Read the stand-in model functions: they return fixed text so the lab is deterministic.
- Write gate(outline) to check every required section is present and the outline is under the word budget.
- Run the chain: outline, gate, draft, translate, stopping with a clear message if the gate fails.
- Count model calls so you can see the draft step was skipped when the gate failed.
- Run it on a brief that produces a bad outline, then on one that produces a good one.
Done when
- The bad brief stops at the gate, naming the missing section, after 1 model call.
- The good brief completes after 3 model calls.
- The gate itself makes no model call.
Open in the Playground No API key needed.
Starter code (Python)
REQUIRED = ["Problem", "Solution", "Pricing", "Call to action"]
calls = {"n": 0}
def outline_llm(brief):
calls["n"] += 1
sections = ["Problem", "Solution", "Call to action"] if "short" in brief else REQUIRED
return "\n".join("- " + s for s in sections)
def draft_llm(outline):
calls["n"] += 1
return "Draft with sections: " + ", ".join(l[2:] for l in outline.splitlines())
def translate_llm(draft):
calls["n"] += 1
return {"fr": "[fr] " + draft, "de": "[de] " + draft}
def gate(outline, max_words=60):
# TODO: return a list of problems: missing sections, or too many words
return []
def chain(brief):
calls["n"] = 0
outline = outline_llm(brief)
# TODO: run the gate; stop with a message if it finds problems
draft = draft_llm(outline)
return translate_llm(draft)
print(chain("a short launch note"), calls["n"])
Show a solution
REQUIRED = ["Problem", "Solution", "Pricing", "Call to action"]
calls = {"n": 0}
def outline_llm(brief):
calls["n"] += 1
sections = ["Problem", "Solution", "Call to action"] if "short" in brief else REQUIRED
return "\n".join("- " + s for s in sections)
def draft_llm(outline):
calls["n"] += 1
return "Draft with sections: " + ", ".join(l[2:] for l in outline.splitlines())
def translate_llm(draft):
calls["n"] += 1
return {"fr": "[fr] " + draft, "de": "[de] " + draft}
def gate(outline, max_words=60):
problems = [f"missing section: {s}" for s in REQUIRED if f"- {s}" not in outline]
if len(outline.split()) > max_words:
problems.append("outline over word budget")
return problems
def chain(brief):
calls["n"] = 0
outline = outline_llm(brief)
problems = gate(outline)
if problems:
return f"stopped at gate: {problems}"
draft = draft_llm(outline)
return f"done: {sorted(translate_llm(draft))}"
for brief in ["a short launch note", "a full launch announcement"]:
result = chain(brief)
print(f"{brief}: {result} (model calls: {calls['n']})")