Lesson 2 / 25

Prompt Chaining and Routing

Split a task into sequential steps with checks, and route inputs to specialised handlers.

Chain, then gate

Prompt chaining breaks a task into steps where each output feeds the next, for example draft, then translate. Add a gate between steps, a simple check in code, so a bad intermediate result stops early. Routing classifies the input first and sends it to the prompt or model best suited for it.

A chain with a gate

llm stands for any function that calls a model. The gate stops the chain before wasting a second call on an empty outline.

def write_article(topic: str) -> str:
    outline = llm(f"Write a 5-point outline about {topic}")
    if outline.count("\n") < 4:          # gate: outline too short
        raise ValueError("Outline failed validation")
    return llm(f"Write an article from this outline:\n{outline}")

Route cheap work to cheap models

Simple questions can go to a small, fast model and hard ones to a stronger model. A router that is wrong too often costs more than it saves, so measure its accuracy.

Quick check: What is a "gate" in a prompt chain?

  • A paid API tier
  • A programmatic check between steps
  • A firewall rule
  • The final answer
Answer

A programmatic check between steps — A gate validates an intermediate output so errors do not flow into later steps.