Never Lose Work to Compaction: Threshold-Based Context Backups for Claude Code
Proactively backup your Claude Code session before compaction hits. StatusLine monitoring and threshold triggers keep your work safe.
You're 4 hours into a complex implementation. Context hits 83%. Auto-compaction fires. Suddenly Claude doesn't remember the specific error message you debugged, the exact function signatures you discussed, or the architectural decisions that led to your current approach.
The summary captures the gist. The precision is gone.
Here's the alternative: backup your session at 30%, 15%, and 5% remaining - before compaction destroys your context. When compaction hits, you have a structured markdown file with every user request, file modification, and key decision preserved.
Why StatusLine Is the Only Solution
Most Claude Code hooks don't receive context metrics. PreToolUse, PostToolUse, Stop - none of them know how much context you've consumed.
StatusLine is different. It receives a JSON payload on every turn with context_window.remaining_percentage - live data showing exactly how much room you have left.
This is the ONLY mechanism in Claude Code that provides real-time context visibility. Without it, you're flying blind until compaction hits.
The 16.5% Buffer Calculation
Here's what trips people up: that remaining_percentage field includes the 16.5% autocompact buffer that you can't actually use. (This was previously 22.5% when the buffer was 45K tokens -- it was recently reduced to 33K.)
When StatusLine reports 25% remaining, you don't have 25% before compaction. You have 8.5%.
The threshold-based backup system accounts for this. When we say "backup at 30%", we mean 30% free-until-compaction - which is actually 46.5% remaining in the raw metric.
The Threshold Concept
Auto-compaction is reactive - it fires when you've already used too much context, then throws away detail in the summarization process.
Threshold-based backup is proactive. Set thresholds at meaningful points:
| Threshold | When It Fires | Purpose |
|---|---|---|
| 30% | ~60K tokens free until compact | First warning, full backup |
| 15% | ~30K tokens free until compact | Getting critical, updated backup |
| 5% | ~10K tokens free until compact | Last chance before compaction |
| Under 5% | Every context decrease | Continuous backup mode |
Why these numbers? At 30%, you still have substantial working room but should capture state. At 15%, compaction is approaching. Below 5%, every turn could be your last - so backup continuously.
Three-File Architecture
A production backup system needs clean separation of concerns. We use three files:
backup-core.mjs - The Engine
This file handles everything about creating backups:
- Transcript parsing: Reads the JSONL transcript file and extracts user requests, file modifications, tasks, and Claude's key responses
- Markdown formatting: Structures the data as a readable markdown file
- File operations: Saves numbered backups with timestamps
- State management: Tracks which session is active and what the current backup path is
The key insight: backups should be structured, not raw dumps. The markdown format groups information logically so you can quickly find what you need when recovering.
statusline-monitor.mjs - The Detector
This runs on every turn via StatusLine. Its job:
- Calculate true "free until compaction" percentage
- Check if any threshold was crossed (going down)
- Trigger
backup-corewhen thresholds cross - Display formatted status with warning indicators
The output format tells you exactly where you stand:
That second line? It's the file you'll load after compaction. No hunting through directories.
conv-backup.mjs - The Safety Net
PreCompact hooks fire right before compaction happens - your last chance to capture state. This file triggers backup-core with precompact_auto or precompact_manual as the trigger reason.
Think of it as the emergency backup. StatusLine-based thresholds are proactive; PreCompact is reactive but still better than losing everything.
Configuration
The system requires two settings.json entries:
The async: true on PreCompact matters - backups shouldn't slow down the compaction process.
Backup File Format
Backups use numbered filenames with human-readable timestamps:
Inside, you get a structured summary:
This isn't a raw transcript. It's a structured summary that tells you what happened, what changed, and what's still pending.
The Recovery Workflow
When compaction happens:
- StatusLine shows backup path: You see exactly which file has your latest backup
- Run /clear: Start a fresh session (cleaner than continuing with compacted context)
- Load the backup: Read the markdown file to restore context
- Continue work: Claude now has structured context about what you were doing
The alternative - working with compacted context - means Claude has a summary of your session but has lost the specifics. Loading a structured backup gives you those specifics back.
Why /clear Instead of Continuing?
After compaction, you have two types of context:
- Compaction summary: Auto-generated, lossy, captures the gist
- Loaded backup: Structured, detailed, captures specifics
Having both can confuse things. The summary might contradict details in the backup. Starting fresh with /clear and loading only the backup gives you cleaner, more reliable context.
State Tracking
Both StatusLine and PreCompact hooks update a shared state file:
This lets the StatusLine monitor know:
- Which session it's tracking (to reset thresholds when sessions change)
- What the last context percentage was (to detect threshold crossings)
- Where the current backup lives (to display in the statusline)
Transcript Parsing Details
The backup system parses Claude Code's JSONL transcript files to extract meaningful data. Here's what it captures:
| Data Type | How It's Extracted |
|---|---|
| User Requests | Messages where type === "user" |
| Files Modified | Write/Edit tool calls with file_path |
| Tasks Created | TaskCreate tool calls |
| Tasks Completed | TaskUpdate with status === "completed" |
| Sub-Agent Calls | Task tool invocations |
| Skills Loaded | Skill tool calls |
| MCP Tool Usage | Tool names starting with mcp__ |
| Build/Test Runs | Bash commands containing build/test/npm/pnpm |
The parser filters out noise - tool results, system messages, single-character inputs - to focus on what actually matters for session recovery.
Why This Beats Manual Tracking
You could manually copy important context into a file as you work. But you won't. You're focused on the implementation, not on documentation.
The threshold system runs automatically. Every time context drops through a threshold, you get an updated backup without thinking about it. The cognitive load is zero.
And the backups are structured. Not a raw paste of conversation, but organized sections you can scan quickly.
Comparison: Auto-Compaction vs Threshold Backup
| Aspect | Auto-Compaction | Threshold Backup + /clear |
|---|---|---|
| When it happens | At ~83.5% usage | At configurable thresholds |
| What's preserved | Lossy summary | Structured markdown with full detail |
| Control | None (hardcoded) | You choose thresholds |
| Recovery | Continue with summary | Load specific backup file |
| Specifics retained | Only what fits summary | Everything in backup |
Auto-compaction is the default because most users don't set up backup systems. But if you're working on complex, multi-hour sessions where precision matters, threshold-based backup gives you much better recovery options.
Key Takeaways
- StatusLine is the only live context monitor - Other hooks don't get token counts
- Raw percentage includes 16.5% buffer - Calculate true "free until compact"
- Threshold backups are proactive - Capture state before compaction, not during
- Structured backups beat raw dumps - Parse transcripts into organized markdown
- Three-file architecture - Clean separation between detection, backup logic, and triggers
- Recovery workflow: /clear + load - Cleaner than mixing compacted context with backup
Get the Full Implementation
The concepts in this guide translate to a working three-file system included in the ClaudeFast Code Kit:
- Complete
backup-core.mjswith transcript parsing and markdown formatting - StatusLine monitor with threshold detection and visual indicators
- PreCompact hook for emergency backups
- State management across sessions
The full implementation handles edge cases - session changes, transcript discovery, numbered backup sequences, and the critical buffer calculation.
Get ClaudeFast Code Kit to add threshold-based backups to your Claude Code workflow.
Related Resources
- Context Buffer Management - Why the 33K-45K buffer exists
- Claude Code Hooks Guide - All 12 hook types explained
- Context Engineering - Strategic context usage
- Session Lifecycle Hooks - Setup and cleanup automation
Last updated on