Never Lose Context Again: Pre-Compact Memory Snapshots
The most frustrating moment in AI conversations: Claude compacts your context and forgets everything important. That architecture decision you spent 30 minutes discussing? Gone. The specific conventions for your codebase? Forgotten. The active tasks and pending obligations? Lost. ULPI’s pre-compact hook solves this. Before Claude compresses conversation history, the hook automatically creates a memory snapshot of everything that matters. Result: 100% context preservation across sessions.The Problem: Context Compaction Amnesia
Without Pre-Compact Hooks
- The Scenario
- What Gets Lost
- Current Workarounds
Long conversation with Claude Code:
-
9:00 AM: Discuss authentication architecture
- Decided on OAuth 2.0 with JWT tokens
- Custom session management approach
- Specific error handling pattern
-
10:30 AM: Implement login function
- 200 lines of code written
- Tests added
- Edge cases discussed
-
12:00 PM: Context approaching token limit (200K tokens)
- Claude shows warning: “Context will be compacted soon”
- You continue working…
-
12:15 PM: COMPACTION HAPPENS
- Claude compresses conversation to free tokens
- Removes “less important” parts
- Keeps recent code but loses rationale
-
12:30 PM: You ask: “Why did we choose OAuth over session cookies?”
- Claude: “I don’t have that information in our current context. Can you remind me?”
The Solution: Automatic Memory Snapshots
How Pre-Compact Hook Works
1
Detect Imminent Compaction
Claude Code (or your IDE) detects context approaching token limit:
2
Pre-Compact Hook Fires
BEFORE compaction happens, the hook triggers:
3
Intelligent Context Extraction
Hook uses AI to extract important context:What it looks for:
- Architecture decisions and their rationale
- Code patterns and conventions
- Active tasks and their status
- Pending obligations (reviews, acks, etc.)
- Key insights and learnings
- Critical code snippets
- 0.9-1.0: Critical (always save)
- 0.7-0.9: Important (save)
- 0.5-0.7: Moderate (save if space)
- Below 0.5: Skip (not important enough)
4
Create Memory Snapshot
Hook stores high-salience memories to ULPI Memory:
5
Compaction Proceeds Safely
Hook returns exit code Claude compacts conversation, but critical context is saved.
0 → Compaction allowed:6
Next Session: Auto-Load Memories
When you start a new session, session-start hook automatically loads relevant memories:YOU DON’T HAVE TO RE-EXPLAIN ANYTHING.
What Gets Preserved
Memory Types
Architecture Decisions
Examples:
- “We chose PostgreSQL over MongoDB because…”
- “Microservices pattern instead of monolith due to…”
- “REST over GraphQL for this project because…”
Code Conventions
Examples:
- “Use camelCase for functions, PascalCase for classes”
- “Error handling: always wrap in try-catch, log to Winston”
- “Testing: Jest, minimum 80% coverage”
Active Tasks
Examples:
- “Implementing OAuth login (60% complete)”
- “Refactoring auth module (started, 3 files done)”
- “Adding rate limiting middleware (planned)”
Pending Obligations
Examples:
- “Code review required for auth.ts changes”
- “Security audit needed before deployment”
- “Alice requested collaboration on user model”
Key Insights
Examples:
- “Performance bottleneck in N+1 queries”
- “Discovered better pattern for async error handling”
- “Caching strategy reduces DB load by 80%”
Code Snippets
Examples:
- Reusable utility functions
- Complex regex patterns
- Configuration templates
Salience Scoring: How It Works
Automatic Importance Detection
The pre-compact hook uses AI to score each potential memory:Scoring Examples
High Salience (0.9-1.0): Architecture Decision
High Salience (0.9-1.0): Architecture Decision
Conversation excerpt:Salience factors:
- ✅ isDecision: true
- ✅ discussionLength: 5 messages
- ✅ userEmphasis: “important for whole project”
- ✅ affectsMultipleFiles: true (architecture choice)
- ✅ securityRelated: true
High Salience (0.8-0.9): Code Convention
High Salience (0.8-0.9): Code Convention
Conversation excerpt:Salience factors:
- ✅ isConvention: true
- ✅ userEmphasis: “always follow this”
- ✅ affectsMultipleFiles: true
- ✅ repetition: 3 (discussed multiple times)
Moderate Salience (0.6-0.8): Useful Insight
Moderate Salience (0.6-0.8): Useful Insight
Conversation excerpt:Salience factors:
- ✅ isInsight: true
- ✅ discussionLength: 3 messages
- ⚠️ affectsMultipleFiles: false (one query)
- ✅ recentlyDiscussed: true
Low Salience (0.3-0.5): Routine Exchange
Low Salience (0.3-0.5): Routine Exchange
Conversation excerpt:Salience factors:
- ❌ isDecision: false
- ❌ isConvention: false
- ❌ isInsight: false (basic syntax question)
- ❌ discussionLength: 2 messages
- ❌ userEmphasis: none
Configuration
Customize Pre-Compact Behavior
~/.ulpi/config.json
Salience Threshold Tuning
Recommended settings based on usage:- Conservative (Save More)
- Balanced (Default)
- Aggressive (Save Less)
- You frequently need historical context
- Working on complex projects with lots of decisions
- Want comprehensive memory coverage
Integration with ULPI Memory
Pre-compact hooks are powered by ULPI Memory product:Memory Storage
Where snapshots go:- Stored in ULPI Memory with vector embeddings
- Searchable via semantic similarity
- Tagged with project, timestamp, agent identity
- Automatically decayed based on relevance
Memory Retrieval
Session-start hook automatically loads relevant memories:Cross-Session Continuity
Example workflow: Monday 9 AM:- Discuss OAuth architecture
- Implement login function
- Context compacted at noon
- Pre-compact hook saves 15 memories
- New session starts
- Session-start hook loads OAuth memories
- Continue implementing logout function
- Claude remembers OAuth decision without re-explanation
- Another new session
- Session-start hook loads recent memories
- Implement password reset
- Claude still remembers OAuth approach
- Memories still accessible
- Can search: “Why did we choose OAuth?”
- Get instant answer from memory
Real-World Examples
Example 1: Architecture Decision Preservation
- Without Pre-Compact Hook
- With Pre-Compact Hook
9:00 AM - Discuss authentication approach12:00 PM - Context compacted3:00 PM - New sessionYOU SPEND 15 MINUTES RE-EXPLAINING.
Example 2: Code Convention Retention
- Without Pre-Compact Hook
- With Pre-Compact Hook
Week 1 - Establish error handling patternWeek 2 - Context compacted multiple timesWeek 3 - New featureYOU EXPLAIN AGAIN.
Performance
Hook Execution Time
Pre-compact hook is the slowest hook (~200ms), but this is acceptable: Why it’s slower:- Must analyze entire conversation (large context)
- Extract and score potential memories (AI-powered)
- Store multiple memories to API (bulk insert)
- Only fires every 2-3 hours (rare)
- Runs BEFORE compaction (not blocking user)
- Saves 15-30 minutes of re-explanation time
- ROI: 200ms cost → 15 min savings = 4500x return
Memory Snapshot Size
Typical snapshot:- 20-50 memories saved
- Each memory: 100-500 characters
- Total: ~20KB of text
- Storage cost: Negligible
- 80-100 memories
- Total: ~50KB
- Still very small
Best Practices
Mark important decisions explicitly
Mark important decisions explicitly
Recommendation: When making important decisions, say “This is important” or “Remember this”Example:Effect: Boosts salience score, ensures preservation
Review memories periodically
Review memories periodically
Recommendation: Check stored memories to ensure important context is savedBenefit: Catch any missed important context
Manually store critical memories
Manually store critical memories
Recommendation: For extremely critical decisions, store manually to guarantee preservationWhen to use: Business-critical decisions, security choices, compliance requirements
Tune salience threshold for your workflow
Tune salience threshold for your workflow
Recommendation: Experiment with
minImportance settingStart conservative (0.4) → Review what’s saved → Increase if too much (0.6-0.7)Find your sweet spot: Saves important context without cluttering memorySuccess Metrics
Teams using pre-compact hooks report:100% Context Preservation
Critical decisions and conventions never lostDown from 40% preservation without hooks
30 Minutes Saved Daily
Per developer, per dayNo more re-explaining architecture decisions
Seamless Session Continuity
Every session starts with full contextLike you never left the conversation
Better Onboarding
New team members browse memory archivesLearn project decisions and patterns quickly