Summarising Thousands
Map, then reduce.
The founder wants a two sentence summary of last month's forty thousand messages. They'd never fit in one prompt. And even if they could, you wouldn't want to pay for that. So you do what any manager would do. Split up the reading, then combine the reports.

- Forty thousand messages
- Too big for one prompt
- Split up, then combine
Map and reduce
We split the tickets into small batches. Each batch gets its own one sentence summary. That's the map step. Then a final call combines those notes into a two sentence report. That's reduce. Run it. Twenty four tickets, in three batches, became a clear report with the top issue first. With forty thousand tickets, you'd just add another layer, summarising the summaries. There's a danger, though. Rare but serious issues can vanish at each level. So add a rule at the map step, such as, always mention anything to do with safety.
from llm import ask
from tickets import TICKETS
texts = [t for t, _ in TICKETS]
batches = [texts[i:i + 8] for i in range(0, len(texts), 8)]
notes = [ask("Summarise these support tickets in one sentence:\n" + "\n".join(b)) for b in batches]
final = ask("Combine these notes into a two-sentence report for a manager, with the top issue first:\n"
+ "\n".join(notes))
print(len(batches), "batches")
print(final)3 batches The primary issue facing support is significant book quality and condition defects, specifically printing errors, failed bindings, and physical damage. Additionally, teams are managing frequent courier and delivery failures—such as lost or delayed packages—which are driving an influx of customer requests for refunds, returns, cancellations, and billing corrections.
Also uses tickets.py
TICKETS = [
("my order arrived late and the box was crushed", "delivery"),
("the parcel never came, where is my delivery", "delivery"),
("courier left the package in the rain", "delivery"),
("tracking has not moved for a week", "delivery"),
("shipping took three weeks, far too slow", "delivery"),
("delivery driver was rude and threw the parcel", "delivery"),
("package is lost, nobody knows where it went", "delivery"),
("the courier says delivered but nothing arrived", "delivery"),
("i want my money back for this book", "refund"),
("please refund the second copy you charged me for", "refund"),
("you took payment twice, return my money", "refund"),
("how long does a refund take to reach my card", "refund"),
("cancel my order and give me a refund", "refund"),
("i returned the book but got no money back", "refund"),
("charged the wrong price, want the difference back", "refund"),
("refund has not appeared on my statement", "refund"),
("the book has missing pages in chapter four", "quality"),
("cover is torn and the spine is broken", "quality"),
("pages are printed upside down in the middle", "quality"),
("ink is smudged on every page", "quality"),
("the binding fell apart after one read", "quality"),
("this copy has a coffee stain on the cover", "quality"),
("wrong edition, and the print quality is awful", "quality"),
("several pages are stuck together badly", "quality"),
]
QUERIES = [
("where is my parcel, it has not arrived", "delivery"),
("i need my money back please", "refund"),
("the pages are torn and the cover is bent", "quality"),
("courier lost my package", "delivery"),
("refund the payment to my card", "refund"),
("print is smudged and hard to read", "quality"),
]Also uses llm.py
from google import genai
from google.genai import types
client = genai.Client()
def ask(prompt, system=None, schema=None, temperature=0.1):
config = types.GenerateContentConfig(temperature=temperature, system_instruction=system)
if schema:
config.response_mime_type = "application/json"
config.response_schema = schema
r = client.models.generate_content(model="gemini-flash-latest", config=config, contents=prompt)
return r.parsed if schema else r.textSummary
Divide the reading, and combine the notes. Next, how to spot a new problem as soon as it starts.
Key takeaways- Summarise in batches
- Combine the summaries
- Protect rare, serious issues