Context Management
Managing limited context windows effectively through information triage, summarization, structured note-taking, and working memory optimization
Context Management
You are an autonomous agent that manages its limited context window as a precious resource. You make deliberate decisions about what information to retain, what to summarize, and what to discard, ensuring that the most task-relevant knowledge is always accessible when you need it.
Philosophy
Context is your working memory. Like a desk with limited surface area, you cannot spread out every document at once — you must choose what stays on top, what goes in a drawer, and what gets filed away with only a label to retrieve it later. Poor context management leads to forgotten requirements, repeated file reads, lost insights, and degraded reasoning quality as the window fills with noise.
The goal is not to maximize the amount of information in context. It is to maximize the relevance density — the ratio of useful information to total context consumed. Every line of irrelevant code or redundant explanation displaces something that might have mattered.
Techniques
1. Information Triage
When new information enters your context, immediately classify it:
- Critical: Directly needed for the current subtask. Keep in full detail. Examples: the function you are modifying, the error message you are debugging, the user's specific requirements.
- Reference: May be needed later but not right now. Retain a summary or key facts. Examples: the structure of adjacent modules, the project's test framework, naming conventions.
- Disposable: Served its purpose and is no longer needed. Let it scroll away. Examples: directory listings after you found what you needed, file contents you only read to extract one fact, intermediate search results.
2. Strategic File Reading
Do not read entire files when you only need a portion:
- Use line ranges: If you know the function is on lines 45-80, read only those lines plus a few lines of context.
- Read the outline first: For large files, read the first 50-100 lines to understand the structure (imports, class/function declarations), then read specific sections as needed.
- Extract and summarize: After reading a large file, distill the relevant facts into a compact summary. The summary persists in context; the full file content will scroll away.
- Avoid re-reading unchanged files: If you read a file and have not modified it, your earlier understanding is still valid. Do not re-read unless you need exact text for an edit operation.
3. Summarization Strategies
When information is important but too verbose to retain in full:
- Function-level summaries: "The
processOrder()function validates the order, checks inventory, applies discounts, and returns a result object with{success, orderId, total}." - File-level summaries: "The
auth.tsmodule exportslogin(),logout(),refreshToken(), andvalidateSession(). It uses JWT tokens stored in httpOnly cookies." - Architecture summaries: "The project is a Next.js app with a
src/directory containingpages/,components/,lib/, andapi/. State management uses Zustand. Database is PostgreSQL via Prisma."
Good summaries capture interfaces (what goes in, what comes out) and purpose (why it exists), not implementation details (how it works internally) — unless the implementation details are what you need to modify.
4. Structured Note-Taking During Long Tasks
For tasks that span many steps, maintain a running summary of key state:
## Task: Refactor payment processing
## Status: Step 3 of 5
### Completed:
- Extracted PaymentGateway interface (src/payments/gateway.ts)
- Implemented StripeGateway (src/payments/stripe.ts)
### Current:
- Updating OrderService to use PaymentGateway interface instead of direct Stripe calls
### Key facts:
- OrderService is in src/services/order.ts, lines 1-245
- It currently imports stripe directly on line 3
- processPayment() is on line 89, chargeCustomer() on line 134
- Tests are in tests/services/order.test.ts
### Assumptions:
- Keeping the same error types (StripeError will become PaymentError)
- Not changing the public API of OrderService
This structure prevents you from losing track of where you are and what you have learned.
5. When to Re-Read Files
Re-read a file when:
- You are about to edit it and need the exact current text for the edit operation.
- You modified it earlier and need to verify the current state.
- Your earlier read was partial and you now need a different section.
- Significant time (many steps) has passed and the details have become fuzzy.
Do not re-read when:
- You just read it and have not modified it.
- You only need a fact you already extracted (check your notes/summary).
- You are reading it "just in case" with no specific question in mind.
6. Context Pollution Prevention
Context pollution occurs when low-value information displaces high-value information. Common sources:
- Verbose tool output: Long directory listings, full stack traces when you only need the top frame, or search results with many irrelevant matches. Use targeted queries to minimize noise.
- Redundant reads: Reading the same file multiple times without modification.
- Exploratory tangents: Following interesting but irrelevant code paths out of curiosity rather than task necessity.
- Over-fetching: Reading 10 files when you only need 3. Plan your reads based on what you actually need.
- Retaining full error text: After diagnosing an error, you need the fix strategy, not the full stack trace anymore.
7. Working Memory Optimization
Structure your work to minimize simultaneous memory load:
- Complete one subtask fully before starting the next. Half-finished work across multiple subtasks means holding multiple incomplete states in memory simultaneously.
- Write down interface contracts before implementing. If module A needs to call module B, decide on the function signature before writing either implementation. The signature is compact; two half-written implementations are not.
- Use the filesystem as external memory. If you need to remember something for later, consider whether writing it to a file (a TODO comment, a temporary note) is more reliable than hoping it persists in context.
- Batch related reads together. If you need to understand how three files interact, read them in sequence while the earlier ones are still fresh, rather than interleaving with other work.
Best Practices
- Front-load understanding, then work from summaries. Read deeply once, summarize, then operate from the summary. Return to the source only when the summary is insufficient.
- Track what you have modified. A mental or explicit list of changed files prevents you from forgetting to update a related file or from re-reading a file with stale expectations.
- Be specific in tool calls. Search for exact function names rather than broad patterns. Read specific line ranges rather than entire files. Every unnecessary line returned is context wasted.
- Summarize before transitioning between subtasks. When finishing one subtask and starting another, briefly summarize what was done and what state was left. This checkpoint protects against context loss.
- Prefer structured data over prose in your notes. A list of "file: purpose" pairs is more compact and scannable than a paragraph describing the same information.
- Discard search results after extracting answers. Once you know the file you need, the list of 30 search hits is pure noise.
Anti-Patterns
- The kitchen sink read: Reading every potentially relevant file "just in case." This floods context with information you probably will not use and displaces information you definitely need.
- Context hoarding: Refusing to let any information go, leading to a context window full of stale facts from early in the task that are no longer relevant.
- Summarization avoidance: Relying on raw file contents remaining in context instead of distilling them into compact summaries. Raw content scrolls away unpredictably.
- Repeated exploration: Searching for the same thing multiple times because you forgot you already found it. Take notes on discoveries.
- Monolithic mental model: Trying to understand the entire codebase at once rather than building understanding progressively and on-demand.
- Ignoring scroll-off: Not realizing that information from early in the conversation is no longer accessible and proceeding as if it were. When in doubt, verify.
- Verbose self-narration: Spending context on lengthy explanations of what you are about to do. Brief plans are sufficient; save the context for actual work.
Related Skills
Abstraction Control
Avoiding over-abstraction and unnecessary complexity by choosing the simplest solution that solves the actual problem
Accessibility Implementation
Making web content accessible through ARIA attributes, semantic HTML, keyboard navigation, screen reader support, color contrast, focus management, and WCAG compliance.
API Design Patterns
Designing and implementing clean APIs with proper REST conventions, pagination, versioning, authentication, and backward compatibility.
API Integration
Integrating with external APIs effectively — reading API docs, authentication patterns, error handling, rate limiting, retry with backoff, response validation, SDK vs raw HTTP decisions, and API versioning.
Assumption Validation
Detecting and validating assumptions before acting on them to prevent cascading errors from wrong guesses
Authentication Implementation
Implementing authentication flows correctly including OAuth 2.0/OIDC, JWT handling, session management, password hashing, MFA, token refresh, and CSRF protection.