Course · Codex · Sessions & context
Reclaim a full context window with /compact
The money-handling refactor has sprawled. To move every float to integer cents you’ve had Codex read the importer, the models, the reports endpoint, and a pile of tests - and an hour of file dumps and command output has piled into the window. Put a number on just the three files chapter 1 already sized: csv.py at 2,100 estimated tokens, models.py at 3,400, the test suite at 12,400. That’s 17,900 tokens spent on reads alone, before a single line of the actual back-and-forth is counted - about 9% of the window, gone to finding out where things are rather than to the fix itself. The agent is getting hazy: a moment ago it re-read a model it had already read, and it’s started hedging on decisions it was firm about earlier.
You’re in the worst spot for the two resets you already know. You can’t start a fresh session - the half-finished refactor is this thread, and abandoning it means re-establishing everything. And resuming a leaner session wouldn’t help, because the bloat isn’t in some other session, it’s right here. You’re not done, you’re not leaving, you’re just out of room. This is the exact situation compaction exists for: keep the understanding, spend far fewer tokens carrying it.
Condensing the thread
Section titled “Condensing the thread”/compact # summarise the conversation so far, keep the essentialsCompaction replaces the long back-and-forth with a summary of itself. The load-bearing parts tend to survive - the goal you’re working toward, the decisions you reached, the current state of the code, the most recent turns. The verbose middle tends to collapse: the full file contents Codex read an hour ago, exact command output, the approaches you tried and discarded.
One number is worth holding before you run it: if /compact is doing its job, whatever summary comes out the other end should be a small fraction of the 17,900 tokens those three reads alone cost - not thousands, something you could count on two hands’ worth of digits. Remember that shape; you’re about to check it against a real one instead of taking it on faith.
> /compact
⎿ Compacted. Conversation summarised. Kept: goal: float to integer cents across importer, models, and reports; cents convention locked; importer and models done, reports pending; last three turns preserved. Collapsed: full file dumps and command output from earlier in the thread.
> /status [the window has real room again]
> now convert the reports endpoint, the last float column [carries on with the refactor's context intact, the dead weight gone]Now pay off the number the way chapter 1 taught you to price prose: that “Kept” line is 162 characters. At 4 characters per token, that’s about 40 estimated tokens - carrying the goal, the convention, and the state of every file, for roughly 40. Against the 17,900 the three reads cost going in, that’s roughly 450 times smaller. Same understanding, a rounding error of the footprint. That’s not a guess about what compaction probably does - it’s the same arithmetic you’d run by hand, checked against what actually came back.
Run /status (the next lesson) right after and you’ll see the window drop back down - same thread, same plan, far less weight.
A note on judgment over mechanism. The official docs describe /compact as a command you run; whether Codex also condenses on its own as you near the limit isn’t something to count on, so don’t build a habit around it doing the work for you. Treat compaction as a deliberate move you make at a clean boundary - between finishing one file and starting the next - rather than something you hope fires at the right instant. You choosing the moment beats anything choosing it for you in the middle of a delicate step.
What compaction can’t do
Section titled “What compaction can’t do”That 450x number should feel almost too good, and the honest half comes immediately: it’s cheap because it’s lossy, not because nothing was lost. Compaction is a bet about what matters. It keeps what looks load-bearing and discards what looks like noise, and on a long refactor that bet is usually right, but “usually” is not “always” - and you don’t find out which turn you were on until later.
Watch the stakes before the rule. This is a session shaped like your refactor - window filling with file dumps and test output, the agent starting to re-read things - with its context window x-rayed beside the terminal. When it pauses, choose what happens next; the branch where you don’t choose is the one worth seeing. (The commands wear generic names here - in Codex, the condense move is /compact, and the fresh start is a new session.)
Here’s the rule to take from this lesson if you take nothing else: compaction is lossy, so get unsaved work onto disk first. If a change exists only in the conversation - Codex described the exact new body of to_cents() in detail but you never actually wrote it to a file - compaction can flatten “the precise implementation” down to “converted the amount to cents,” and the specifics are simply gone. They were never anywhere but the chat, and the chat just got summarised at a 450-to-1 ratio, which is exactly why they didn’t survive. Save, then compact: get the work into a file, a commit, or a note before you condense the thread holding it. Make that reflexive and compaction is free; skip it once on the wrong turn and you learn it the expensive way.
This is also where the chapter’s thesis comes home. The cents convention, the category list, the test command - the context you cannot afford to lose - shouldn’t live in a thread that a compaction might paraphrase away, no matter how good the odds usually are. It belongs on disk, in AGENTS.md or a commit, where no summariser can touch it. Compaction is for the transient understanding of a session; the durable kind gets written down. Closing the gap between what the agent should know and what you’ve actually told it is the whole game, and a thread that’s about to be compacted is precisely the wrong place to keep anything you’d hate to lose.
What you’ve reclaimed
Section titled “What you’ve reclaimed”The window has room again, the refactor’s context is intact, and the work that mattered is on disk where compaction can’t reach it. The thread is healthy.
But “the agent feels hazy” sent you to /compact on instinct this time, and instinct isn’t always right - sometimes the conversation isn’t even the biggest thing in the window. Before you reach for compaction reflexively, it’s worth being able to see what’s actually eating the budget, so you fix the real culprit instead of the assumed one. That’s a single command away: inspect what’s in the window.