Programming Fundamentals · Recursion and Divide-and-Conquer

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.”

Compress it yourself · a 3600-token renovation chat

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.

Current context3600token
Split → summarize each → merge, three acts in a row
Try turning on “Keep the latest 4” and compress again. That’s what real Agents do: the latest turns are usually what you’re doing right now—too precious to touch—so only the older parts get summarized. You’ll see tokens don’t drop all the way to 200—fidelity and space are always a trade.
Concept card · the same playbook as merge sort

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:

Split

✂️ 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.

Conquer

🔧 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

🧩 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.

Why not ask the AI to summarize the whole thing in one shot? Because “summarize ten thousand words” would itself blow the context—exactly the problem we’re solving. Divide and conquer’s cleverness: turn an “unsolvable big problem” into a pile of “definitely solvable small ones”, then spend a little effort assembling. In the Engineering Advanced Part, Coding Agents read large codebases the same way: file by file, note the key points, then roll up.
Honest card · the cost of compression

⚠️ Summaries are lossy compression—details get lost

Original: “budget under 80k” “I really hate red—no red anywhere in the house
After: “discussed budget and color preferences”← the exact number and the red ban are gone
After compression the AI still knows you “talked about budget,” but ask “what’s my budget?” and it can only guess; next time it picks a red sofa and you’ve got no ground to stand on. That’s why the AI Practicum keeps stressing: critical facts (numbers, hard constraints, decisions) must live in long-term memory or a doc—don’t trust the summary to keep them. Summaries cover “what we roughly talked about”; the archive holds what’s set in stone.

What this lesson wants to share