Intermediate level · Module 2: Prompt Chaining and Task Decomposition · Lesson 1 of 3
Implementing Prompt Chains
Learners implement a research chain (understand the question, plan searches, gather evidence, draft, check) as separate Python functions with typed inputs and outputs.
In this lesson you will
- Implement a chain as separate functions
- Pass validated intermediate outputs between steps
Key concepts
chain steptyped input and outputintermediate resultsingle responsibilitycomposition
The lesson
Each step is a function with a clear input and output: plan_searches(question) returns a list of queries; gather(queries) returns evidence; draft(question, evidence) returns a report draft. Small steps are easier to prompt, test and debug.
Validate intermediate outputs before passing them on, so that a bad plan does not silently produce a bad report.
Chains cost more calls than a single prompt, but usually give more reliable results on multi-part tasks.
Step by step
- Write the chain as four functions.
- Add a validation check after the planning step.
- Run five questions and log every intermediate result.
- Compare quality with a single-prompt version.
Practice activity
Implement and compare a four-step research chain with a single prompt.
Expected result: A working chain with logged steps and a measured comparison.
Check your understanding
-
Why validate intermediate outputs?
Show answer
To stop errors in early steps from silently spreading to the final result.
-
What is the main cost of chaining?
Show answer
More model calls, so more tokens and time.
Further learning
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.