Divide and Conquer: The Algorithm Behind Context Compression
In the AI Practicum you learned: when a chat is too long to fit, the AI runs Compaction—compressing old turns into a summary. We said it was “like packing clutter before a move.” This lesson peels back the wrapping: it’s a two-thousand-year-old algorithm pattern called divide and conquer. Last lesson’s recursion was “split”; this one adds the other half: “split, then merge.”
Below is a full chat where you and an AI discuss a renovation plan—12 messages, about 3600 tokens, about to blow the context window. Hit “Start compressing,” and watch the three acts: first split (into three segments), then conquer (each segment becomes one summary), finally merge (three summaries into one)—the token counter up top shows how much you saved.
Those three acts have a formal name: divide and conquer (Divide and Conquer). Its textbook celebrity is merge sort—split a list into small pieces, sort each, then merge pairwise. Merge sort sorts numbers; Compaction compresses talk—same skeleton:
✂️ Cut into pieces
If the big problem won’t solve directly, cut it into smaller pieces. Merge sort: halve a list until you have singles. Compaction: split a long chat by topic or turn into segments.
🔧 Solve each piece
Small pieces are easy. Merge sort: sort each short run. Compaction: summarize each segment—short enough for the AI to read and summarize accurately in one go. This step can even run in parallel, so it’s faster.
🧩 Merge the results
Assemble the piece results into a full answer. Merge sort: fuse two sorted runs into one. Compaction: merge several summaries into one master summary. Still too many segments? Recurse for another round—last lesson’s flavor is back.
⚠️ Summaries are lossy compression—details get lost
What this lesson wants to share
- Divide and conquer = split small, solve each, then merge: an unsolvable big problem becomes a pile of solvable small ones
- Compaction is divide and conquer’s real face in AI: segment → summarize each → merge—same skeleton as merge sort
- Recent turns are the most precious: real Agents only compress the old and keep the new—fidelity and space are always trading
- Compression is lossy: facts like “under 80k” and “no red” need a separate store—don’t count on the summary
- Too many segments? Recurse and compress again: last lesson’s recursion + this lesson’s merge = full divide and conquer