Sub topic 9
Refine: carry a running summary forward
This chapter assumes you can write basic Python, have sent prompts to an LLM, and know what a token is. It also assumes you have already split a document into chunks, as in the previous chapters on chunking and map-reduce.
The idea
Map-reduce summarises every chunk on its own and then merges the results. Refine works differently. It reads the chunks in order and keeps one running summary. For each new chunk, the model gets the current summary plus the chunk and returns an updated summary. After the last chunk, the running summary is the answer.
The code
def refine_summary(chunks, call_llm):
summary = ''
for chunk in chunks:
if not summary:
prompt = 'Summarise this text:\n\n' + chunk
else:
prompt = (
'Here is the summary so far:\n' + summary +
'\n\nUpdate it using the next part of the document. Keep it under 200 words.\n\n' + chunk
)
summary = call_llm(prompt)
return summaryHere call_llm is any function that takes a prompt string and returns the model's text. The first chunk has no summary to build on, so it gets a plain summarisation prompt. Every later call includes the previous summary, the instruction, and the next chunk.
The word limit in the prompt matters. Without it, the summary tends to grow with each step. A growing summary eventually competes with the chunk for space in the context window.
Using it with chunks
You can feed it the output of chunk_paragraphs from the chunking chapter:
chunks = chunk_paragraphs(document_text, 3000, count_tokens)
result = refine_summary(chunks, call_llm)
print(result)Each call needs room for the summary, the instruction, and one chunk. If the budget is 3000 tokens per chunk and the summary is up to about 300 tokens, the prompt stays near 3400 tokens. Set the chunk budget with that overhead in mind.
Strengths and weaknesses
| Property | Refine | Map-reduce |
|---|---|---|
| Document order | Kept, because chunks are read in sequence | Kept only if the combine step preserves it |
| Parallel calls | No, each call needs the previous result | Yes, the map step runs in parallel |
| Number of calls | One per chunk | One per chunk plus the reduce calls |
| Early details | Can fade as they are rewritten many times | Each chunk is summarised once, so details are treated evenly |
| Good fit | Narratives, meeting transcripts, anything that builds over time | Reports with independent sections |
The main failure is drift. Each rewrite is a chance for the model to drop a detail or restate it a little differently. By chunk 20, a fact from chunk 2 has been rewritten 18 times. Recent chunks also tend to get more space than early ones, because the model has the newest text in front of it.
A second failure is that errors carry forward. If the model misreads a point in chunk 4, every later step starts from that mistake.
Reducing drift
- Tell the model what must survive. For example, "Keep all names, dates and decisions."
- Give a firm length limit, so the model must choose what to compress instead of simply appending.
- Ask it to use only the summary and the new text. Add "Do not add information that is not in the text."
- Keep chunks fairly large. Fewer chunks mean fewer rewrites.
- For critical facts, extract them into a separate list that is only appended to, and keep it outside the rewritten summary.
When to choose refine
Choose refine when order and continuity matter and you can accept slower runs. Examples are a long email thread, a legal timeline, or a chaptered story. Choose map-reduce when you want speed, or when the sections are independent. If a document is long enough that even the partial summaries do not fit in one call, the next chapter covers hierarchical summarisation.
Exercise
A 40-chunk transcript is summarised with refine_summary. The final summary barely mentions the first ten minutes of the meeting. Give two likely causes and two changes to the prompt or process that would help.
Show answer
Likely causes: the early details were rewritten many times and compressed away (drift), and the fixed 200-word limit forced the model to favour the newest chunks it had just read. Changes: add an instruction to keep key names, dates and decisions from the existing summary, and keep a separate append-only list of key facts per chunk. Other valid changes: use larger chunks so there are fewer rewrites, or raise the word limit.