liveBook · Manning

Chapter 3: Summarizing text using LangChain

What you will learn in chapter 3 of AI Agents and Applications

The introduction explains what a summarization chain is and why automating summarization is useful for large volumes of documents.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains what a summarization chain is and why automating summarization is useful for large volumes of documents.

Key terms: chain, summarization engines, large document volumes

3.1 Summarizing a document bigger than the context window

This section explains why a document can exceed an LLM’s prompt limit and introduces a chunk-and-summarize approach for handling it.

Key terms: context window, token limit, MapReduce, tokenizer, tokens, chunks, intermediate results

3.1.1 Chunking the text into Document objects

The section sets up a Moby Dick summarization example, chooses an initial chunk size, and explains how text length and model choice affect cost.

Key terms: chunks, 3,000 tokens, context window, GPT-5-nano, Moby-Dick.txt, LLMChain, tokens

3.1.2 Split

This section prepares the OpenAI model and explains how a LangChain split chain wraps a text-splitting function as a Runnable component.

Key terms: Runnable, RunnableLambda, split_text(), chunks, getpass, OPENAI_API_KEY, OpenAI model

3.1.3 Map

This section shows how a LangChain map chain summarizes document chunks independently, how its components pass outputs along, and how that step fits the MapReduce model.

Key terms: map chain, document chunk, RunnableParallel, summarize_map_chain, pipe operator (|), summarize_chunk_prompt, llm, StrOutputParser(), MapReduce, map operation

3.1.4 Reduce

This section explains how the reduce chain turns summaries of document chunks into one final summary.

Key terms: reduce chain, map chain, lambda function, summarize_ summaries_prompt

3.1.5 MapReduce combined chain

This section combines splitting, mapping, and reducing into one MapReduce summarization chain and identifies how chunk processing can run in parallel.

Key terms: MapReduce chain, document-splitting chain, map chain, reduce chain, map(), summarize_map_chain

3.1.6 MapReduce execution

This section explains how to inspect the MapReduce summary and warns about execution costs and API limits.

Key terms: print(summary), map_reduce_chain, 429 Rate Limit error, OpenAI API credit balance

3.2 Summarizing across documents

This section explains how to summarize content from multiple sources using MapReduce or progressive refinement, and when each approach is useful.

Key terms: Document, map operation, reduce operation, MapReduce, refine technique, current draft, final summary, content loss, processing load

3.2.1 Creating a list of Document objects

This section explains how preparing existing documents for summarization differs from splitting one large document, and why Document creation depends on the text source.

Key terms: chunks, Document objects, Document object, DocumentLoader, data source

3.2.2 Wikipedia content

This section explains how to load Wikipedia content for summarization and why the loader can return multiple documents.

Key terms: WebBaseLoader, WikipediaLoader, IMSDbLoader, AZLyricsLoader, Document list, maximum number of documents, summarization costs

3.2.3 File-based content

This section introduces the Paestum source files and explains how file loaders produce Document lists, contrasting that approach with manually creating token-sized documents.

Key terms: loader, documents, word_docs, pdf_docs, txt_docs, TextLoader, Python file reader, Document objects, tokens, LLM prompt

3.2.4 Creating the Document list

This section brings documents from multiple sources into one list for summarization and introduces loaders for mixed file types.

Key terms: Document list, refine technique, UnstructuredLoader, DirectoryLoader, document loaders

3.2.5 Progressively refining the final summary

This section builds a step-by-step chain that summarizes each document, progressively refines an overall summary, and exposes the final and intermediate results.

Key terms: doc_summary_chain, refine_chain, refine_summary(), full_summary, final_summary, intermediate_steps

3.3 Summarization flowchart

The section provides a flowchart for choosing a summarization approach based on document count, context-window fit, and the desired coverage of multiple documents.

Key terms: context window, stuff, MapReduce, refine technique

3.4 Summary

This section recaps how to choose and assemble LangChain summarization approaches for documents of different sizes and counts.

Where this is used later

Concepts explained in this chapter

← Chapter 2: Executing prompts programmatically Chapter 4: Building a research summarization engine →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.