Skip to content
15 min read45 min build
Prerequisites:ch01-minimal-coding-loopch02-tool-registrych03-planner-task-graph
What You Will Build

Context management with truncation, summarization, and checkpoints for long-running agent sessions

Why This Matters

Every message your agent sends and receives is stored in the messages array and re-sent to the LLM on the next turn. After a few tasks, this array contains hundreds of messages — tool calls, file contents, error logs, planning output. The LLM's context window is finite. When messages overflow the context window, the API rejects the request, or worse, the model silently loses track of critical earlier context.

This is not a theoretical problem. Any agent that runs more than a few turns will hit this wall. Context management is what separates a demo agent from one that can handle real work.

What You Will Build

A context management system with three strategies: truncation (simple but lossy), summarization (the best default), and checkpoint saving for resume and rollback. By the end, your agent can compact old messages into summaries while keeping recent context intact.

Story Mode for this chapter is coming soon

We are crafting a fun, code-free explanation with metaphors and interactive mini-games. In the meantime, switch to Builder Mode to start learning.

What You Can Do Next

You now have the four building blocks of a core coding agent: a loop (Chapter 1), tools (Chapter 2), a planner (Chapter 3), and memory management (Chapter 4). This agent can accept complex coding requests, plan them, execute them with tools, and manage its own context over long sessions.

But this agent runs in your working directory. It writes files alongside your code. If it makes a mistake, you have to manually undo the changes. What if it corrupts a file? What if two agents are working on different tasks in the same directory?

Part II: Execution Lanes starts with Chapter 5: Safe File Editing and Patch Application, where you will learn to make changes safely using structured diffs instead of overwriting files.