Context Is Managed, Not Infinite
For a long time I wanted a bigger window. The wish was wrong. A window is a budget with a hard edge, and Claude Code defends it early. It drops duplicate reads, defers rare tools, and folds the rest into one note.
Clearing room without losing your place is the skill. The tidy is the product.
Context budget
The agent works at a small desk. It holds a few papers at a time. Every file it reads and every message it gets takes a spot.
Fill the desk and the agent has no room to think. So it tidies: keep the papers that matter now, fold the rest away where it can fetch them later.
Most of the craft is choosing what stays on the desk and what gets put away.
That desk is the context window. Build as if it has no edge, and the session gets slow and expensive. Claude Code counts what sits in the window and acts at the trim line, short of the ceiling. Watch the budget fill, cross that line, and get pulled back.
01Context starts before the prompt
The window is not empty when you start. Before you type, it already holds the branch, what changed, recent history, the project notes, and the date. None of it is free. So the agent keeps each entry short and truncates the long ones. Even a useful line earns its slot or leaves.
02What it has read is part of the state
Duplicates arrive on their own. A follow-up opens the file again, and a subagent opens the one next to it. Without a record of the reads, the same file lands in the window over and over. The agent carries that record: the file, the range, and the time it last changed. The tool idea in this guide gave it in full.
The record pays a second time as a safety rule. The agent refuses to edit a file it did not read first. It refuses again if the file changed after that read. So it never writes over something it did not look at. One record, a smaller window and safer edits.
03Counting tokens is harder than it looks
Tidying at the right moment needs an honest count of how full the window is. The easy mistake is to count the reply alone. The window holds far more than the reply.
So the agent counts the whole window: what went in, what sat in cache, and what came back. It walks the transcript piece by piece. It runs several calls at once, and one answer returns in parts that belong together. The count has to match the real pile, lumpy and uneven as it is.
04The first valve is local
Not every full window needs the big move, and the first valve I reach for is local. Microcompaction drops the bulkiest scraps: old file reads, old search hits, old command output. Rewriting the whole window this early loses more than it frees. Reach for the local valve while the conversation itself still reads clean and only the tool output is heavy. Spend the cheap move first, and let the expensive one wait until it earns its cost.
05The big valve crosses a boundary
When the local valve is not enough, auto-compact folds the whole window into one note. Four numbers hold that fold in check. It reserves output room for the note. It treats a fraction of the window as the working edge. It fires the fold well below that edge. And it counts failures, so a note that fails a few times in a row trips a breaker.
Three of those numbers draw lines on the same bar. The trigger sits under the working edge, and the working edge sits under the ceiling. The gap between them is the room the note needs.
06The rebuild is the point, not the summary
After the fold, the agent rebuilds the desk in one fixed order, every time. The order runs marker, then the note, then the turns worth keeping. Then it puts back the recent files, the live plan, and the tools still in reach.
Images come off before the fold. A picture is heavy, and it does not compress into a written note. The fold drops them and keeps what can be written down.
The distinction that matters
A fold has one job: leave the agent able to take the next step. A summary carries the meaning. The put-back step carries the state. Judge any agent that folds by one test: the next tool call lands without the user repeating anything.
07Memory is background work with gates, not magic
The agent keeps one memory file that outlives the session. What it writes there shows up in the next session too. A subagent writes it, and its write access covers that file alone. It waits until the conversation passes a size threshold, then waits again between notes. Writing is still writing, so it goes through the same permission door as any edit.
A deeper pass runs behind a clock and a lock. It reads back over old sessions, consolidates what it learned, and rolls itself back cleanly when it slips. A memory file without those limits fills with invention.
08Even the tool list is budgeted
The tool list costs room too, because every tool ships a description and descriptions take slots. So rare tools stay deferred until the agent goes looking for one, and the prompt stays small and steady. Finding a tool on demand costs one call. Carrying every description costs every turn. The system prompt idea and the tool idea already paid this trade, and read dedupe pays it again.
The cost
Every one of these moves can go wrong. A fold that keeps failing loops, so a counted breaker stops it after a fixed number of tries. A cut in the wrong place splits a request from its result, so the fold moves whole pairs. That pairing rule comes from the first idea in this guide, and the fold does not get to break it.
Decision
Judge an agent by what it does with a full window. Two things have to be true. It clears room without dropping the work in flight. You can name the move it made: read dedupe, microcompaction, deferred tools, or auto-compact. A bigger window moves the edge further out, and the work stays the same. The next idea is what this tidying costs, and why the order of the parts sets the bill.