Lab Guide
Three exercises: chunking, map-reduce summarization, and a cost/quality comparison against single-pass summarization.
📥 Demo Files
⬇ sample_document.txtPrerequisites
- Completed Theory reading
ANTHROPIC_API_KEYin.env- Packages:
anthropic,python-dotenv - The file
demo/sample_document.txt— a ~5,000-word article on AI history - Open
lab.pdfin Jupyter
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:
- Split
texton whitespace to get a list of words. - Starting at index 0, take words
[i : i + chunk_size]. - Advance
ibychunk_size - overlap(not bychunk_size). - Continue until
i >= len(words). - 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:
- 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.
- Reduce phase: Combine all chunk summaries into a single prompt, asking Claude to synthesize them into a coherent 3–5 paragraph document summary.
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:
- Which summary is higher quality? (More accurate? More comprehensive? Better organized?)
- Which approach uses more total tokens?
- For
sample_document.txt(~5,000 words), which approach is more cost-effective? - At what approximate document length would the two approaches have equal cost?
Checkpoint: You should have both summaries and both token counts recorded before writing your comparison.
Completion Checklist
chunk_textimplemented and produces 10+ chunks from the sample document- Overlap verified by inspecting chunk boundaries
map_reduce_summarizeimplemented with separate map and reduce phases- Total token count recorded for map-reduce
- Single-pass summary and token count recorded
- Written comparison answers all 4 questions