Skip to main content

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
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?”
YOU JUST EXPLAINED THIS 3 HOURS AGO.

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
Salience scoring: Each potential memory rated 0.0-1.0
  • 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 0 → Compaction allowed:
Claude compacts conversation, but critical context is saved.
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…”
Salience: Typically 0.9-1.0 (very high)Why important: These guide all future development

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”
Salience: 0.8-0.9 (high)Why important: Ensures consistency across codebase

Active Tasks

Examples:
  • “Implementing OAuth login (60% complete)”
  • “Refactoring auth module (started, 3 files done)”
  • “Adding rate limiting middleware (planned)”
Salience: 0.8-0.95 (high)Why important: Can resume exactly where you left off

Pending Obligations

Examples:
  • “Code review required for auth.ts changes”
  • “Security audit needed before deployment”
  • “Alice requested collaboration on user model”
Salience: 0.9-1.0 (very high)Why important: Don’t forget commitments

Key Insights

Examples:
  • “Performance bottleneck in N+1 queries”
  • “Discovered better pattern for async error handling”
  • “Caching strategy reduces DB load by 80%”
Salience: 0.7-0.9 (moderate to high)Why important: Learnings that improve future work

Code Snippets

Examples:
  • Reusable utility functions
  • Complex regex patterns
  • Configuration templates
Salience: 0.6-0.8 (moderate)Why important: Avoid reinventing the wheel

Salience Scoring: How It Works

Automatic Importance Detection

The pre-compact hook uses AI to score each potential memory:

Scoring Examples

Conversation excerpt:
Salience factors:
  • ✅ isDecision: true
  • ✅ discussionLength: 5 messages
  • ✅ userEmphasis: “important for whole project”
  • ✅ affectsMultipleFiles: true (architecture choice)
  • ✅ securityRelated: true
Score: 0.95 → Definitely saved
Conversation excerpt:
Salience factors:
  • ✅ isConvention: true
  • ✅ userEmphasis: “always follow this”
  • ✅ affectsMultipleFiles: true
  • ✅ repetition: 3 (discussed multiple times)
Score: 0.85 → Saved
Conversation excerpt:
Salience factors:
  • ✅ isInsight: true
  • ✅ discussionLength: 3 messages
  • ⚠️ affectsMultipleFiles: false (one query)
  • ✅ recentlyDiscussed: true
Score: 0.75 → Saved
Conversation excerpt:
Salience factors:
  • ❌ isDecision: false
  • ❌ isConvention: false
  • ❌ isInsight: false (basic syntax question)
  • ❌ discussionLength: 2 messages
  • ❌ userEmphasis: none
Score: 0.35 → NOT saved (generic knowledge)

Configuration

Customize Pre-Compact Behavior

~/.ulpi/config.json

Salience Threshold Tuning

Recommended settings based on usage:
Effect: Saves more memories, including moderately useful onesUse when:
  • You frequently need historical context
  • Working on complex projects with lots of decisions
  • Want comprehensive memory coverage
Tradeoff: More memories = more storage, slightly slower session-start

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:
You start every session with full context.

Cross-Session Continuity

Example workflow: Monday 9 AM:
  • Discuss OAuth architecture
  • Implement login function
  • Context compacted at noon
  • Pre-compact hook saves 15 memories
Monday 3 PM:
  • New session starts
  • Session-start hook loads OAuth memories
  • Continue implementing logout function
  • Claude remembers OAuth decision without re-explanation
Tuesday 9 AM:
  • Another new session
  • Session-start hook loads recent memories
  • Implement password reset
  • Claude still remembers OAuth approach
One week later:
  • Memories still accessible
  • Can search: “Why did we choose OAuth?”
  • Get instant answer from memory

Real-World Examples

Example 1: Architecture Decision Preservation

9:00 AM - Discuss authentication approach
12:00 PM - Context compacted3:00 PM - New session
YOU SPEND 15 MINUTES RE-EXPLAINING.

Example 2: Code Convention Retention

Week 1 - Establish error handling pattern
Week 2 - Context compacted multiple timesWeek 3 - New feature
YOU 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)
Why 200ms is acceptable:
  • 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
Large snapshot (major session):
  • 80-100 memories
  • Total: ~50KB
  • Still very small

Best Practices

Recommendation: When making important decisions, say “This is important” or “Remember this”Example:
Effect: Boosts salience score, ensures preservation
Recommendation: Check stored memories to ensure important context is saved
Benefit: Catch any missed important context
Recommendation: For extremely critical decisions, store manually to guarantee preservation
When to use: Business-critical decisions, security choices, compliance requirements
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 memory

Success 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

Next Steps

ULPI Memory Product

Explore the full Memory product powering pre-compact snapshots

Use Cases

See how teams use hooks for perfect context preservation

Lifecycle Events

Learn about all 8 hooks and when they fire

Performance Metrics

Detailed analysis of hook latency and optimization