Lesson 05 Context Windows & Long Documents
Theme:

Lab Guide

Three exercises: chunking, map-reduce summarization, and a cost/quality comparison against single-pass summarization.

📥 Lab Notebooks

⬇ lab.pdf ⬇ lab_answer.pdf

📥 Demo Files

⬇ sample_document.txt

Prerequisites

Exercise 1 — Text Chunking

Implement a function that splits a long text into overlapping word-count chunks. This is the foundation for all downstream processing exercises.

Task 1.1 — Implement chunk_text(text, chunk_size=500, overlap=50)

The function takes a string and returns a list of strings. Each chunk contains approximately chunk_size words. Adjacent chunks overlap by overlap words to prevent boundary artifacts.

Algorithm:

  1. Split text on whitespace to get a list of words.
  2. Starting at index 0, take words [i : i + chunk_size].
  3. Advance i by chunk_size - overlap (not by chunk_size).
  4. Continue until i >= len(words).
  5. Return the list of chunk strings (joined with spaces).

Checkpoint: For chunk_size=500, overlap=50, the document should produce 11–14 chunks. Verify: len(chunk_text(text)) >= 10.

Task 1.2 — Inspect chunks

After implementing, run the inspection cell that prints: total chunks, word count per chunk, and the last 50 words of chunk N overlapping with the first 50 words of chunk N+1. Confirm the overlap is working correctly.

Exercise 2 — Map-Reduce Summarization

Use your chunks from Exercise 1 to implement the map-reduce summarization pattern.

Task 2.1 — Implement map_reduce_summarize(chunks)

The function takes a list of chunk strings and returns a single summary string.

Required steps:

  1. Map phase: For each chunk, send a single API call asking Claude to summarize that chunk in 2–3 sentences. Collect the summaries in a list.
  2. Reduce phase: Combine all chunk summaries into a single prompt, asking Claude to synthesize them into a coherent 3–5 paragraph document summary.
Model choice: Use claude-haiku-4-5 for the map phase (cheaper, fast enough for simple chunk summarization) and claude-opus-4-5 for the reduce phase (better synthesis quality).

Task 2.2 — Count tokens used

After running map_reduce_summarize, the notebook's token counting cell estimates total tokens used: sum of (chunk token count + summary prompt overhead) for each map call, plus the reduce call input. The token counter uses len(text.split()) * 1.33 as a rough word-to-token estimate.

Exercise 3 — Single-Pass vs Map-Reduce Comparison

Compare two approaches to summarizing sample_document.txt: sending the full document in one call vs. the map-reduce approach from Exercise 2.

Task 3.1 — Run single_pass_summarize(full_text)

The notebook provides single_pass_summarize (already implemented). It sends the entire document to Claude in one call with a prompt asking for a 3–5 paragraph summary. Record the output and the token count.

Task 3.2 — Run map_reduce_summarize(chunks)

Run your implementation from Exercise 2 on the same document. Record the output and total token count.

Task 3.3 — Compare results

In the comparison markdown cell, answer these questions:

Checkpoint: You should have both summaries and both token counts recorded before writing your comparison.

Completion Checklist