Modern AI Engineering

Lesson 9.2 · 22 min · video lesson

Prompt Chaining: Decomposing Complex Tasks into Steps

What if, instead of asking one giant prompt to do five jobs at once, we let the model do one job at a time and check its work in between?

In short: Prompt chaining splits a complex task into a sequence of smaller LLM calls, where the output of one call becomes the input of the next. Between calls, ordinary code can validate, route or transform the data. Chains are easier to debug, test and control than one big prompt, at the price of more calls, more latency and the risk that an early mistake flows downstream.

What you will learn

  • What is a prompt?
  • What is Prompt Chaining?
  • Why do we need Prompt Chaining?
  • How does Prompt Chaining work step by step?
  • A real example of Prompt Chaining
  • Code example of Prompt Chaining
  • Common patterns in Prompt Chaining
  • Advantages of Prompt Chaining
  • Things to take care of while using Prompt Chaining
  • When to use Prompt Chaining

In this lesson

  1. What is a prompt, and what is prompt chaining?
  2. Why do we need prompt chaining?
  3. How does prompt chaining work, step by step?
  4. A real example of prompt chaining
  5. Code example of prompt chaining
  6. Common patterns in prompt chaining
  7. Advantages of prompt chaining
  8. Things to take care of while using prompt chaining
  9. When to use prompt chaining
  10. Worked example, step by step
  11. Practice: try it yourself

Key terms in this lesson

LLM call
One request to a language model: a prompt in, one response out.
Prompt chaining
Running several focused LLM calls in sequence, passing each output into the next prompt.
Gate
A code check between chain steps that decides to continue, retry or stop.
Routing
Choosing the next prompt or model based on the result of a classification step.
Fan-out / merge
Running a step on many pieces in parallel, then combining the results in one call.
Error propagation
A mistake in an early step being carried into and amplified by later steps.

This lesson is part of the Generative AI Engineering and Complete AI Engineer tracks. See plans and pricing to open the full lesson, its video, labs and quiz, or try the free lesson first (free account needed).

← 9.1 Chain-of-Thought Prompting: Making Models Reason Step by Step · 9.3 Prompt Caching: Reusing Computation Across API Calls →