0PricingLogin
AI Prompt Engineering · Lesson

Map-Reduce Summarization Pattern

Summarize each chunk independently then synthesize the summaries.

The Map-Reduce Pattern

When a document exceeds the LLM context window, you cannot pass it all at once. The map-reduce pattern solves this:

  • Map: summarize each chunk independently
  • Reduce: synthesize all chunk summaries into one final summary

This mirrors the classic MapReduce from distributed systems — the same divide-and-conquer logic applied to language tasks.

Step 1: The Map Phase

In the map phase, each chunk is sent to the LLM with a summarization prompt. The model returns a short summary of that chunk only. These summaries are collected into a list.

Each summary should be much shorter than the original chunk — typically 10–20% of the original length. This compression is what makes the reduce step feasible.

import openai

client = openai.OpenAI(api_key='sk-...')

def summarize_chunk(chunk, model='gpt-4o'):
    resp = client.chat.completions.create(
        model=model,
        messages=[
            {'role': 'system', 'content': 'Summarize the following text concisely in 3-5 sentences.'},
            {'role': 'user', 'content': chunk}
        ]
    )
    return resp.choices[0].message.content

def map_phase(chunks):
    return [summarize_chunk(c) for c in chunks]

All lessons in this course

  1. Chunking Strategies for Long Texts
  2. Map-Reduce Summarization Pattern
  3. Hierarchical Summarization
  4. Maintaining Context Across Chunks
← Back to AI Prompt Engineering