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.
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.
- The Key Decision: What to keep and what to discard. This is an information-theory problem — not all Tokens are equal, and some information is irrecoverable once lost.
- Low-Risk Operations: Pruning old tool call results (e.g., file listings, search outputs) — these generally don't affect downstream reasoning.
- High-Risk Operations: Discarding the reasoning behind architectural decisions or unsolved bug descriptions — if dropped, the Agent will repeat past mistakes.
- Claude Code in Practice: Retains architectural decisions and unresolved bug information; discards redundant file content output and intermediate steps of completed tasks.
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.
- Core Idea: Externalize short-term memory (context window) into long-term memory (file system), enabling information continuity across windows.
- Claude Code in Practice: Maintains a TODO list file, updating it after each step — so even if the context is compressed or reset, opening TODO immediately shows progress.
- Claude Plays Pokémon Case Study: The Agent maintained a game notes file recording map position, collected items, and next steps. Each new conversation began by reading this file to resume memory.
- Key Design Principle: Note format must be fixed and structured — free-form prose requires extra Tokens to interpret when read back.
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.
- Core Value: Separation of concerns + context isolation. A sub-agent's working drafts don't pollute the orchestrator's context.
- Token Economics: A sub-agent may internally consume 30,000 Tokens reading code, analyzing dependencies, and reasoning — but reports only 1,500 Tokens of conclusions upstream. The orchestrator's context stays lean.
- Parallelism Advantage: Multiple sub-agents can work simultaneously, each exploring a different direction, with the orchestrator synthesizing results. This is far faster than a single Agent exploring serially.
- Real-world Examples: Cursor's background agent and Claude Code's Task tool are both implementations of sub-agent architecture.
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.
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.