Sub topic 5
Summarising in a Single Pass
This chapter assumes you can write basic Python, have sent prompts to an LLM through an API, and know that models read text as tokens. Single-pass summarisation means sending the whole document to the model in one call and getting one summary back. It is the simplest strategy, and you should try it first.
When a single pass works
A single pass works when the document, the prompt, and the room left for the reply all fit inside the model's context window. The context window is the maximum number of tokens the model can handle in one call, counting input and output together.
The budget check looks like this:
def fits_in_one_pass(text, count_tokens, context_window, prompt_tokens=300, reply_tokens=800, safety=0.9):
available = context_window * safety - prompt_tokens - reply_tokens
return count_tokens(text) <= availableThe safety factor leaves margin because token counts from a different tokenizer than the model's can be off by several percent. Use the tokenizer that matches your model when you can.
A minimal implementation
The prompt should state the purpose, audience, length, and a rule against outside facts. Chapter 4 covers prompt design in detail. This version shows only the shape of the call.
def summarise_single_pass(text, call_llm, count_tokens, context_window):
if not fits_in_one_pass(text, count_tokens, context_window):
raise ValueError('Document is too long for a single pass')
prompt = (
'Summarise the following document for a busy manager. '
'Write one paragraph of at most 120 words. '
'Use only information in the document.\n\n'
'DOCUMENT:\n' + text
)
return call_llm(prompt)Raising an error is deliberate. Many APIs reject oversized input, but some truncate it without warning, and then you get a summary of only the first part of the document. Failing loudly is safer.
Strengths and costs
| Aspect | Single pass |
|---|---|
| Calls | One |
| Context across the document | Whole document visible at once |
| Implementation effort | Minimal |
| Latency | One call, but a long input slows it down |
| Main risk | Quality drops on long inputs |
The main advantage is that the model sees every part of the document together. It can connect a claim on page 2 with a correction on page 40. Chunking strategies cannot always do this, because each call sees only part of the text.
The main risk is that a document that technically fits is not always summarised well. Models tend to use the start and end of a long input more reliably than the middle, so details buried in the middle may be dropped. The next chapters on long-context pitfalls cover this in more depth.
Common errors
- Forgetting the reply budget. If the input fills the window, the model has no room to answer, and the output is cut off or the call fails.
- Counting characters instead of tokens. Characters per token vary by language and content. Code and non-English text often use more tokens per word.
- Not fixing the length. Without a limit, summary length changes from run to run, which makes outputs hard to compare.
- Accepting a missing-source answer. If the input was truncated, the summary will look fine but cover only part of the document. The error check above prevents this.
Practical advice
Start with a single pass on a sample of your real documents. Read the summaries next to the sources. If the quality is good and the cost is acceptable, stop there. Move to chunking only when documents exceed the window or when you see the model skipping content in the middle.
Exercise
A model has a 16,000-token context window. Your prompt uses 400 tokens, you want a reply of up to 600 tokens, and your document is 14,800 tokens. Using a 0.9 safety factor as in fitsinone_pass, does it fit in one pass? Show the calculation.
Show answer
Available = 16,000 × 0.9 − 400 − 600 = 14,400 − 1,000 = 13,400 tokens. The document is 14,800 tokens, which is more than 13,400, so it does not fit. You need to chunk the document or use a model with a larger window.
Exercise
Why does summarisesinglepass raise an error for an oversized document instead of passing the text to the API anyway?
Show answer
Some APIs truncate oversized input silently. The model would then summarise only the part it received, and the summary would look complete while missing the rest of the document. Raising an error makes the problem visible.