Sub topic 2
Summarising in a Single Pass
Prerequisites: basic Python, experience sending prompts to an LLM, and familiarity with tokens. A single-pass summary sends the whole document and one instruction in one model call. It is the simplest strategy when the input and requested output fit the model's context window.
Summarising in a Single Pass
A context window is the maximum number of input and output tokens a model can handle together. Your prompt should state the purpose, audience, length, and constraints of the summary.
prompt = """Summarise the document for a project manager.
Write 150 words or fewer.
Include the main decision, supporting evidence, and unresolved risks.
Use only information from the document.
DOCUMENT:
""" + document
summary = call_llm(prompt)This approach has low coordination overhead and preserves relationships between sections. It works well for short reports and documents whose key ideas are spread across the text.
Why Long Inputs Degrade Quality
A document can fit technically and still produce a weak summary. Long inputs increase cost and latency. They also give the model more competing details. Important information near the middle may receive less attention, a problem often called lost in the middle. The model may omit evidence, overemphasise the beginning and end, or combine unrelated claims.
A context window limit is therefore a boundary, not a quality guarantee.
> callout warning > Do not solve a long-input problem by silently truncating the document. You may remove the section that contains the main qualification or conclusion.
Splitting a Document into Chunks
Chunking divides a document into smaller parts. Split at paragraph, section, or speaker boundaries rather than cutting through sentences. Keep a small overlap when a topic continues across boundaries. Each chunk must leave room for the prompt and generated summary.
def chunk_paragraphs(text, max_tokens, count_tokens):
chunks, current, size = [], [], 0
for paragraph in text.split("\n\n"):
n = count_tokens(paragraph)
if current and size + n > max_tokens:
chunks.append("\n\n".join(current))
current, size = [], 0
current.append(paragraph)
size += n
if current:
chunks.append("\n\n".join(current))
return chunksA paragraph larger than the limit remains oversized, so production code should also split such paragraphs or reject them explicitly.
Map-Reduce: Summarise the Parts, Then Combine
Map-reduce applies one operation to each chunk, then combines the results. The map step creates partial summaries. The reduce step summarises those partial summaries. Map calls can run in parallel, but the reduce step can lose details already omitted by the map step.
Exercise
When should you prefer map-reduce over a single pass?
Show answer
Use map-reduce when the document does not fit comfortably in one context window or when long-input quality is unreliable.