Long Documents
Summarising in parts.
Some documents are too long to send in one prompt, like a three hundred page manual. Even when they fit, they're slow and expensive. One answer is to summarise in parts, building up a summary as we go.

- Too long for one prompt
- Slow and costly
- Summarise in parts
A running summary
We join all the policy files into one handbook, and split it into parts. Then we loop. Each time, the model gets the summary so far, and the next part, and writes an updated summary. Run it. The summary is updated once per part, and the final version covers the whole handbook in a few sentences.
from pathlib import Path
from langchain.chat_models import init_chat_model
from langchain_text_splitters import RecursiveCharacterTextSplitter
handbook = "\n".join(p.read_text() for p in sorted(Path("kb").glob("*.md")))
parts = RecursiveCharacterTextSplitter(chunk_size=600, chunk_overlap=0).split_text(handbook)
model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
summary = ""
for i, part in enumerate(parts, 1):
summary = model.invoke(
f"Summary so far:\n{summary}\n\nNew text:\n{part}\n\n"
"Write an updated summary covering both, in at most three short sentences.").text
print(f"after part {i} of {len(parts)}: {len(summary.split())} words")
print()
print(summary)after part 1 of 4: 49 words after part 2 of 4: 69 words after part 3 of 4: 56 words after part 4 of 4: 48 words Riverside Books operates a physical shop in Bath alongside an online store with flexible returns, gift cards, a loyalty club, and various shipping options. The shop is open daily, selling new and second-hand books, hosting a Saturday children's story time, and offering customer support via email and phone.
Also uses kb/shipping.md
# Shipping
Standard delivery in the UK takes 3 to 5 working days and costs £2.99.
Delivery is free on orders over £25.
Express delivery arrives the next working day if you order before 2pm, and costs £5.99.
We ship to Ireland and France in 5 to 8 working days for £7.50.
Every order gets a tracking link by email once it leaves our warehouse.Also uses kb/returns.md
# Returns and refunds
You can return any book within 30 days of delivery for a full refund.
Books must be unread and in the condition you received them.
Damaged or faulty books can be returned at any time, and we pay the return postage.
Refunds go back to the original payment method within 5 working days of us receiving the book.
To start a return, reply to your order confirmation email.Also uses kb/gift_cards.md
# Gift cards
Riverside Books gift cards come in values from £10 to £100.
Gift cards never expire and can be used online or in our shop.
A gift card can be used across several orders until the balance is spent.
Gift cards cannot be exchanged for cash.Also uses kb/loyalty.md
# Riverside Readers loyalty club
Joining the Riverside Readers club is free.
Members earn one point for every pound they spend.
100 points can be swapped for a £5 voucher.
Members get early access to signed editions and a birthday discount of 10 percent.Also uses kb/shop.md
# About the shop
Riverside Books has one physical shop, at 12 Mill Lane, Bath.
The shop is open from 9am to 6pm Monday to Saturday, and 11am to 4pm on Sunday.
We sell new and second-hand books, and run a children's story time every Saturday at 10am.
You can contact us at hello@riverside-books.example or on 01225 000 000.Running summaries
This approach is simple, and keeps each prompt small. But it's slow, because each step waits for the one before. And details from early parts can fade, as the summary is rewritten again and again. The next lesson shows a faster approach.
| key | value |
|---|---|
| Simple | one loop |
| Small prompts | one part at a time |
| Slow | steps wait in turn |
| Fading | early details |
Summary
A running summary updates itself, part by part, so any length of document can be summarised. It's simple, but slow. Next, a faster way, map reduce.
Key takeaways- Split the document
- Update the summary
- One part at a time