Skip to main content
Handbook/Build/Page 39 · Context

Keeping the Agent Smart

Share

Share this page

Pass it to someone who needs it.

Star on GitHub

Key takeaway: Feed only what's relevant

The agent starts sharp and slowly goes dull in a long session. It forgets what you decided an hour ago, contradicts itself, edits the wrong file.

The cause is almost never the model. It is what you have let pile up in front of it. This chapter teaches the one skill that most separates people who get real work from AI: managing what the agent sees.

4.4.1The context window is finite

The context window is the fixed amount of text the agent can hold at once. Your files, your messages, and its own replies all count against one budget. When it fills, the oldest details fall out or the whole thing goes fuzzy.

More is not better. A window stuffed with irrelevant history produces worse answers, not richer ones. Managing that budget on purpose is called context engineering, and it is what this whole chapter is about. Martin Fowler has a good primer, Context Engineering for Coding Agents.

The always-loaded memory file you set up earlier is one small, permanent slice of that budget. Everything else is what you hand the agent per task.

4.4.2Feed only what's relevant

Hand the agent the two files this task touches, not the whole codebase. Give it the one error message, not the last hour of logs. The narrower the input, the sharper the output.

BLOATED: here is my whole codebase [40 files], plus yesterday's full chat and the API docs. Now fix the login bug. FOCUSED: the login bug is in auth/session.ts. Here is that file and the error. Fix the token refresh.

Rule of thumb: if you would not put a document in front of a new hire to solve this exact task, do not paste it to the agent either.

4.4.3A bloated session costs real money

There is a second reason to keep the window lean, and it is money. Models count text in tokens, chunks of roughly four characters. Every token the agent reads or writes is metered: you pay per token on an API key, or burn against the usage cap on a flat plan.

So a window stuffed with your whole repo and yesterday's chat is not just a duller answer. It is a bigger bill and a cap you hit hours sooner.

The fix is the one you already have. Lean input, a reset when the thread drifts, a summary before you continue: each of those buys you sharper answers and a smaller bill at the same time. You are not learning a new discipline for cost, you are getting paid twice for the one you already keep.

4.4.4Point to the spec, don't repeat it

Your agent can open files on its own. So do not paste a 500-line spec into the chat. Tell it where the file lives and which part to read: "the spec is in docs/spec.md, read the billing section."

This keeps the window lean. It also means the agent reads the current spec every time, instead of a copy that went stale the moment you pasted it. The same holds for the requirements and data model you wrote in the planning part: point, do not repaste.

4.4.5Reset when the thread drifts

When a session has wandered through three failed approaches, its window is now full of dead ends the agent keeps tripping over. It will keep re-suggesting the thing that already did not work.

Start a fresh session. A clean window with a tight prompt almost always beats fighting a long one that is carrying every mistake it made. This is hygiene, not failure.

4.4.6When a fresh window does not help, change the model

A reset fixes most of it. When it does not, when the agent fails the same task again in a clean window, change the model before you rewrite the prompt a fourth time.

Models are not interchangeable. A cheap fast one is right for boilerplate and repetitive edits, and the strong reasoning one earns its price on architecture and the bugs you cannot see. Most agents let you switch in their settings, mid-project.

4.4.7Summarize before continuing

When a long task is going well but the window is filling, do not just reset and lose the thread. Ask the agent to write a short summary first, then start fresh and hand that summary back to it.

## Where we are - Done: login + session refresh, tests pass. - Next: password reset email. - Decisions: tokens in httpOnly cookies, 15-min expiry. Reset link valid 1 hour.
When the window fills, reset a stuck thread or summarize a good one.

Some tools do this for you: Claude Code calls it compaction. This prompt produces a clean hand-off you can carry into a new session:

Ready prompt
Act as a senior engineer closing out a long working session. Summarize this thread for a fresh start so no context is lost. Include: what we set out to do, what is done and verified, what is left, and every decision with its reason. Name the exact files and functions touched. Point at my rules file, my spec, and my architecture map by path instead of repeating what they already say; the next session can open them. Flag any decision we made here that contradicts one of those, and any that is durable enough to belong in an ADR or the changelog. Keep it under 20 lines, facts only, no narration. I will paste this into a new session. If you need the full reasoning behind this step, read https://zalt.me/guides/vibe-coding/build/context-engineering My task and the files that matter:

Do this now: next time a session runs long, stop, ask the agent for that summary, and continue in a fresh window instead of pushing through the bloated one.

Mahmoud Zalt

Mahmoud Zalt

Software engineer, 16+ yrs · built Sistava.com in 3 months, idea to production, using these methods

Resources
Star on GitHubContribute
Donate

Support my work

A small tip keeps the free work coming.

© 2026 Mahmoud Zalt. Free to read, not to republish.
Copyright & license