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
  • 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?”
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:
⚠️  Context Warning

Current tokens: 185,000 / 200,000
Compaction will occur soon

ULPI Pre-Compact Hook: ACTIVE
Critical context will be preserved automatically.
2

Pre-Compact Hook Fires

BEFORE compaction happens, the hook triggers:
🎣 Pre-Compact Hook Triggered

Token usage: 195,000 / 200,000
Time until compaction: ~30 seconds

Analyzing conversation for critical context...
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:
# MCP Tool: store-memory (bulk insert)
POST /api/memory/store-bulk
{
  "memories": [
    {
      "content": "Chose OAuth 2.0 over session cookies because...",
      "type": "architecture_decision",
      "importance": 0.95,
      "tags": ["auth", "oauth", "architecture"]
    },
    {
      "content": "Error handling pattern: wrap in try-catch, log to Winston...",
      "type": "code_convention",
      "importance": 0.85,
      "tags": ["error-handling", "conventions"]
    },
    // ... more memories
  ]
}
5

Compaction Proceeds Safely

Hook returns exit code 0 → Compaction allowed:
✓ Memory Snapshot Created

Saved to ULPI Memory:
- 12 architecture decisions
- 8 code conventions
- 3 active tasks
- 5 pending obligations
- 14 key insights

Total: 42 memories preserved

Compaction can now proceed safely.
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:
🧠 Loading Relevant Memories

Project: my-auth-project
Loaded 47 memories:
- 12 architecture decisions
- 8 code conventions
- 3 active tasks (resumed)
- 24 insights and patterns

Claude now has context from previous sessions.
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:
interface SalienceFactors {
  // Content characteristics
  isDecision: boolean;        // Architecture or design decision?
  isConvention: boolean;      // Code pattern or standard?
  isInsight: boolean;         // New learning or discovery?

  // Context signals
  discussionLength: number;   // How long was this discussed?
  userEmphasis: boolean;      // Did user say "important" or "remember"?
  repetition: number;         // How many times mentioned?

  // Impact indicators
  affectsMultipleFiles: boolean;  // Impacts many parts of codebase?
  breakingChange: boolean;         // Changes existing behavior?
  securityRelated: boolean;        // Security implication?

  // Temporal factors
  recentlyDiscussed: boolean;     // Discussed in last hour?
  activeTask: boolean;             // Related to current work?
}

Scoring Examples

Conversation excerpt:
User: "Should we use OAuth or session cookies for authentication?"

Claude: "For your use case, I recommend OAuth 2.0 with JWT tokens because:
1. You need mobile app support (cookies don't work well)
2. Microservices architecture requires stateless auth
3. Third-party integrations planned (OAuth is standard)"

User: "Great point about mobile. Let's go with OAuth. This is important
      for the whole project architecture."
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:
User: "How should we handle errors in this codebase?"

Claude: "Based on your existing code, I see a pattern:
- All async functions wrapped in try-catch
- Errors logged with Winston logger
- User-facing errors return { success: false, error: message }
- System errors return 500 with generic message"

User: "Yes, that's our standard. Always follow this pattern."
Salience factors:
  • ✅ isConvention: true
  • ✅ userEmphasis: “always follow this”
  • ✅ affectsMultipleFiles: true
  • ✅ repetition: 3 (discussed multiple times)
Score: 0.85 → Saved
Conversation excerpt:
User: "The database queries are slow. Any ideas?"

Claude: "I see N+1 query problem in your user loading:
- Each user triggers separate query for posts
- Solution: Use eager loading with JOIN
- Performance improvement: 500ms → 50ms"

User: "Nice catch! Let's implement that."
Salience factors:
  • ✅ isInsight: true
  • ✅ discussionLength: 3 messages
  • ⚠️ affectsMultipleFiles: false (one query)
  • ✅ recentlyDiscussed: true
Score: 0.75 → Saved
Conversation excerpt:
User: "What's the syntax for array map in JavaScript?"

Claude: "array.map(item => item * 2)"

User: "Thanks"
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
{
  "hooks": {
    "preCompact": {
      // Enable/disable the hook
      "enabled": true,

      // Create memory snapshot before compaction
      "createSnapshot": true,

      // Minimum salience to save (0.0-1.0)
      "minImportance": 0.5,

      // Include code snippets in memories
      "includeCode": true,

      // Maximum code snippet length (characters)
      "maxCodeLength": 500,

      // Include active task status
      "includeTasks": true,

      // Include pending obligations
      "includeObligations": true,

      // Memory types to save (filter)
      "saveTypes": [
        "architecture_decision",
        "code_convention",
        "insight",
        "active_task",
        "obligation",
        "code_snippet"
      ],

      // Maximum memories to save per snapshot
      "maxMemoriesPerSnapshot": 100,

      // Block compaction if snapshot fails
      "blockOnFailure": false
    }
  }
}

Salience Threshold Tuning

Recommended settings based on usage:
  • Conservative (Save More)
  • Balanced (Default)
  • Aggressive (Save Less)
{
  "minImportance": 0.4
}
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:
🧠 Memory Loading (Session Start)

Searching ULPI Memory for relevant context:
- Project: my-auth-project
- Agent: claude-code-yourname
- Timeframe: Last 30 days
- Min importance: 0.7

Found 47 relevant memories:
✓ Loaded 12 architecture decisions
✓ Loaded 8 code conventions
✓ Loaded 3 active tasks
✓ Loaded 24 insights

Injecting into Claude's context...
Done.
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

  • Without Pre-Compact Hook
  • With Pre-Compact Hook
9:00 AM - Discuss authentication approach
User: "Should we use OAuth or session cookies?"

Claude: [Long discussion about tradeoffs]

Decision: OAuth 2.0 with JWT tokens
Rationale: Mobile support, stateless, scalable
12:00 PM - Context compacted3:00 PM - New session
User: "Why did we choose OAuth again?"

Claude: "I don't have that information in our current context.
         Could you remind me of the decision and rationale?"
YOU SPEND 15 MINUTES RE-EXPLAINING.

Example 2: Code Convention Retention

  • Without Pre-Compact Hook
  • With Pre-Compact Hook
Week 1 - Establish error handling pattern
User: "How should we handle errors?"

Claude: "Let's use this pattern:
- try-catch around async functions
- Log to Winston
- Return { success, error } objects"

User: "Perfect, use this everywhere."
Week 2 - Context compacted multiple timesWeek 3 - New feature
User: "Add error handling to the new login function"

Claude: [Uses generic error handling, not project convention]

User: "No, use our standard pattern!"

Claude: "What's your standard pattern?"
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:
User: "We're choosing PostgreSQL over MongoDB. This is important
      for the whole project architecture."
Effect: Boosts salience score, ensures preservation
Recommendation: Check stored memories to ensure important context is saved
# List recent memories
ulpi memory search --project my-project --limit 50 --sort recent

# Search for specific topic
ulpi memory search "authentication" --project my-project
Benefit: Catch any missed important context
Recommendation: For extremely critical decisions, store manually to guarantee preservation
ulpi memory store \
  --content "OAuth 2.0 chosen over session cookies for mobile support" \
  --type architecture_decision \
  --importance 1.0 \
  --tags auth,oauth,architecture
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