Back to Explore
GuideVibeFix editorialUpdated Nov 20, 2026

Don't wait for context rot: managing the context window while vibe coding

The context window is a bill, not a mystery: 7,850 tokens at startup, 22,225 at peak, compacted into a 1,725-token summary. This guide follows Claude Code's official docs through /context, /compact, /rewind, /clear, /autocompact, and subagent delegation, with a context ledger and five anti-patterns.

Hands typing on a laptop at night, the screen showing a dark code editor

First, the boundary of this guide. The site already has several guides about "making agents remember," but none of them is about this one. The Agent Memory guide is about memory across sessions: in-session, cross-session, and project memory. The AGENTS.md guide is about writing rules into a file so every new session reads them on arrival. The Parallel Agents Workflow is about one person directing several agents: how to split the work and merge it back. This guide is about something more basic, and easier to overlook, than all of those: inside a single session, how the context window gets eaten away bit by bit, and what to do once it is. Memory solves "remember it next time"; this guide solves "don't let it rot this time." There is also a news item on the site about Claude Code's idle compaction feature — that one is about the feature itself. This is a hands-on guide: the exact commands to type and the order to do them in while you code every day.

The "Money Bag" app below is a fictional practice project: a personal ledger app one person built with AI help. It records spending and produces monthly reports. Every operation, command, and number shown with it is invented to explain the method, not a real record. All the rules, commands, and defaults about Claude Code's context window come from Anthropic's official documentation, listed at the end. Other coding tools have their own versions of "start a fresh session" and "compress this," and the thinking carries over, but the command names and defaults differ — this guide follows Claude Code's official docs only, so check your own tool's docs before copying anything across.

What actually lives in the context window

The context window is all the text the model can see in one go, measured in tokens. Everything you say to Claude Code in the terminal, every file it reads, every reply it writes, every command output — all of it sits in this window. The window is not infinite. When it fills up, Claude Code does not stop and wait for you; it automatically compresses the conversation into a summary to make room and keep working. Compaction saves the day, but a summary always loses something — and what it loses might be exactly the agreement you care about most. So the first step is understanding where everything in the window comes from.

The official docs work through one example session with very concrete numbers. Before you have typed anything, the window already holds quite a lot: a system prompt of about 4,200 tokens, auto memory of about 680, environment info of about 280, MCP tool names of about 120, skill descriptions of about 450, your personal CLAUDE.md of about 320, and the project-root CLAUDE.md of about 1,800. That adds up to about 7,850 tokens before any work has started — nearly one twentieth of a 200K-token window, spent on startup. This is a fixed startup cost: every new session loads it again. You cannot dodge it; you can only keep it small.

The big spending starts once work begins. In that same example session, reading four files costs 2,400, 1,100, 1,800, and 1,600 tokens; two path-scoped rules cost 380 and 290; one search costs 600; the model's own analysis 800; two file edits 400 and 600; two hook outputs 120 and 100; one test run 1,200; and the final summary of changes 400. Over that round of work, the window grows from 7,850 to 22,225 tokens. The pattern is obvious: the more files you have it read and the longer the command outputs, the faster the window grows. Every large file you ask it to read and every long log you paste in is real tokens.

So stop treating "the context is full" as a mystery. It is a bill: a fixed startup charge, plus the running tab of file reads and command outputs. Once you can read the bill, every trick that follows has a reason behind it. The official docs also tally the bill after compaction: before compacting, the window holds 22,225 tokens; after, 17,095 — saving 5,130 tokens at the price of a 1,725-token summary. After compaction the window holds the 7,850 tokens of startup content in full, the 1,725-token conversation summary, the four re-read files at 6,900 tokens, and one 620-token skill body. 7,850 plus 1,725 plus 6,900 plus 620 is exactly 17,095 — the numbers balance. They also make one thing clear: compaction does not empty the window. It swaps twenty-two thousand tokens of back-and-forth for a seventeen-hundred-token summary, and what you save depends entirely on how good that summary is.

The context ledger of the example session: 7,850 tokens loaded at startup, growing to 22,225 tokens while working, back to 17,095 tokens after compaction, of which the conversation summary is only 1,725 tokens

Look first: /context is your dashboard

When a session starts to feel off, do not act first — look at the dashboard first. Type /context at the prompt and Claude Code draws a colored usage grid: how full the window is right now, what is taking up the most space, and suggestions for what to trim, including which tools are context-heavy, whether your memory files are bloated, and any capacity warnings. To edit what is in those memory files, run /memory.

Build one habit: halfway through a task, the moment the model starts to feel "a bit off," run /context first and then decide whether to compact, clear, or carry on. Most people do it backwards: they argue with the model for two rounds, get nowhere, and only then think to look at the window. Those two rounds of arguing burn context too — the more you argue, the more expensive and the more confused it gets.

Still on the same task: /compact, and say what to keep

When the task is not finished but the conversation has grown long, use /compact. It replaces the conversation so far with a structured summary. The session continues, just shorter. The key is to compact with instructions: run bare /compact and the automatic pass guesses what matters; tell it what to keep and the summary keeps what you chose. The official docs give this example:

/compact focus on the auth bug fix and the list of failing tests

In other words, before you compact, think about what the task needs next: the bug you are fixing and the tests still failing, or the list of files changed and the command that runs the tests. Put that in the compact instruction and the summary will keep those things first.

There is an even lazier way: write that sentence into your project-root CLAUDE.md under a heading called # Compact instructions. Every compaction after that — manual or automatic — follows it. Set it once and forget it. It suits the fixed information every task needs, such as "keep the list of files we changed and the test commands."

What actually survives compaction? The official docs have a very practical table for that. The system prompt and output style keep applying; the project-root CLAUDE.md and unscoped rules are re-injected from disk; auto memory is re-injected from disk; skill descriptions are rebuilt from your current skills and re-injected; if the session started with a Git status snapshot, a fresh one is read from your repository; the plan written in plan mode is re-injected from disk; invoked skill bodies are re-injected, capped at 5,000 tokens per skill and 25,000 tokens in total, with the oldest dropped first; background commands and background subagents keep running, and Claude Code reminds the model they are still running so it does not start duplicates.

But a few things do not survive, or survive at a discount. After compaction, Claude Code re-reads up to five of the files it read or edited in the session, most recently modified first; a file over 5,000 tokens comes back as a path reference without its content, shown as Referenced file instead of Read. Path-scoped rules and nested CLAUDE.md files load on demand when their trigger file is read, so compaction summarizes them away with everything else; if a rule must survive compaction, drop its paths: frontmatter or move it into the project-root CLAUDE.md. Skill bodies are re-injected too, but large skills are truncated to fit the per-skill cap — and truncation keeps the start of the file, so the most important instructions belong near the top of SKILL.md.

There is one more trap that is easy to walk into: a rule you only ever mentioned in conversation will probably be gone after compaction. The official docs put it bluntly: if an instruction disappeared after compaction, it was given only in conversation. Important rules do not belong in the chat log; they belong in CLAUDE.md.

Walk through it with "Money Bag." You ask Claude Code to add a monthly report to the ledger app. It reads four files and runs the tests twice, and the conversation is past twenty-two thousand tokens. You run /compact focus on the monthly report feature and the failing tests. The summary keeps "we are building the monthly report" and "which tests still fail," and drops the command outputs from the trial and error in between. When you then say "keep fixing the tests," it can carry on without re-reading the files.

Only want to compress half of it: /rewind

Sometimes the problem is not that the whole conversation is long, but that one stretch of it went down the wrong path: you had it try three approaches, the first two were dead ends, and only the third worked. Compressing everything is wasteful here; /rewind fits better. Press Esc twice on an empty prompt, or type /rewind, pick one of your earlier messages, and choose an action: Summarize from here squashes everything after that message into a summary, and Summarize up to here squashes everything before it. The wrong turn gets compressed away; the part that worked stays as it is.

This is best for trial-and-error stretches. The model has been walking further and further in a direction you can already see is wrong, and the conversation is full of attempts from that direction. Go back, compress that stretch, and it is like wiping the wrong half of the blackboard clean while the good half stays. Cheaper than a full compaction, and gentler than clearing everything.

Switching tasks? Clear it: /clear, and don't be sentimental

When a task is done, or you are moving on to something completely unrelated, run /clear. It empties the context entirely and starts a new, empty conversation. Your project memory (CLAUDE.md) stays; the conversation goes. The official docs are explicit about this: clear between unrelated tasks, because old conversation crowds out the files the next task needs and costs tokens on every message.

There is a cost difference here worth remembering. The official costs documentation states it plainly: /compact reads the conversation it summarizes, so compacting a large context is itself a large request; when you want a fresh start instead of continuity, /clear costs nothing. Compaction is paying for continuity; clearing is a free reset. Holding on to an old conversation after the task has changed is the worst of both: it occupies the window, burns tokens, and drags down the quality of the new task.

There is another signal to watch for: you have already corrected the model twice on the same thing and it is still making the same mistake. Stop correcting and clear and restart. The mistaken stretch of conversation has already pulled the model off course, and leaving it in the window only lets it keep explaining along the wrong direction. A fresh session with the task stated clearly is usually faster than a third correction. Worried you will lose the old session? Name it before you clear it — /rename login-fix-try-1 — and you can bring it back later with --resume. Session records stay on disk locally; clearing only empties the current window, not the history.

The auto-compact dial: /autocompact

If you never compact manually, Claude Code compacts on its own as you approach the limit. Automatic compaction is the same step as a manual /compact: it clears older tool outputs first, then summarizes the conversation if it still needs room. So even if you do nothing, a session does not die because the window filled up — it makes room for itself. The catch is that both the timing and what the summary keeps are its guesses, not yours.

To set the timing yourself, run /autocompact with a token count, for example /autocompact 500k: compact automatically once the window reaches 500,000 tokens. The setting is saved per model, so different models can have different values. The official docs offer another option: if your model supports a 1-million-token context — the Fable models, Sonnet 5 and later, Haiku 5.5, Opus 4.6 and later, and Sonnet 4.6 — the window itself is twice as large and the compaction point moves back with it. A bigger window is not a reason to stop managing context; it only gives you more room to make mistakes in. A 1-million-token session rots the same way, just later.

There is also a cheap compaction habit: compact manually before you step away. The usage advice in the official costs documentation includes exactly that — "compact before you step away." You go to lunch or to a meeting, and when you come back, a compacted session is far cleaner than a long conversation left sitting all afternoon: the first thing you say connects immediately instead of spending a few rounds pulling the model back from its drift. One more thing from that same costs page: session recaps and idle compaction can use tokens while you are idle. Leave a very long session sitting unattended and it may compact itself while you are away, adding a line to the bill you never watched. Compact it yourself before you leave — cleaner, and no surprise charge.

When the window is not big enough, get a bigger one?

Someone will ask: if the window is always running out, why not just switch to a 1-million-token window? The official docs answer that plainly: you can, but be clear about what it solves. With a model that supports a 1-million-token context, the window is twice as large and the automatic compaction point moves back with it, so the same work lasts longer. But a bigger window only postpones the problem; it does not cancel it. A 1-million-token session still fills up, still compacts, and still loses things in the summary. The bigger window fits work that is big by nature — taking over an unfamiliar codebase for the first time, where you need to read dozens of files before you understand its shape. It is not a license to run five unrelated jobs in one session. Split the tasks first, and only then consider a bigger window. Do not reverse that order.

Push the heavy work to a subagent; don't carry it yourself

Some jobs are expensive in context by nature: asking the model to first read through the whole codebase, open a dozen files, and then come back with a conclusion. If the main session does the reading itself, all dozen files land in your window. The right move is to send a subagent: the subagent has its own separate context window, the large reads stay in it, and only a summary and a small metadata trailer come back.

The official docs' example session has the numbers for this: the subagent used 9,890 tokens in its own window and came back with a 420-token summary. What the main session saves is not just those nine thousand tokens — it is also that its own window was never stuffed with files that were read once and never used again. Research, finding code, reading docs, making comparisons — any job that reads a lot and ends in a conclusion — belongs with a subagent.

The same logic applies in reverse: do not paste long logs into the main session by hand, and do not ask it to read whole directories. Before you paste, ask one question: do I really need this to stay in the window the whole time? If all you need is a conclusion, send a subagent. If you only need to confirm one error line, paste that one line.

Everyday discipline, on one page

The commands are easy to learn; the habits are the hard part. These are the ones you can follow while coding every day:

First, one session, one task. The session that fixes the login bug only fixes the login bug; when it is done, clear it and open a new session for the monthly report. More tasks means a longer session, and a longer session means compaction loses things. Sessions are divided by task, not by day.

Second, put rules in files, not only in conversation. CLAUDE.md is re-read from disk after every compaction; conversation is not. The important agreements, the project structure, the test commands go in the project-root CLAUDE.md. Rules that only apply inside one directory go in that directory's CLAUDE.md — but remember they need to be re-triggered after compaction.

Third, keep CLAUDE.md itself lean. It loads into the window at the start of every session, so the longer it is, the bigger the fixed cost of each session. The official usage advice gives an example of a CLAUDE.md costing 49,000 tokens per request. When writing CLAUDE.md, cut the pleasantries written for human readers and keep only the instructions and facts the model actually needs to work.

Fourth, do not paste whole logs and do not have it read whole directories. Paste the few lines of an error that matter; read one file at a time; use search to locate the target file before reading it. In the example session, reading files and running tests alone accounted for more than ten thousand tokens — pasting logs only makes that number bigger.

Fifth, check /context and /usage regularly. The first shows how much window is left; the second shows how much this session has already cost. Spending a lot is not necessarily bad — but spending a lot with no progress is a signal to stop and think.

Five anti-patterns

Anti-pattern one: one session from morning to night. Fixing a bug in the morning, adding a feature at noon, restyling in the afternoon, then going back to the bug in the evening. The window holds three unrelated jobs, and when compaction runs, the summary does not know which one to keep. One task, one session; when it is done, /clear.

Anti-pattern two: arguing with the model. You have corrected the same thing twice and it still gets it wrong, so you correct it a third and fourth time. Every round of arguing burns context, and the model gets more confused the longer it goes on. Stop after two corrections, clear, restart, and state the task more clearly.

Anti-pattern three: compacting without instructions. Running bare /compact and letting the automatic pass guess what matters. If it guesses wrong, the summary keeps the process and drops the conclusions. Spend ten seconds before compacting to say what to keep — or write that sentence under # Compact instructions in CLAUDE.md.

Anti-pattern four: keeping important agreements only in conversation. "Don't touch the database schema this time" said once in the chat log is gone after compaction. Agreements belong in CLAUDE.md; written into a file is what counts.

Anti-pattern five: stuffing everything into the main session. Research, reading code, reading logs — all in the main session, and the window fills up fast. Send the heavy work to a subagent; keep only conclusions and next steps in the main session.

Checklist: run through it before your next session

Before you start: is CLAUDE.md lean? Is today's work one task? Should you name the session first, so you can bring it back later with --resume?

While you work: when the model starts drifting, run /context first; when research means reading many files, send a subagent; when pasting an error, paste only the key lines.

As a task wraps up: run /compact with instructions, or use /rewind to compress the stretch that went wrong; before switching tasks, run /clear instead of carrying the old conversation into the new task.

When you step away: compact manually before you leave, so you can pick up right where you left off.

Context management is not an advanced technique. It is a bill plus a few rules. Once you can read the bill, the window stops filling up for no reason; once you keep the rules, the model stops getting stupid for no reason. Fold these few moves into your daily coding and one session will last you three or four times as long. The two to adopt first are these: one session, one task — and clear when the task changes; and always compact with a sentence saying what to keep, instead of letting the automatic pass guess. Get those two right and the rest follow easily. When you get used to this way of working, you will find the model "going dumb" far less often — most of the time it was never the model getting dumber. The window was just too full for it to see what you actually wanted.

Views 5Comments 0

Comments (0)

ME
0/1000
Loading comments...

Sources

Browse projectsPublish your project

Related articles

Real photo: hands typing on a MacBook Pro showing JavaScript code that changes HTML content, with a white mouse, papers, and binders on a white desk
Guide
Functions without the confusion: JavaScript functions, parameters, and arrow functions, explained plainly

You already call functions every day; this guide names what you have been doing. Following MDN's Functions, Arrow function expressions, and Rest parameters: declarations versus expressions, parameters and arguments, default values, rest parameters, spread syntax, return values, arrow function shorthand, the this trap, callbacks, and scope.

Tool TipsFrontend EngineeringAI Coding
Real photo: a Casio scientific calculator on a wooden desk showing 123456789 + 987654321 = 1111111110, beside handwritten math notebooks, a pen, a mug, and a laptop edge
Guide
Don't let 0.1 + 0.2 break your bill: JavaScript numbers and Math, explained plainly

Numbers in JavaScript are one type, Number, stored as 64-bit floats — which is why 0.1 + 0.2 is 0.30000000000000004. This guide follows MDN's Basic math in JavaScript, Math, and Number: one number type, float error, counting money in cents, converting strings, NaN and Infinity, safe integers, the four rounding methods, Math.random, the Math toolbox, and formatting for display.

Tool TipsFrontend EngineeringAI Coding
Real photo: a woman in an office sticking colorful sticky notes in rows on a white wall, holding a pink note in her hand
Guide
Stop shimming with margins: Flexbox one-dimensional layout, explained plainly

Flexbox lays things out in one direction at a time, and when it is done right, the nav bar, the card wall, and the cart rows all behave. This guide follows MDN's Basic concepts of flexbox, Aligning items in a flex container, and Controlling ratios of flex items along the main axis: the two axes, containers and items, direction and wrapping, free space, the three flex properties, alignment on both axes, and gaps and auto margins.

Developer WorkflowFrontend EngineeringAI Coding