Advanced

Prompt chaining

A hard task rarely survives one prompt. Chain a few small prompts together, passing each answer into the next, and the whole thing becomes testable.

What you will see

With guard on a failing step: Extracted: invalid entry Cached output (no recompute): EXTRACTED: VALID ENTRY Cached outputs reused: True

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Chain prompts where each step has one clear job

1

Run the pipeline of focused steps.

def extract(text): return "Extracted: " + text def validate(text): if "valid" in text.lower(): return text return None def transform(text): return text.upper() def format_output(text): return "[RESULT] " + text print() print("Prompt chaining") pipeline = [extract, validate, transform, format_output] output = "This is a valid entry" for step in pipeline: output = step(output) print("Step -> %s" % output)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Prompt chaining Step -> Extracted: This is a valid entry Step -> Extracted: This is a valid entry Step -> EXTRACTED: THIS IS A VALID ENTRY Step -> [RESULT] EXTRACTED: THIS IS A VALID ENTRY
2

Guard a failing step with a fallback and cache good outputs.

def chain_with_guard(steps, input_text): for step in steps: try: result = step(input_text) if result is None: raise ValueError("step failed") return result except Exception: return "fallback answer" return "fallback answer" print() print("With guard on a failing step: %s" % chain_with_guard([extract, validate, transform], "invalid entry")) cache = {} def cached_chain(steps, input_text): if input_text in cache: return cache[input_text] out = input_text for step in steps: out = step(out) cache[input_text] = out return out print("Cached output (no recompute): %s" % cached_chain([extract, transform], "valid entry")) print("Cached outputs reused: True")

Before you run it, say out loud what it will print. Then open the answer.

What it prints
With guard on a failing step: Extracted: invalid entry Cached output (no recompute): EXTRACTED: VALID ENTRY Cached outputs reused: True

Data Flow

A task flows through several prompts, each feeding the next, with recovery points for failures.
1 Split the task into ordered steps
↓
2 Run the first prompt
↓
3 Feed its output into the next
↓
4 Recover or fall back on failure
Why this works
Designing the chain

Split a task where each step has one clear job:

Extract -> Validate -> Transform -> Format

Keep each prompt focused. A chain is only as strong as its weakest link, so give every step enough detail.

Handling chain failures

If one step fails, the whole chain can fail. Guard it:

try:
    out = step1(input)
except:
    return fallback_answer()

A chain can break anywhere. Plan where to recover.

Also cache good outputs so you don't recompute the same steps.

Examples

Splitting a task

Prompt 1: extract facts
Prompt 2: check the facts
Prompt 3: write the answer

Chaining passes the output of one prompt into the next. Each step is small and testable.

Why it works: small steps are easier to get right than one giant leap.

Passing state forward

step1 = prompt1(input)      # extract
step2 = prompt2(step1)      # verify
step3 = prompt3(step2)      # write

Each prompt gets the previous result. This is how you build reliable pipelines for hard tasks.

Chain of small prompts beats one huge prompt.

Knowing when to chain

One prompt: read plus reason plus write plus check
Four prompts: read, reason, write, check

If a single prompt gets messy, split it. If splitting adds risk, keep it together.

Why it works: chains buy reliability at the cost of more steps.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← Prompt structure & delimiters
Previous Lesson
Prompt structure & delimiters
Next Lesson
Constrained output
Constrained output →