Sub topic 5
Summarising in a Single Pass
A single-pass summary sends the whole document to the model in one call and gets one summary back. It is the simplest strategy, and it should be your default whenever the document fits. This chapter covers how to decide that it fits, how to write the call, what can go wrong, and when to move to chunking.
Prerequisites
This chapter assumes you can write basic Python and have sent prompts to an LLM through an API. It also assumes you know what a token is and that every model has a context window, a maximum number of tokens for input plus output. The earlier chapters on goals, tokens and prompt design cover the details. Here we put them together.
Why start with one call
A single pass has real advantages over the multi-call strategies in later chapters.
- The model sees everything at once. It can connect a claim on page 2 to a caveat on page 40. Map-reduce and refine both lose some of this.
- Fewer moving parts. There is no chunking code, no merge step and no ordering logic to get wrong.
- Lower latency. One call is usually faster than a chain of calls, and cheaper than a pipeline that re-reads the text several times.
- Easier to debug. If the output is bad, the cause is the prompt or the model, not an interaction between stages.
The cost is a hard ceiling. If the document plus the prompt plus the summary does not fit in the context window, the call fails or the input is truncated.
The fit check
Before calling the model, compute a token budget. Three quantities compete for the context window:
- The prompt instructions (usually a few hundred tokens at most).
- The document.
- The room you reserve for the summary (the output).
The condition is:
instruction_tokens + document_tokens + output_reserve <= context_window
Leave a safety margin of 5 to 10 percent. Token counts from a different tokenizer than the model's own can be off by a noticeable amount, and the chat format adds a few tokens of overhead per message.
def fits_in_context(document, instructions, count_tokens,
context_window, output_reserve=1000, margin=0.9):
used = count_tokens(instructions) + count_tokens(document) + output_reserve
return used <= context_window * marginHere count_tokens is the same function you pass to the chunking code from the chunking chapter. For a rough estimate without a tokenizer, English text averages about 0.75 words per token, so a 30,000-word report is around 40,000 tokens. Treat that only as a first guess. Code, tables, and non-English text often use more tokens per word.
The single-pass function
The following function does the fit check and then makes one call. It raises an error instead of silently truncating, because a summary of a truncated document looks fine and is quietly wrong.
def summarise_single_pass(text, call_llm, count_tokens,
context_window=128000, output_reserve=1000):
instructions = (
'Summarise the document below in one paragraph of at most 150 words. '
'Use only information in the document. '
'If the document does not say something, do not add it.\n\n'
'Document:\n'
)
if not fits_in_context(text, instructions, count_tokens,
context_window, output_reserve):
raise ValueError('Document does not fit in the context window; '
'use chunking instead')
return call_llm(instructions + text)Note the design choices:
- The instructions come before the document. That is a convention that works well, but see the next section on placement for long inputs.
- The word limit is explicit. "Short" means different things to different readers, and models tend to drift longer without a number.
- The "use only information in the document" line is a cheap first defence against invented facts. It reduces but does not eliminate them.
Where to put things in a long prompt
With long inputs, the position of the instruction can change the result. Two common layouts:
| Layout | Strength | Weakness |
|---|---|---|
| Instruction, then document | The model reads the task before the text and knows what to look for. | By the end of a long document, the task may be far back in the prompt. |
| Document, then instruction | The task is the last thing the model sees. | The model reads the document without knowing the goal. |
A practical compromise is to state the task before the document and repeat the key constraints (length, audience, format) briefly after it. Test both on your own documents. The better layout varies between models.
Also mark where the document starts and ends with clear delimiters, such as <document> and </document> tags. Delimiters stop the model from confusing text inside the document with your instructions. This matters when the document itself contains phrases like "ignore the above", which is data to summarise, not a command.
def build_prompt(text, max_words=150):
return (
'Summarise the document between the tags in at most '
+ str(max_words) + ' words. Use only information in the document.\n\n'
'<document>\n' + text + '\n</document>\n\n'
'Reminder: at most ' + str(max_words) + ' words, '
'only information from the document.'
)Fitting is not the same as working well
A document that fits can still produce a poor summary. A larger window does not mean the model attends equally to all of it. Models often use information near the start and end of a long input more reliably than information in the middle, an effect called "lost in the middle". The next chapter covers it in detail. For now, know the symptoms in a single-pass summary:
- The summary covers the opening and closing sections well and skips the middle.
- Specific numbers, names or dates from the middle are missing or blurred.
- The summary is generic and could describe many documents of that type.
Quality tends to fall as input grows, even well below the stated limit. If your documents are routinely near the limit, test on a sample before trusting single pass.
Controlling the output
A single pass gives you one chance, so make the prompt specific. Useful controls:
- Length: a word, sentence or bullet limit.
- Audience: "for a finance manager who has not read the report".
- Structure: for example, "three sections: decisions, risks, open questions".
- Scope: what to include or ignore, such as "ignore the appendix".
- Grounding: "if the document does not state X, write 'not stated'".
The last one is valuable. Without it, a model asked for "the deadline" in a document with no deadline may produce a plausible one.
def structured_summary(text, call_llm):
prompt = (
'Read the document and produce exactly these three sections.\n\n'
'Decisions: bullet list of decisions made.\n'
'Risks: bullet list of risks mentioned.\n'
'Open questions: bullet list of unresolved questions.\n\n'
'Rules: use only information in the document. '
'If a section has no content, write "None stated". '
'Keep each bullet under 20 words.\n\n'
'<document>\n' + text + '\n</document>'
)
return call_llm(prompt)Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| API error about exceeding the context length | No fit check, or the output reserve was ignored | Run the fit check and subtract the reserve |
| Summary ends mid-sentence | The output token limit was too low | Raise the output limit and ask for a shorter summary |
| Summary ignores the middle of the document | Position bias in long inputs | Test shorter inputs, ask for coverage of each section, or use chunking |
| Summary contains facts not in the source | The model filled gaps from general knowledge | Add grounding rules and check claims (see the faithfulness chapter) |
| Summary is longer than requested | No explicit length limit | State a number of words or bullets |
| Different runs give different summaries | Sampling randomness | Lower the temperature setting if your API has one |
A subtle error is silent truncation. Some tools and wrappers cut the input at a limit without warning. If a summary never mentions the last third of a long document, check how many tokens were actually sent.
When to leave single pass
Move to a chunked strategy when any of these is true:
- The fit check fails.
- The document fits but the summary quality drops on your tests, especially for the middle sections.
- You must summarise many documents and cost matters, since long prompts are billed per input token. A fixed per-token price makes cost grow roughly linearly with length, but latency and failure rates can grow faster.
- You need to trace each point to a section of the source. Chunk-level summaries make that easier.
The decision flow looks like this.
Practical checks after the call
Even for one call, add a few cheap checks before you accept the output.
- Confirm the output length is within your limit.
- Check that the finish reason from the API is a normal stop, not a length cutoff.
- Spot-check numbers and names in the summary by searching for them in the source.
- For important documents, ask the model to list the section headings it covered and compare them with the real ones.
import re
def numbers_not_in_source(summary, source):
found = set(re.findall(r'\d+(?:[.,]\d+)?', summary))
return sorted(n for n in found if n not in source)The function returns numbers that appear in the summary but nowhere in the source text. It is a crude check. It can flag legitimate cases, such as a total the model computed or "three" written as "3", but an empty result is reassuring and a non-empty one tells you where to look first.
Summary of the approach
- Define purpose, audience and length first.
- Count tokens and run the fit check with an output reserve and margin.
- Write a prompt with explicit length, grounding rules and delimiters.
- Make one call and check the output.
- If the fit check fails or quality is poor on your samples, switch to a chunked strategy.
Exercise
A model has a 32,000-token context window. Your instructions use 300 tokens, you reserve 1,500 tokens for the summary, and you want a 10 percent safety margin on the window. What is the largest document, in tokens, that you can send in a single pass using the check in this chapter? Give the number, and explain one reason you might still choose chunking for a document of that size.
Show answer
The usable window is 32,000 x 0.9 = 28,800 tokens. Subtract 300 for instructions and 1,500 for the output reserve: 28,800 - 300 - 1,500 = 27,000 tokens. Even at that size, chunking may be better because quality in long inputs can fall before the limit is reached, for example when the middle of the document is under-represented in the summary. Needing to trace points back to specific sections is another reason.
Exercise
A single-pass summary of a 60-page report describes the introduction and conclusion accurately but says nothing about the methods section in the middle. The document fits in the context window. Name the likely cause, and give two changes you could try.
Show answer
The likely cause is position bias in long inputs, often called lost in the middle. Possible changes: ask explicitly for coverage of each named section and list the headings; use a structured output format with one entry per section; shorten the input by removing irrelevant parts; or switch to map-reduce so each section is summarised on its own.