Lesson 1 of 7 · Patterns
Prompt Chaining
Prompt chaining splits a task into focused LLM steps with gates that check each output. Learn when to chain and how to design the steps, with a Python example.
- 5 min read
- Intermediate
- Reviewed
- Explain prompt chaining and why focused steps beat one big prompt
- Mix LLM steps with plain code steps
- Add gates that stop bad outputs from flowing downstream
- Decide when a task suits a chain
The simplest agentic workflow
Most dependable AI systems aren't autonomous agents. They're workflows: fixed sequences of steps, where LLM calls do the parts that need language understanding and code does everything else. This course covers the standard workflow patterns, starting with the most common.
A support-email chain
Four steps turn a customer email into a reply ready for review. The two LLM steps are stand-ins here, so the page runs without a key:
import json
# ---- Each function stands in for one focused LLM call (or plain code) ----
def extract(email):
"""Step 1 (LLM): pull out structured facts."""
return {"customer": "Dana Ruiz", "order": "A-5521", "issue": "item arrived damaged", "wants": "replacement"}
def gate(facts):
"""Gate (code): check step 1's output before continuing."""
missing = [k for k in ("customer", "order", "issue", "wants") if not facts.get(k)]
return missing
def decide(facts):
"""Step 2 (code): apply the business rule, no model needed."""
return "send a replacement" if facts["wants"] == "replacement" else "issue a refund"
def draft_reply(facts, action):
"""Step 3 (LLM): write the reply, using only checked facts."""
return (f"Hi {facts['customer'].split()[0]}, sorry your order {facts['order']} arrived damaged. "
f"We'll {action} today, and you don't need to return the item.")
def check_reply(reply, facts):
"""Step 4 (code): the reply must mention the order number and stay short."""
return facts["order"] in reply and len(reply.split()) < 60
email = "Hello, my order A-5521 came with a cracked lid. Could you send a new one? Dana Ruiz"
facts = extract(email)
print("1. extract:", json.dumps(facts))
problems = gate(facts)
print(" gate:", "passed" if not problems else f"stop, missing {problems}")
action = decide(facts)
print("2. decide:", action)
reply = draft_reply(facts, action)
print("3. draft:", reply)
print("4. check:", "OK to send for review" if check_reply(reply, facts) else "needs a person")- Extract (LLM): turn free text into fields.
- Gate (code): are all fields present? If not, stop now rather than writing a reply based on guesses.
- Decide (code): a business rule, no model needed, which makes it exact and auditable.
- Draft (LLM): write the reply from checked facts only.
- Check (code): final rules before a person reviews it.
Each LLM call has one small job, so its prompt can be short and specific, and each can be tested on its own.
When to chain
Chaining fits when a task naturally splits into steps you can name in advance: extract then summarise, outline then write, translate then check tone. It trades a little speed (several calls instead of one) for much higher accuracy and control.
Common mistakes
Write the gate between two steps of a prompt chain: stop bad extractions before they reach the next step.
import re
def gate(facts):
# Check the output of the extraction step before the chain continues.
# Return a list of problems:
# "missing <field>" for each of order, customer, issue that is empty or absent
# "bad order id" if the order is present but doesn't look like A-1234
return []
print(gate({"order": "A-5521", "customer": "Dana", "issue": "damaged"}))
print(gate({"order": "5521", "customer": "", "issue": "late"}))
What is the main purpose of a gate in a prompt chain?
Summary
- Prompt chaining splits a task into a fixed sequence of focused steps.
- Mix LLM steps (language) with code steps (rules, checks, calculations).
- Gates between steps stop bad outputs from spreading.
- Chains are easier to test, cheaper to tune and more accurate than one big prompt.
Next: routing, sending each input down the right path.
Finished reading? Tick it off to track your progress.