D4 · Prompt Engineering › Cluster 8 · Reliability, Hallucination & Iteration

8.3 · Chaining Complex Prompts

Decompose into linked steps

CONCEPT One hard prompt → a sequence of simple ones

Prompt chaining is breaking a complex task into a sequence of simpler subtasks, where each prompt is a separate API call and each step's output feeds the next step's input. The alternative — cramming everything into one giant prompt ("summarize, then fact-check, then translate, then format") — forces the model to juggle every requirement at once, and quality degrades as instructions compete for attention.

❌ One-shot mega-prompt: "Read these 12 reports, extract risks, rank them, draft an email, and translate it to French." → Errors compound silently; you can't tell which sub-step failed.
✅ Chain: extract risks → rank risks → draft email from the ranked list → translate the draft. Each step is small, inspectable, and independently fixable.

Anthropic's guidance is nuanced for current models: with adaptive thinking and subagent orchestration, Claude handles much multi-step reasoning internally. Explicit chaining (separate API calls) is still valuable when you need to inspect intermediate outputs or enforce a specific pipeline structure — logging, evaluating, or branching at each hop.

CONCEPT Why chains are more reliable and debuggable

PropertyOne mega-promptChained prompts
AccuracyRequirements compete; each is done "okay"Full attention per subtask → higher quality
DebuggabilityA wrong final answer — but which step?Inspect each step's output; isolate the failing hop
TraceabilityOne opaque callLog / evaluate / branch at any point
ReuseMonolithic, hard to repurposeSteps recombine into new pipelines

The most common chaining pattern is self-correction: generate a draft → have Claude review it against criteria → have Claude refine based on the review. Because each step is a separate call, you can capture and gate on the review before spending tokens on the refinement.

🔑 Hand results between steps with XML tags — pass the previous output back as, e.g., <draft>…</draft> so the next prompt knows exactly what it's operating on.

DIAGRAM The self-correction chain

flowchart LR IN["Task + input"] --> S1["Step 1: DRAFT
generate first pass"] S1 -->|"<draft> … </draft>"| S2["Step 2: REVIEW
check draft vs. criteria"] S2 -->|"<feedback> … </feedback>"| S3["Step 3: REFINE
apply the review"] S3 --> OUT["🟢 Final output"] S2 -.->|"inspect / log /
branch here"| DBG["🟠 Human or eval gate"] classDef ok fill:#d4edda,stroke:#28a745,color:#155724; classDef gate fill:#ffe8cc,stroke:#ff9f40,color:#9a4a0a; class OUT ok; class DBG gate;

CODE A three-step chain in code

Each step is its own messages.create call; the output of one is interpolated into the next, wrapped in XML tags. You can inspect, log, or gate between any two hops.

import anthropic
client = anthropic.Anthropic()

def step(prompt):
    r = client.messages.create(model="claude-opus-4-8",
        max_tokens=2048, messages=[{"role":"user","content":prompt}])
    return next(b.text for b in r.content if b.type == "text")

# Step 1 — DRAFT
draft = step(f"Draft a product-launch email from these notes:\n{notes}")

# Step 2 — REVIEW (feeds on step 1's output, wrapped in XML)
feedback = step(f"Review this draft for tone, clarity, and factual accuracy. "
                f"List concrete fixes.\n<draft>{draft}</draft>")

# --- inspect / log / gate here before spending tokens on step 3 ---

# Step 3 — REFINE (feeds on both prior outputs)
final = step(f"Rewrite the draft applying every fix.\n"
             f"<draft>{draft}</draft>\n<feedback>{feedback}</feedback>")

TRADE-OFFS Explicit chain vs. one call (or internal reasoning)

✅ Explicit prompt chain

  • Inspectable intermediate outputs — easy to debug
  • Enforces a fixed pipeline; gate/branch per step
  • Higher accuracy on genuinely multi-stage tasks

⚠️ Cost of chaining

  • More API calls → more latency and tokens
  • Orchestration code to write and maintain
  • Often unnecessary — adaptive thinking handles many multi-step tasks in one call
Chain explicitly when you need to see or gate the middle; let a single call with adaptive thinking handle internal multi-step reasoning when you don't.

EXAM TRAPS

Trap: "Always chain multi-step tasks into separate calls." → Not always — with adaptive thinking Claude does much multi-step reasoning internally. Chain when you need to inspect intermediate outputs or enforce a pipeline, not reflexively.
Trap: Confusing prompt chaining with tool use / agents. → Chaining is you orchestrating a fixed sequence of prompts where each output feeds the next. Agentic loops let the model decide the next action. Different control locus.
Trap: Passing prior output as a raw blob. → Wrap hand-offs in XML tags (<draft>, <feedback>) so the next step cleanly separates the carried content from its new instruction.
Trap: Thinking chaining's only benefit is accuracy. → Its biggest practical win is debuggability — a monolithic prompt fails opaquely; a chain tells you exactly which step broke.

PRACTICE

Q1 · A single prompt that summarizes, fact-checks, and translates produces mediocre results. Why does splitting it into three calls help?

Each subtask gets Claude's full attention instead of competing with the others, so quality per step rises. Just as important, you can inspect each intermediate output and see exactly which step (summary, check, or translation) is failing — impossible with one opaque call.

Q2 · Describe the self-correction chain and why each step is a separate API call.

Draft → review against criteria → refine from the review. Keeping the steps as separate calls lets you log, evaluate, or branch at any point — e.g. gate on the review before paying for the refine step, or loop the review until it passes.

Q3 · When is explicit chaining not worth it on current models?

When the multi-step reasoning is internal and you don't need to see or enforce the intermediate structure — adaptive thinking and subagent orchestration handle it in one call, saving the extra latency, tokens, and orchestration code that explicit chaining adds.