The Deska blog
Context Management for Long Agent Sessions
Learn effective context management for long agent sessions, including token compaction and infinite canvas strategies for AI development.
· 10 min read
Effective context management for long agent sessions is the primary bottleneck in autonomous software engineering today. As developers shift from simple chat completions to multi step agentic workflows, the accumulation of chat history, file content, and terminal output quickly exceeds the practical limits of any Large Language Model window. To maintain a coherent workflow, developers must move beyond basic truncation and adopt sophisticated strategies for state retention and noise reduction.
The Challenge of Token Drift in Long Sessions
The more an agent interacts with a codebase, the more information it must track. Every command run in a terminal and every file read adds to the active memory. When these sessions span hours or days, the agent starts to suffer from token drift. This occurs when the most relevant architectural decisions are buried under hundreds of lines of recent, but less important, build logs or diagnostic output.
Current LLMs have expanded their context windows significantly, yet the problem remains. Information at the center of a long context block is often harder for the model to recall than information at the beginning or the end. This phenomenon necessitates a proactive approach to what we feed the agent. If you provide everything, the agent becomes slow, expensive, and prone to hallucinations.
Practical Strategies for Context Compaction
Compaction is the process of reducing the volume of data without losing the essential meaning. In the context of AI agents, this can be achieved through several technical layers.
- Selective History Pruning: Regularly removing redundant conversational turns where the agent and user were simply troubleshooting a minor syntax error.
- Summarization of Logs: Instead of keeping 500 lines of a failed build log, the system should replace that block with a high level summary of the error message and the stack trace.
- Hierarchy of Relevance: Moving less critical files out of the active context and into a retrieval augmented generation (RAG) system until they are explicitly needed.
Many developers rely on manual intervention to keep the context clean. You might find yourself opening a new session and pasting only the relevant bits of code to reset the "brain" of the agent. This is a common but inefficient workaround for a lack of native context management tools.
The Architecture of Infinite Canvas Workspaces
An alternative approach to managing long sessions involves spatial organization. Instead of a linear chat thread, an infinite canvas allows you to decouple different parts of a project. By placing panels across a large workspace, you can visually organize the context.
In Deska, for example, the infinite canvas workspace lets you place multiple agent panels side by side. You can have Claude Code working on a backend refactor in one area while Codex CLI handles your deployment scripts in another. Because these are separate panels, their individual context windows remain focused on specific tasks. You can zoom out to see the entire system or zoom in to focus on a single agent session.
This spatial model prevents the "everything everywhere" problem. When you need to cross reference information, you can use Ask Deska to bridge the gap. The assistant can look across different panels and sessions to provide an overview without dragging the entire history of every panel into a single LLM prompt.
Local First Benefits for Context Security
When dealing with deep context, privacy becomes a major concern. Sending thousands of lines of proprietary code to a cloud service for hours on end is a security risk for many organizations. A local-first approach ensures that the primary data resides on your machine.
By keeping files, terminals, and session logs local, you reduce the latency of context retrieval. This is particularly useful when using a mobile companion app to monitor agent progress. Instead of the mobile device needing to download a massive cloud database, it connects via a secure relay to your local machine, allowing you to check on a long running agent session without the data ever being stored on an intermediate server.
Integrating Multiple Agents Side by Side
One of the most effective ways to handle long agent sessions is to distribute the work among specialized agents. Rather than asking one agent to manage the frontend, backend, and documentation, you can run different tools for each.
- Open a terminal panel for a low level CLI agent like OpenCode.
- Open a Monaco based code editor for manual review.
- Open a browser panel to monitor live documentation or UI changes.
By running these panels side by side, you create a modular context. If the frontend agent gets confused by a long session, you can restart that specific panel without losing the progress or context of the backend agent. This isolation is a key tactic for maintaining high performance over several days of development.
FAQ on Agent Context
How to reduce agent token costs?
The most effective way to reduce costs is through aggressive context compaction. By using tools that allow you to prune old terminal output and by summarizing long file reads, you send fewer tokens to the provider. Using your own API keys via a pricing model that supports BYOK can also help you manage expenses directly with the LLM provider.
Why do AI agents forget instructions?
Agents typically forget instructions because the initial system prompt or the early user requirements are pushed out of the context window by new data. This is why managing agent threads is vital. You must ensure that high level goals stay pinned or are frequently re-injected into the active session.
Best way to manage long terminal logs?
Long terminal logs should never be sent in their entirety to an LLM. Use a workspace that supports partial selection or automatic truncation of terminal output. This keeps the agent focused on the output of the most recent command rather than the history of the entire session.
Getting Started with Optimized Workspaces
Managing context shouldn't be a manual chore that takes you away from coding. By using a specialized environment designed for AI collaboration, you can automate much of the noise reduction required for complex tasks.
If you are looking for a way to organize your AI agents, terminals, and code in a single, local-first environment, you can download Deska for Mac, Windows, or Linux. The workspace provides the infinite canvas infrastructure needed to keep your sessions organized and your agent context windows under control.