Context Is a Budget: Hygiene Habits for Long Sessions with a Coding Agent

Date:

The longer you work with a coding agent, the more its memory, not its intelligence, decides whether the session stays sharp or quietly falls apart. A model that reasons brilliantly at hour one can lose the thread by hour four, not because it got dumber, but because the context window filled with clutter and the important decisions scrolled out of reach. Treating that window as a budget you spend deliberately, rather than a buffer that fills by accident, is the single biggest lever most people never pull.

The short answer: A coding agent degrades over a long session not because the model gets worse but because its context window (the finite memory holding the conversation) fills with clutter until the decisions that matter scroll out of reach. Treat that window as a budget you spend on purpose: compact deliberately at a task boundary around 70–80% full instead of letting an automatic summary fire mid-edit; give every parallel task its own isolated workspace so two sessions can’t corrupt each other’s staging; read the live source of truth instead of a stale local copy; and capture raw data, selecting the fields you need downstream. Four cheap habits, one principle (spend a finite resource deliberately), and a long session stays as sharp at the end as it was at the start.

The window is a budget, not a buffer

Every message, file read, and tool result an agent produces lives in a fixed context window. When it fills, the session summarizes older turns into a lossy recap and keeps going. That recap is a genuine trade: exact details (a code snippet, an error string, a file path) are replaced with a paraphrase. Useful, but not the same thing.

Because the summary is lossy, when it happens matters as much as that it happens. Let it fire automatically at the last moment and you get an uncontrolled summary at the worst time: mid-edit, with no headroom left to finish the current step. Trigger it yourself, at a natural boundary, and you decide what survives.

Compaction is a trade, not a free save. Spend it on purpose.

Compact at a boundary, around three-quarters full

The habit that pays off: watch the context meter, and when it crosses roughly 70–80% and you’re between logical steps, compact deliberately, ideally with a one-line hint about what to preserve (“focus on the build we just finished and the remaining tasks”). A task boundary is the cheapest place to do it, because less live working state is in flight.

Two mistakes bracket that sweet spot:

  • Too early (say 30%) wastes fidelity for space you don’t need, and often makes the agent re-read files it still had. There’s also little to compress well when there isn’t a clear chapter to fold up.
  • Too late (waiting for the automatic pass) hands control to the machine at the moment you can least afford it.

Compact between chapters, not mid-sentence.

Four habits, one principle: spend a finite resource on purposeEach habit treats a scarce resource as a budget, not a buffer
Context window

Compact at a boundary ~70-80% full not the auto-pass

Working state

One checkout per session reap it once merged

Source of truth

Query the live remote not a stale local copy

Someone's server

Store the whole response select fields downstream

Context, working state, other people's servers: each is finite, and each stays cheap only when you spend it on purpose.

Give every parallel task its own workspace

The subtler failure isn’t about the window at all. It’s about shared state. Run two agent sessions against the same checkout of a project and they share one working directory and one staging area. A broad “stage everything and commit” in one session will happily sweep up the other session’s half-finished work into the wrong changeset. Nobody notices until a review turns up files that don’t belong. It is the same class of collision as a human and an automation driving one browser tab, where two actors on one mutable surface corrupt each other’s work.

The fix is boring and total: one workspace per session. Give each task its own isolated checkout (a git worktree, one per parallel agent) so two sessions can never step on one another. It costs a few seconds of setup and removes an entire category of “where did that change come from?” incidents.

And reap as you go. Isolated workspaces are cheap to create and easy to forget; on a busy stretch it’s normal to accumulate a dozen, most already merged and doing nothing but adding confusion. Clearing the merged, clean ones is part of the loop, not a chore you do “later” — later never comes.

Read the source of truth, not the stale copy

A specific trap worth naming: a shared, long-lived checkout of a project’s main branch drifts behind the real remote the moment anyone merges. Search that local copy and you’re reading yesterday’s tree, and you’ll “confirm” something is a certain way when it changed hours ago. When the question is “what’s actually on main right now?”, ask the remote reference directly instead of the local working copy. It’s a one-line change of habit that prevents confidently-wrong answers. It is the same distinction as a real loss versus a merely stale read, where trusting an out-of-date view invents a problem that the system of record would have denied.

Capture raw, interpret later

The last principle comes from data collection, but it’s really about the same budget in a different currency. When an agent gathers information from an outside source (pages, feeds, API responses), the temptation is to keep “just the fields we need” at collection time. That’s a quiet bet that today’s questions are the only questions you’ll ever ask.

That bet loses silently. The day a new question arrives, the answer is in a field you dropped, and the only way back is to collect it all over again, slower and harder on whoever’s server you’re pulling from. Store the whole thing; select downstream, where the choice re-runs for free. A size or checksum check won’t save you here: it happily passes on a trimmed copy, because it’s comparing the copy against itself.

Trimming at capture is interpretation in the wrong layer — and it’s unrecoverable without doing the work twice.

The short version

None of these is exotic. Compact on purpose, at a boundary. One workspace per task, reaped when done. Read the live source, not a stale local copy. Capture raw and interpret later. Every one of them treats a finite resource (context, working state, other people’s servers) as something you spend deliberately instead of consume by default. Do that consistently and long agent sessions stop degrading into guesswork and stay as sharp at the end as they were at the start.

Frequently Asked Questions

Isn’t compacting early “safer”?

No. Early compaction discards exact detail for space you don’t need and often forces the agent to re-read what it just had. Compact around 70–80% full at a task boundary; a typical long session needs it only a handful of times, at chapter breaks.

Should I compact or start a fresh session?

Compact when the next step continues the same work, so the summary carries the decisions forward. When you’re pivoting to genuinely unrelated work, a fresh session is usually cleaner: nothing from the old task competes for the window, and nothing has to survive a lossy summary.

Why one workspace per session instead of just being careful?

Because “be careful” fails under load. Two sessions sharing one checkout share one staging area, so a single broad commit can capture the wrong work. Isolation makes the mistake impossible rather than merely unlikely.

Share post:

Subscribe

Popular

More like this
Related

Preventing Accidental Data Loss When a Git Worktree Is Removed

A script that saves its output next to its own source file writes into whichever checkout runs it. Run it from a linked worktree, remove the worktree, and the output is gone, with no warning from git.

Apply Database Changes to Production by Re-running the Migration, Not Copying Rows

Your schema change works perfectly on staging. The new table looks exactly right. So you dump that table, restore it onto production, and move on - and a week later production has quietly lost a day of real writes, or a foreign key points at a row that only ever existed in your test data. The change itself was correct. The way you shipped it was not.

An SSH Disconnect Kills Background Process Trees – and nohup Won’t Save Them

You SSH into a machine, kick off a long job, background it, redirect its output to a file, and close the laptop with the comfortable feeling that it will keep running. You come back an hour later and it's gone: the log stops dead a few seconds after you disconnected, the work half-finished, no error anywhere. You did everything the old advice says, nohup included, and the job still died the moment you hung up.

Why innerText Glues Your Scraped Values Together

Your scraper reads a product card and stores ₹1,249₹1,39910% off. Three values went in and one string came out, and no code can reliably split it back.