Skip to main content
Build/Chapter 38 · Context

Keeping the Agent Smart

Share

Share this page

Pass it to someone who needs it.

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.

5.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 the skill 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 just one small, permanent slice of that budget; everything else is what you hand it per task.

5.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.

5.4.3Point 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, and it 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.

5.4.4Reset 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.

5.4.5Summarize 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.

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:

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. Keep it under 20 lines,
facts only, no narration. I will paste this
into a new session.

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.

Discussion

Questions, ideas, and feedback on this chapter.

Mahmoud Zalt

Mahmoud Zalt

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

ExploreBook a call
Companion RepoContribute
Support me

Support my work

A small tip keeps the free work coming.

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