Lesson 4 of 4 Conversational Systems and Prompt Flows

Chaining Focused Prompts for Complex Tasks

This lesson describes common prompt chaining patterns and where to implement them, since complex tasks are more reliable as a chain of small, focused prompts than as one large prompt.

Learning outcomes

After completing this lesson, you will be able to:

  • Build sequential chains.
  • Use branching, parallel and iterative chains.
  • Choose where to implement a chain.

Sequential chains

A single prompt that asks a model to extract facts, analyse them and write a polished report at once often does each part poorly, and when the result is wrong it is hard to tell which part failed. Prompt chaining divides the work. In a sequential chain, the output of one prompt becomes the input of the next: extract facts from a document, then analyse them, then write a summary for a specific audience.

Each step has its own instructions, examples, model and parameters, so a small, cheap model can handle extraction while a larger model writes the analysis. Each step can also be tested and improved separately. Validation between steps, such as checking that extracted JSON is valid and complete, stops an error in an early step from propagating silently into the final result.

True or false

Why chain prompts?

Exam skills 1.6.6

Check three claims about splitting work into a chain of prompts.

Log in to try this activity Interactive activities are free with an account.

Branching, parallel and iterative chains

Chains are not always a straight line. In conditional branching, one prompt's output, such as a classification, determines which prompt runs next; a support request classified as a billing question, for example, goes to a prompt with billing rules and examples. Parallel chains run independent prompts at the same time, such as analysing each section of a contract separately, and merge the results, which reduces total latency.

Map-reduce handles documents longer than the context window: each chunk is summarised separately, and the summaries are then summarised together. Evaluator-optimizer loops improve quality iteratively: one prompt generates a draft, another critiques it against defined criteria, and the draft is revised until the critique passes or a maximum number of iterations is reached. The iteration limit is essential, because without it a loop that never satisfies the critic would run, and cost money, indefinitely.

The following diagram shows a map-reduce chain that summarises a 300-page report.

Map-reduce summarisation of a 300-page report. Step Functions splits the report into sections, a Map state summarises the sections in parallel and extracts risks from each summary, and a final prompt combines the section summaries and risks. Parallelism keeps the total time within minutes.
The number of sections depends on the report and the context window.
Match the pairs

Chain patterns

Exam skills 1.6.6

Match each chaining pattern with an example of its use.

Log in to try this activity Interactive activities are free with an account.

Where to implement chains

Chains can be implemented in three places, each suited to different needs. Amazon Bedrock Flows implement chains declaratively, as connected nodes, with versioning and no orchestration code, which suits prompt-centric chains that business users may also adjust. AWS Step Functions implements chains with Bedrock service integrations, Parallel and Map states, retries, error handling and waits of up to a year, which suits production workflows that also call other AWS services or need human approval.

Application code, or an agent framework, implements chains when they are embedded in an application and need custom logic between steps. Whichever is used, prompts from Prompt Management can be shared across all three, so that the same approved prompt is used wherever a step appears. The following table summarises the chaining patterns.

PatternUse forTypical implementation
SequentialExtract, analyse, writeFlows or Step Functions sequence
Conditional branchingDifferent handling per categoryFlows condition node; Step Functions Choice
Parallel / map-reduceIndependent parts; long documentsStep Functions Parallel or Map; Flows iterator
Evaluator-optimizerQuality-critical draftsStep Functions loop with iteration limit
Worked example

Summarising 300-page reports

A firm must summarise 300-page reports that exceed the model's context window, highlighting risks, within a few minutes.

A single prompt cannot hold the report. A map-reduce chain in Step Functions splits the report into sections, summarises them in parallel with a Map state, extracts risks from each section summary, and then combines the section summaries and risks in a final prompt. Parallelism keeps the total time within minutes.

Check yourself

What stops an evaluator-optimizer loop from running indefinitely?

Show answer

An iteration limit, after which the best result so far is returned or the task is escalated.

Exam Tips

Remember for the exam

  • Split complex tasks into focused prompts and validate between steps.
  • Long documents beyond the context window: map-reduce chains.
  • Quality-critical output: evaluator-optimizer loop with an iteration limit.
  • Declarative chains: Flows; chains with AWS integrations and waits: Step Functions.