Programming with AI
ENRU
5.3

Session context

why the AI starts getting confused towards the end

What it is

Context is everything the AI "sees" in the current conversation: your messages, its own replies, the files it read, the output of commands. It has a limit, and as a conversation approaches it, the quality of the answers drops.

It drops gradually rather than sharply — which is why the signs are worth recognising early.

What it looks like in code

Diagram from the book — to be redrawn for the web
The longer the session, the less room is left for the task. Towards the end the AI is leaning mostly on things no longer current.

Signs that the context is full

SignWhat is happening
The AI forgot a requirement from the startthe old was pushed out
It returns to a rejected solutionboth versions are in the context
It contradicts itself in neighbouring repliesit leans on different parts of the history
The answers got shorter and vaguerless room for reasoning
It "re-read" a file and produced the old versionit remembers the first read, not the last

The last sign is the most treacherous with an agent: the file changed long ago and the AI is reasoning about what it saw at the start of the session.

What takes up the room

The biggest consumers are not your messages but:

  • files that were read — an agent reads the whole file even when one function is needed;
  • command output — long logs, file listings, test output;
  • the AI's own replies — especially long code listings;
  • rejected versions — everything you tried stays in the history.

What to tell the AI

The main technique is not to accumulate anything unnecessary.

Prompt
Read only the fetch_weather function from handlers/weather.py,
not the whole file.
Prompt
Show only the last 20 lines of the log, not the whole file.

When the session is already long, compression helps — ask the AI for a summary you can carry into a new session:

Prompt
Summarise: what we did, which decisions we made and what is
left. Briefly, so I can start a new conversation from that text.

To check whether the context has drifted:

Prompt
Which requirements for this task do you currently remember?
List them.

If something you agreed at the start is missing from the list, it is time to start over (article 5.4).

What to look for

Files read in full for no reason. One thousand-line file pushes out the whole previous conversation.

Long logs pasted in their entirety. What is needed is the chunk around the error, not all of it.

A session that has run all day. By evening it holds three abandoned approaches and two versions of the code. Starting over is easier than explaining which of it is out of date.

"But you remember" as an argument. If the AI forgot, repeating is cheaper than finding out why it forgot.

Cheat sheet

  • Context is finite; the longer the session, the less room for the task.

  • Signs: forgot a requirement, returned to a rejected idea, contradicts itself.

  • The biggest consumers are files that were read and command output.

  • Ask for part of a file and part of a log, not all of it.

  • The check: "which requirements do you currently remember?"

  • A long context does not make an AI smarter.