Langchain Summarization Techniques: When and How to Use Them?
Explore the best techniques for summarizing long texts with LLMs using Langchain.
Whether you’re building an AI assistant, analyzing long text articles, or compressing documents into useful insights, summarization is one of the most practical use cases for language models.
In this post, let's dive into the main types of summarization techniques and how you can implement each using LangChain.js. We’ll also share when to use which strategy, because not all summarizers are created equal.
Extractive vs. Abstractive Summarization
1. Extractive Summarization
Pulls key sentences or phrases directly from the source text. Think of it as copy-pasting the important bits.
Pros: Fast, accurate to the source, deterministic
Cons: Doesn’t paraphrase or restructure, might miss the big picture
2. Abstractive Summarization
Generates new sentences that paraphrase the meaning. This is what GPT-style models excel at.
Pros: Natural, high-level summaries
Cons: Can hallucinate, needs LLMs, higher cost
Summarization Techniques with LangChain
Let’s get practical. Here are the major summarization strategies, with examples using LangChain in Node.js. But first let's install the dependencies
npm install langchain openai dotenv
1. Map-reduce
As the name suggests, this techniques processes the content in two parts i.e mapping and reducing. Here's how it goes
Splits the input into chunks → summarizes each (Map) → combines the results into a final summary (Reduce).
Pros:
-
Scales well to large docs
-
More accurate final output than one-shot
-
Can run on streaming inputs
import { ChatOpenAI } from 'langchain/chat_models/openai';
import { loadSummarizationChain } from 'langchain/chains';
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
import { Document } from 'langchain/document';
const summarizeMapReduce = async (longText) => {
const model = new ChatOpenAI({ temperature: 0 });
const chain = await loadSummarizationChain(model, { type: 'map_reduce' });
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 100,
});
const docs = await splitter.createDocuments([longText]);
const res = await chain.call({ input_documents: docs });
console.log('Summary:', res.text);
return res.text;
};
2. Stuffing
This techniques loads (or stuffs) everything into the model context at once and summarizes it in a single call to the LLM. This technique is best used to summarize short document that can be provided to an LLM without touching the context window's boundary.
Pros:
-
Simple and fast
-
Accurate for small inputs
Cons:
-
Doesn’t scale to large documents (token limits!)
const summarizeStuff = async (text) => {
const model = new ChatOpenAI({ temperature: 0 });
const chain = await loadSummarizationChain(model, { type: 'stuff' });
const doc = new Document({ pageContent: text });
const res = await chain.call({ input_documents: [doc] });
console.log('📄 Summary:', res.text);
};
3. Refinement
This technique starts with a summary of the first chunk and iteratively improves it by processing one chunk at a time. This technique is useful for medium to large documents where you want more accurate summaries.
Pros:
-
Great for factual accuracy
-
Refines the story as it reads
const summarizeRefine = async (text) => {
const model = new ChatOpenAI({ temperature: 0 });
const chain = await loadSummarizationChain(model, { type: 'refine' });
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 100,
});
const docs = await splitter.createDocuments([text]);
const res = await chain.call({ input_documents: docs });
console.log('Summary:', res.text);
}
4. Extractive Summarization (Custom with direct prompts)
LangChain doesn’t have built-in extractive summarization like sentence scoring, but you can simulate it by prompting the model to extract key sentences only.
This isn’t "true" extractive summarization, but works well with GPT for light use cases.
const extractiveSummary = async (text) => {
const model = new ChatOpenAI({ temperature: 0 });
const prompt = `Extract the 3 most important sentences from this text:\n\n${text}`;
const res = await model.call(prompt);
console.log('Extractive Summary:', res.content);
};
When to use which?
Use Case | Technique | Best For |
Small article, blog post | Stuff | Fast, low-cost summaries |
Medium docs (e.g., essays, papers) | Refine | Balanced detail and accuracy |
Large reports, books | Map-reduce | Parallelizable, scalable |
Factually important docs | Refine | Iterative improvement |
Just need bullet points or key lines | Extractive | Quick takeaways |
I hope you learned something new in this blog!