Search⌘ K
AI Features

Session Strategy and Context Primer

Explore how to manage AI agent sessions effectively during long-running tasks by learning key strategies like resuming, forking, compacting, and starting clean. Understand how to prevent context degradation, use scratchpad patterns to preserve essential data, and decide the best approach based on session progress and task needs to keep agent behavior accurate and reliable.

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 /compact reduces context size without losing progress

  • How 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 ...