Agent Design Patterns

Three Strategies for Long-Context Agents

When a task spans multiple context windows, each new window starts with amnesia. Three battle-tested strategies address this fundamental challenge, keeping Agents coherent and efficient across long tasks.

The Core Challenge
The Amnesia Problem in Long Tasks
A complex coding task may require the Agent to perform dozens of steps, generating tens of thousands of Tokens of conversation history. As the context window fills up, the system faces a dilemma:

Option A: Start a new window — but the new window remembers nothing and the Agent repeats work already done.
Option B: Continue in the old window — but as Tokens accumulate, the model's attention is diluted and performance degrades.

This is not a theoretical problem. Claude Code, Cursor, and Devin face this every day in production.
Strategy 1
1
Compaction
Context Compression
When the conversation is approaching the context window limit, make a single LLM call to summarize the existing conversation: retain critical information, discard redundant details, then continue working on the compressed context.
Practical Tip
The summary generated by the LLM during compaction should be structured — free-form prose makes it hard to locate information quickly. For example: "Done: [list] | Pending: [list] | Key Decisions: [list] | Known Issues: [list]" — this allows downstream reasoning to find what it needs fast.
Strategy 2
2
Structured Note-taking
Externalizing Memory
The Agent actively writes critical information to external files during execution rather than relying solely on conversation history. When the context resets (new window), it reads from the note file to restore memory.
Comparison with Compaction
Compaction compresses old information to continue using it; Note-taking stores information externally to retrieve later. The former suits continuous work sessions; the latter suits scenarios that may be interrupted or need to span multiple sessions. Both can be combined.
Strategy 3
3
Sub-agent Architecture
Delegated Deep Exploration
The orchestrator Agent delegates subtasks requiring deep exploration to sub-agents. Each sub-agent works within its own isolated context window (potentially consuming tens of thousands of Tokens), ultimately returning only a refined summary (1,000–2,000 Tokens) to the orchestrator.
Analogy
Imagine a CEO (orchestrator Agent) assigning 3 department managers (sub-agents) to research competitors, analyze the market, and evaluate technology respectively. Each manager may spend a week (many Tokens), but only delivers a one-page executive summary to the CEO (a refined summary). The CEO's cognitive bandwidth stays at the strategic level.
JIT Context vs Preloading
Preloading
Inject information into context at the start of the conversation
  • CLAUDE.md / Rules files loaded directly
  • User preferences, project configuration
  • Frequently used context information
  • Pro: Immediately available, no extra calls
  • Con: Consumes Tokens every time, whether needed or not
JIT (Just-In-Time)
Retrieve information into context only when needed
  • Use glob/grep to search files on demand
  • Use RAG to retrieve relevant documents
  • Call APIs for real-time data
  • Pro: Context stays lean, only contains what's needed now
  • Con: Adds one tool-call round-trip of latency
Best Practice: Hybrid Strategy
Preload high-frequency information (project conventions, core rules, user preferences) + fetch long-tail information on demand (specific file contents, API docs, history logs).

Analogous to browser caching: hot data in memory cache (preload), cold data fetched from disk or network (JIT). The goal is to maximize context hit rate — most information needed for reasoning is already in the window, with dynamic fetching reserved for occasional needs.
Comparison of Three Strategies
Strategy Core Idea Best For Examples
Compaction Compress old context, retain critical info and continue Continuous long sessions without interruption Claude Code auto-compact
Note-taking Actively write notes externally, read back across windows Tasks that may be interrupted or span multiple sessions Claude Code TODO, Cursor Rules
Sub-agent Sub-agent explores deeply, returns only a summary Deep exploration without polluting the orchestrator's context Cursor Task, Claude Code spawn
The fundamental challenge of long tasks is a finite attention window versus an infinitely growing body of information. Compaction, Note-taking, and Sub-agent Architecture each solve a distinct problem: keeping the window lean, transferring memory across windows, and isolating the noise of deep exploration. Used together, they keep Agents efficient throughout complex, long-running tasks.