Proactive context window management via token monitoring, intelligent extraction, and selective rehydration. Features predictive budget monitoring, context health indicators, and priority-based...
This skill enables proactive management of Claude Code's context window through intelligent token monitoring, context extraction, and selective rehydration. Instead of reactive recovery after compaction, this skill helps users preserve essential context before hitting limits and restore it efficiently when needed.
Version 3.0 Enhancements:
User: Check my current token usage
I'll use the context_manager tool to check status:
from context_manager import check_context_status
status = check_context_status(current_tokens=<current_count>)
# Returns: ContextStatus with usage percentage and recommendations
User: Create a context snapshot named "auth-implementation"
I'll use the context_manager tool to create a snapshot:
from context_manager import create_context_snapshot
snapshot = create_context_snapshot(
conversation_data=<conversation_history>,
name="auth-implementation"
)
# Returns: ContextSnapshot with snapshot_id, file_path, and token_count
User: Restore context from snapshot <snapshot_id> at essential level
I'll use the context_manager tool to rehydrate:
from context_manager import rehydrate_from_snapshot
context = rehydrate_from_snapshot(
snapshot_id="20251116_143522",
level="essential" # or "standard" or "comprehensive"
)
# Returns: Formatted context text ready to process
User: List my context snapshots
I'll use the context_manager tool to list snapshots:
from context_manager import list_context_snapshots
snapshots = list_context_snapshots()
# Returns: List of snapshot metadata dicts
When rehydrating context, choose the appropriate detail level:
Start with essential and upgrade if more context is needed.
statusCheck current token usage and get recommendations.
Usage:
from context_manager import check_context_status
status = check_context_status(current_tokens=750000)
print(f"Usage: {status.percentage}%")
print(f"Status: {status.threshold_status}")
print(f"Recommendation: {status.recommendation}")
Returns:
ContextStatus object with usage detailsthreshold_status: 'ok', 'consider', 'recommended', or 'urgent'recommendation: Human-readable action suggestionsnapshotCreate intelligent context snapshot.
Usage:
from context_manager import create_context_snapshot
snapshot = create_context_snapshot(
conversation_data=messages,
name="feature-name" # Optional
)
print(f"Snapshot ID: {snapshot.snapshot_id}")
print(f"Token count: {snapshot.token_count}")
print(f"Saved to: {snapshot.file_path}")
Returns:
ContextSnapshot object with metadata~/.amplihack/.claude/runtime/context-snapshots/rehydrateRestore context from snapshot at specified detail level.
Usage:
from context_manager import rehydrate_from_snapshot
context = rehydrate_from_snapshot(
snapshot_id="20251116_143522",
level="standard" # essential, standard, or comprehensive
)
print(context) # Display restored context
Returns:
listList all available context snapshots.
Usage:
from context_manager import list_context_snapshots
snapshots = list_context_snapshots()
for snapshot in snapshots:
print(f"{snapshot['id']}: {snapshot['name']} ({snapshot['size']})")
Returns:
Instead of just checking current usage, predict when thresholds will be reached:
# The system tracks token burn rate over time
# When checking status, you get predictive insights
status = check_context_status(current_tokens=500000)
# Status includes predictions (when automation is running):
# - Estimated tool uses until 70% threshold
# - Time estimate based on current burn rate
# - Early warning before you hit capacity
# Example output interpretation:
# "At current rate, you'll hit 70% in ~15 tool uses"
# "Consider creating a checkpoint before your next major operation"
How Prediction Works:
The automation tracks:
From this data, it estimates:
Visual indicators for session health, suitable for statusline integration:
| Indicator | Meaning | Usage % | Recommended Action |
|---|---|---|---|
[CTX:OK] |
Healthy | 0-30% | Continue normally |
[CTX:WATCH] |
Monitor | 30-50% | Plan checkpoint |
[CTX:WARN] |
Warning | 50-70% | Create snapshot soon |
[CTX:CRITICAL] |
Critical | 70%+ | Snapshot immediately |
Statusline Integration Example:
# In your statusline script, check context health:
# The automation state file contains health status
# Example statusline addition:
if [ -f ".claude/runtime/context-automation-state.json" ]; then
LAST_PCT=$(jq -r '.last_percentage // 0' .claude/runtime/context-automation-state.json)
if [ "$LAST_PCT" -lt 30 ]; then
echo "[CTX:OK]"
elif [ "$LAST_PCT" -lt 50 ]; then
echo "[CTX:WATCH]"
elif [ "$LAST_PCT" -lt 70 ]; then
echo "[CTX:WARN]"
else
echo "[CTX:CRITICAL]"
fi
fi
When creating snapshots, the system prioritizes content by importance:
High Priority (Always Retained):
Medium Priority (Retained in Standard+):
Low Priority (Only in Comprehensive):
Usage Pattern:
# Create snapshot with priority awareness
snapshot = create_context_snapshot(
conversation_data=messages,
name='feature-checkpoint'
)
# Essential level (~200 tokens): Only high priority content
# Standard level (~800 tokens): High + medium priority
# Comprehensive level (~1250 tokens): Everything
# Start minimal, upgrade as needed:
context = rehydrate_from_snapshot(snapshot_id, level='essential')
Monitor how fast you're consuming context:
# The automation tracks consumption velocity
# Adaptive checking frequency based on burn rate:
# Low burn rate (< 1K tokens/tool): Check every 50 tools
# Medium burn rate (1-5K tokens/tool): Check every 10 tools
# High burn rate (> 5K tokens/tool): Check every 3 tools
# Critical zone (70%+): Check every tool
# This means:
# - Normal development: Minimal overhead (checks rarely)
# - Large file operations: Increased monitoring
# - Approaching limits: Continuous monitoring
Burn Rate Thresholds:
| Burn Rate | Risk Level | Monitoring Frequency |
|---|---|---|
| < 1K/tool | Low | Every 50 tools |
| 1-5K/tool | Medium | Every 10 tools |
| > 5K/tool | High | Every 3 tools |
| Any at 70%+ | Critical | Every tool |
The system automatically creates snapshots before limits are hit:
# Automatic snapshot triggers (already implemented):
# - 30% usage: First checkpoint created
# - 40% usage: Second checkpoint created
# - 50% usage: Third checkpoint created (for 1M models)
# For smaller context windows (< 800K):
# - 55% usage: First checkpoint
# - 70% usage: Second checkpoint
# - 85% usage: Urgent checkpoint
# After compaction detected (30% token drop):
# - Automatically rehydrates from most recent snapshot
# - Uses smart level selection based on previous usage
Periodically check status during long sessions:
status = check_context_status(current_tokens=current)
if status.threshold_status == 'consider':
# Usage at 70%+ - consider creating snapshot
print("Consider creating a snapshot soon")
elif status.threshold_status == 'recommended':
# Usage at 85%+ - snapshot recommended
create_context_snapshot(messages, name='current-work')
elif status.threshold_status == 'urgent':
# Usage at 95%+ - create snapshot immediately
create_context_snapshot(messages, name='urgent-backup')
When 70-85% threshold reached, create a named snapshot:
snapshot = create_context_snapshot(
conversation_data=messages,
name='descriptive-name'
)
# Save snapshot ID for later rehydration
After snapshot creation:
/transcripts for full history if desiredAfter compaction, restore essential context:
# Start minimal
context = rehydrate_from_snapshot(
snapshot_id='20251116_143522',
level='essential'
)
# If more context needed, upgrade to standard
context = rehydrate_from_snapshot(
snapshot_id='20251116_143522',
level='standard'
)
# For complete context, use comprehensive
context = rehydrate_from_snapshot(
snapshot_id='20251116_143522',
level='comprehensive'
)
PreCompact Hook (automatic safety net):
Context Skill (proactive optimization):
Relationship: Complementary, not competing. Hook = safety net, Skill = optimization.
/transcripts (reactive restoration):
Context Skill (proactive preservation):
Relationship: Transcripts for full recovery, skill for efficient management.
~/.amplihack/.claude/runtime/context-snapshots/ (JSON)~/.amplihack/.claude/runtime/logs/<session_id>/CONVERSATION_TRANSCRIPT.mdContext management runs automatically via the post_tool_use hook:
This happens transparently without user intervention.
All context management functionality is provided by:
~/.amplihack/.claude/tools/amplihack/context_manager.py~/.amplihack/.claude/tools/amplihack/context_automation_hook.py~/.amplihack/.claude/tools/amplihack/hooks/tool_registry.pySee tool documentation for complete API reference and implementation details.
Check before long operation and create snapshot if needed:
status = check_context_status(current_tokens=current)
if status.threshold_status in ['recommended', 'urgent']:
create_context_snapshot(messages, name='before-refactoring')
Save state when pausing work on one feature to start another:
# Pausing work on Feature A
create_context_snapshot(messages, name='feature-a-paused')
# [... work on Feature B ...]
# Resume Feature A later
context = rehydrate_from_snapshot('feature-a-snapshot-id', level='standard')
Create comprehensive snapshot for teammate:
snapshot = create_context_snapshot(
messages,
name='handoff-to-alice-api-work'
)
# Share snapshot ID with teammate
# Alice can rehydrate and continue work
~/.amplihack/.claude/tools/amplihack/context_manager.py~/.amplihack/.claude/tools/amplihack/context_automation_hook.py~/.amplihack/.claude/context/PHILOSOPHY.md~/.amplihack/.claude/context/PATTERNS.mdThis skill provides proactive context management through a clean, reusable tool. The tool can be called from skills, commands, and hooks. It complements existing tools (PreCompact hook, /transcripts) rather than replacing them. Use it to maintain clean, efficient context throughout long sessions.
Key Takeaway: Business logic lives in context_manager.py, this skill just tells you how to use it.