Sub topic 10
Hierarchical summarisation for very long documents
This chapter assumes you know basic Python, have sent prompts to an LLM through an API, and understand tokens. It also assumes you have read the chapters on chunking and map-reduce. Hierarchical summarisation extends map-reduce for documents that are too long for one combine step.
Why plain map-reduce runs out of room
In map-reduce, each chunk gets a partial summary, and the partial summaries are joined and summarised again. This works when the joined partial summaries fit in one call. For a very long document, such as a 600-page report or a year of meeting transcripts, they may not.
Suppose a document has 1,200,000 tokens and each chunk is 3,000 tokens. That gives 400 chunks. If each partial summary is 200 tokens, the joined text is 80,000 tokens. That is larger than a 3,000-token budget, and it may be larger than the model's context window as well.
The summarise function from the map-reduce chapter already handles this by calling itself on the joined text. That recursion is hierarchical summarisation. This chapter makes the structure explicit so that you can control it.
The tree of summaries
Think of the process as a tree. The leaves are chunks of the original text. Each level up holds summaries of groups of nodes from the level below. The root is the final summary.
The number of levels grows slowly. If each level shrinks the text by a factor of 10, a 1,200,000-token document needs about four levels to reach 3,000 tokens or fewer. Each level is a full map step, and every call within a level is independent.
Grouping by structure
You do not need to group nodes arbitrarily. Documents usually have structure: chapters, sections, speakers, dates. Grouping along that structure gives better intermediate summaries, because each group covers one topic.
Here is a version that works on a list of sections, where each section is a list of chunks. It summarises each section, then combines the section summaries.
def summarise_group(texts, call_llm, instruction):
joined = '\n\n'.join(texts)
return call_llm(instruction + '\n\n' + joined)
def hierarchical_summary(sections, call_llm, count_tokens, max_tokens=3000):
section_summaries = []
for chunks in sections:
partials = [
summarise_group(
[c], call_llm,
'Summarise this passage in 5 bullet points. Use only information in the text.'
)
for c in chunks
]
section_summaries.append(
summarise_group(
partials, call_llm,
'Combine these bullet points into one short paragraph. Use only information in the text.'
)
)
level = section_summaries
while len(level) > 1:
next_level, group, size = [], [], 0
for s in level:
n = count_tokens(s)
if group and size + n > max_tokens:
next_level.append(summarise_group(
group, call_llm,
'Combine these summaries into one short paragraph. Use only information in the text.'))
group, size = [], 0
group.append(s)
size += n
if group:
next_level.append(summarise_group(
group, call_llm,
'Combine these summaries into one short paragraph. Use only information in the text.'))
if len(next_level) >= len(level):
raise ValueError('Summaries are not being merged; lower the budget or shorten the output')
level = next_level
return level[0]The loop stops when one summary remains. The guard raises an error if a level fails to reduce the number of nodes. Without it, a group size of one would loop forever.
Failure modes
- Compounding loss. Each level discards detail. A fact that survives the first summary may be dropped at the second. Specific numbers and names disappear first.
- Compounding error. A mistake at a low level is repeated as fact at every level above it. The higher models never see the source text.
- Uneven weight. A section with ten chunks produces a bigger share of the partial summaries than a section with one chunk, even when the short section matters more.
- Boundary effects. A topic that spans two groups may be summarised twice, in two different ways, and then merged badly.
Mitigations
- Tell each level what to keep, such as names, dates, and figures. Repeat the instruction at every level.
- Keep intermediate summaries somewhat longer than you think necessary, and let only the final level be short.
- Store every intermediate summary with the ID of its source chunks. This lets you trace a claim in the final summary back to the text.
- Prefer structural boundaries to fixed-size groups.
When to use it
Use hierarchical summarisation when the joined partial summaries exceed your budget. For shorter documents, one map-reduce pass is cheaper and loses less. For documents where order matters and early details must stay visible, consider refine, accepting its sequential cost.
Exercise
A document has 900,000 tokens. You use 3,000-token chunks, and each summary at every level is about 10% of its input. The combine budget is 3,000 tokens. How many levels of summarisation are needed, counting the first chunk-level pass?
Show answer
Level 1 turns 900,000 tokens into about 90,000. Level 2 gives about 9,000. Level 3 gives about 900, which fits in the 3,000-token budget. So three levels are needed, and the last one produces the final summary.
Exercise
Why does the guard in hierarchical_summary raise an error when len(next_level) >= len(level)?
Show answer
If a level produces as many nodes as it had before, merging has stopped making progress. This happens when each group holds only one summary, so the loop would repeat forever.