Press ESC to exit fullscreen
📖 Lesson ⏱️ 90 minutes

Prompt Chaining and Pipelines

Connect multiple prompts to build multi-step AI workflows

What is Prompt Chaining?

Prompt chaining breaks a complex task into smaller, sequential steps. Each step is a separate LLM call, and the output feeds into the next prompt.

Why not do everything in one prompt?

  • Large tasks exceed context windows
  • Intermediate validation catches errors early
  • Each step can use a different model or temperature
  • Easier to debug — you can inspect each step’s output

A Simple Chain: Research Report

import anthropic

client = anthropic.Anthropic()

# Step 1: Extract key claims from source material
step1_response = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1000,
    messages=[{"role": "user", "content": f"Extract the 5 most important claims from this text. Return as a numbered list.\n\n{source_text}"}]
)
claims = step1_response.content[0].text

# Step 2: Evaluate each claim
step2_response = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=2000,
    messages=[{"role": "user", "content": f"For each claim below, assess: (1) is it well-supported? (2) what evidence would strengthen it?\n\n{claims}"}]
)
analysis = step2_response.content[0].text

# Step 3: Synthesize into a report
step3_response = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=1500,
    messages=[{"role": "user", "content": f"Write a 3-paragraph executive summary based on this analysis:\n\n{analysis}"}]
)
report = step3_response.content[0].text

When to Chain vs. Single Prompt

Use chaining whenUse single prompt when
Task has >3 distinct stepsTask is a single, clear operation
Steps can fail independentlyThe whole task fits comfortably in context
You need intermediate validationSpeed is more important than reliability
Different steps need different expertise