New Tool Radar

AI Concepts

How to Fit a Long Document into a Context Window by Summarizing It First

Fit a long document into a context window by summarizing it first and sending the summary with your question.

Schematic illustration of fit a long document into a context window by summarizing it first: step one measure the document, step two summarize in parts, step three ask from the summary.

When a document is longer than the context window, you cannot send it whole: the runtime cuts the prompt before the model reads it, and nothing in the response tells you it happened. The fix is to summarize the document first, in parts that do fit, and then ask your question from the summary. You lose detail, but you gain an answer that is based on the whole document.

Prerequisites

  • The document’s token count and your context window size.
  • A way to split the document into parts that fit.
  • A clear question to answer from the summary.

Steps

Step 1: Measure the document

Count the document’s tokens and compare them with the window. A 9,600-token document does not fit an 8,192-token window, so it needs summarizing, not trimming by a few lines.

Step 2: Summarize in parts

Split the document into parts that fit the window, summarize each part on its own, and keep the part summaries. Then combine the part summaries into one short summary, keeping the facts your question needs.

Step 3: Ask from the summary

Send the combined summary with your question, and say in the prompt that the answer must come from the summary. Check the answer against the summary, not against the full document, since the model never saw the full text.

Worked example: Northgate Clinic 9,600-token policy file

The fictional Northgate Clinic has a 9,600-token policy file and an 8,192-token window. The question is the notice period for cancelling an appointment.

Step Tokens sent Fits 8192 Result
Send the whole file 9600 No Cut off, no warning
Summarize part 1 4800 Yes Summary of part 1
Summarize part 2 4800 Yes Summary of part 2
Ask from the combined summary 1200 plus question Yes Answer: 24 hours

The combined summary is 1,200 tokens, the question adds 200, and the answer, 24 hours, comes from the summary and matches the policy.

Expected result. The question is answered from a summary that fits the window, and the summary covers the whole document. UNTESTED: this guide was not run against a live model or API. The worked example below is fictional practice material, and its result is the expected result, not a measured one.

Limits

  • A summary loses detail; a question about a detail that was summarized away cannot be answered from it.
  • Summarizing in parts can drop connections between parts; read the combined summary before asking.
  • This guide does not cover extending the window itself.

Sources

See all