Session Strategy and Context Primer
Explore strategies to manage context in long AI agent sessions, including resuming, forking, summarizing, and starting fresh. Understand how to detect context degradation, use the scratchpad pattern to preserve critical facts, and apply best practices for reliable multi-turn workflows.
The loops we have built so far work on single, self-contained tasks. Production agents often run longer: a debugging investigation that takes 30 turns, a code review that spans multiple files, or a research task that accumulates dozens of tool results. In those sessions, context becomes a resource that must be managed, not just a byproduct of running the loop.
In this lesson, we cover what goes wrong in long sessions, the four session strategies that address different phases of the problem, and the scratchpad pattern that keeps critical information safe across context compression. By the end of this lesson, we will understand:
The symptoms that indicate context degradation in a running agent
When to resume a session vs. fork it vs. start fresh
How
/compactreduces context size without losing progressHow a scratchpad file preserves facts that context compression would otherwise discard
What goes wrong in long sessions
Every API call sends the full messages array. As a session runs, that array grows: each tool call adds an assistant turn and a user turn, and each response adds more text. Eventually, the array becomes large enough that early content is compressed or dropped by the model. Context degradation does not announce itself with an error. It shows up as a change in behavior. The four most common indicators are:
The agent forgets instructions stated early in the session, such as a formatting rule or a constraint from the original task
Responses become more generic and less specific to the task at hand
Tool selection accuracy drops, and the agent picks the wrong tool for the situation
The agent repeats work it has already completed because it no longer has the earlier results in context
These indicators are not bugs in the application code. They are the natural consequence of a context window that is approaching its limit. The messages array is the agent’s entire working memory. When that memory is full, older content yields to newer content.
The four-session strategies
There is no single correct response to context degradation. The right strategy ...