Memory Management
Efficient context and knowledge management.
Instructions
- Working Memory Model
┌─────────────────────────────────────────┐ │ WORKING MEMORY │ ├─────────────────────────────────────────┤ │ • Current task goal │ │ • Relevant file contents │ │ • Recent decisions │ │ • Active constraints │ └─────────────────────────────────────────┘ ↑ Load ↓ Store ┌─────────────────────────────────────────┐ │ LONG-TERM MEMORY │ ├─────────────────────────────────────────┤ │ • Project structure │ │ • User preferences │ │ • Past solutions │ │ • Domain knowledge │ └─────────────────────────────────────────┘
- Context Prioritization
Order of importance for context:
Priority Content Action
🔴 Critical Current task, active file Always keep
🟠 High Related files, types Keep if relevant
🟡 Medium Project structure Summarize
🟢 Low History, logs Forget if needed
- Information Retention
What to Remember
✅ Keep in context:
- Current task objective
- File being modified
- Type definitions in use
- Recent error messages
- User preferences
❌ Safe to forget:
- Already processed files
- Resolved errors
- Intermediate calculations
- Verbose logs
- Context Summarization
When context grows too large:
Summarization Rules
- Files: Keep imports, types, key functions
- Errors: Keep message, remove stack trace
- Logs: Keep last 10 lines
- History: Keep decisions, remove process
Example
Before (verbose): "I looked at file A, then file B, noticed pattern X, then explored file C, found issue Y, traced it to..."
After (summarized): "Analyzed A, B, C. Found: pattern X, issue Y in C."
- Session State Pattern
// Conceptual session state interface SessionMemory { // Always retain task: { goal: string; status: 'planning' | 'executing' | 'verifying'; progress: number; };
// Retain while relevant context: { activeFiles: string[]; recentDecisions: string[]; constraints: string[]; };
// Summarize or forget history: { summary: string; keyInsights: string[]; }; }
- Knowledge Retrieval
Before Starting New Task
-
Check: Have I seen this before?
-
Recall: What approach worked?
-
Apply: Use proven patterns
-
Adapt: Modify for current context
-
Memory Hygiene
Per-Turn Cleanup
After completing a step:
- ✅ Task still relevant? Keep
- ❓ Might need later? Summarize
- ❌ No longer needed? Forget
End of Task
-
Extract learnings
-
Update knowledge base
-
Clear working memory
-
Context Window Management
Token Budget Allocation
| Category | Budget |
|---|---|
| System prompt | 10% |
| Task context | 30% |
| Active code | 40% |
| Conversation | 20% |
When Near Limit
- Summarize conversation history
- Remove resolved issues
- Keep only relevant code sections
- Preserve critical context
References
-
Working Memory in LLMs
-
Context Compression