
context-management
Proactive context window management via token monitoring, intelligent extraction, and selective rehydration. Features predictive budget monitoring, context health indicators, and priority-based retention. Use when approaching token limits or needing to preserve essential context. Complements /transcripts and PreCompact hook with proactive optimization.
- Installs
- 168
- Last 24h
- —
- Trending rank
- —
- Security
- Not audited
Install
Works with every agent the skills CLI supports.
npx skills add https://github.com/rysweet/amplihack --skill context-management- 01
Install
Run the command in your project. The CLI asks which agents to install for.
- 02
Describe the task
Ask your agent for something this skill covers. It loads context-management automatically when relevant.
- 03
Review the result
The agent follows the SKILL.md steps, scripts and references bundled with the skill.
SKILL.md
The instructions your agent reads when the skill is triggered.
Context Management Skill
Purpose
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:
- Predictive Budget Monitoring: Estimate when capacity thresholds will be reached
- Context Health Indicators: Visual indicators for statusline integration
- Priority-Based Retention: Keep requirements and decisions, archive verbose logs
- Burn Rate Tracking: Monitor token consumption velocity for early warnings
When to Use This Skill
- Token monitoring: Check current usage and get recommendations
- Approaching limits: Create snapshots at 70-85% usage
- After compaction: Restore essential context without full conversation
- Long sessions: Preserve key decisions and state proactively
- Complex tasks: Keep requirements and progress accessible
- Context switching: Save state when pausing work
- Team handoffs: Package context for others to continue
- Predictive planning: Get early warnings before capacity is reached
- Session health: Monitor context health for sustained productivity
Quick Start
Check Token Status
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
Create a Snapshot
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
Restore Context
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
List Snapshots
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
Detail Levels
When rehydrating context, choose the appropriate detail level:
- Essential (smallest): Requirements + current state only (~250 tokens)
- Standard (balanced): + key decisions + open items (~800 tokens)
- Comprehensive (complete): + full decisions + tools used + metadata (~1,250 tokens)
Start with essential and upgrade if more context is needed.
Actions
Action: status
Check 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:
ContextStatusobject with usage detailsthreshold_status: 'ok', 'consider', 'recommended', or 'urgent'recommendation: Human-readable action suggestion
Action: snapshot
Create 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:
ContextSnapshotobject with metadata- Snapshot saved to
~/.amplihack/.claude/runtime/context-snapshots/
Action: rehydrate
Restore 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:
- Formatted markdown text with restored context
- Ready to process and continue work
Action: list
List 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:
- List of snapshot metadata dicts
- Includes: id, name, timestamp, size, token_count
Proactive Features (v3.0)
Predictive Budget Monitoring
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:
- Token count at each check interval
- Number of tool uses between checks
- Average tokens consumed per tool use
- Time elapsed between checks
From this data, it estimates:
- Tools remaining until threshold
- Approximate time until threshold
- Whether current task will complete before limit
Context Health Indicators
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
Priority-Based Context Retention
When creating snapshots, the system prioritizes content by importance:
High Priority (Always Retained):
- Original user requirements (first user message)
- Key architectural decisions
- Current implementation state
- Open items and blockers
Medium Priority (Retained in Standard+):
- Tool usage history
- Decision rationales
- Questions and clarifications
Low Priority (Only in Comprehensive):
- Verbose output logs
- Intermediate steps
- Debugging information
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')
Burn Rate Tracking
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 |
Auto-Summarization Triggers
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
Proactive Usage Workflow
Step 1: Monitor Token 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')
Step 2: Create Snapshot at Threshold
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
Step 3: Continue Working
After snapshot creation:
- Continue conversation naturally
- Let Claude Code compact if needed
- Use
/transcriptsfor full history if desired - PreCompact hook saves everything automatically
Step 4: Rehydrate After Compaction
After 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'
)
Integration with Existing Systems
vs. PreCompact Hook
PreCompact Hook (automatic safety net):
- Triggered by Claude Code before compaction
- Saves complete conversation transcript
- Automatic, no user action needed
- Full conversation export to markdown
Context Skill (proactive optimization):
- Triggered by user when monitoring indicates
- Saves intelligent context extraction
- User-initiated, deliberate choice
- Essential context only, not full dump
Relationship: Complementary, not competing. Hook = safety net, Skill = optimization.
vs. /transcripts Command
/transcripts (reactive restoration):
- Restores full conversation after compaction
- Complete history, all messages
- Used when you need everything back
- Reactive recovery tool
Context Skill (proactive preservation):
- Preserves essential context before compaction
- Selective rehydration, not full history
- Used when you want efficient context
- Proactive optimization tool
Relationship: Transcripts for full recovery, skill for efficient management.
Storage Locations
- Snapshots:
~/.amplihack/.claude/runtime/context-snapshots/(JSON) - Transcripts:
~/.amplihack/.claude/runtime/logs/<session_id>/CONVERSATION_TRANSCRIPT.md - No conflicts: Different directories, different purposes
Automatic Management
Context management runs automatically via the post_tool_use hook:
- Monitors token usage every Nth tool use (adaptive frequency)
- Creates snapshots at thresholds (30%, 40%, 50% for 1M models)
- Detects compaction (token drop > 30%)
- Auto-rehydrates after compaction at appropriate level
This happens transparently without user intervention.
Implementation
All context management functionality is provided by:
- Tool:
~/.amplihack/.claude/tools/amplihack/context_manager.py - Hook Integration:
~/.amplihack/.claude/tools/amplihack/context_automation_hook.py - Hook System:
~/.amplihack/.claude/tools/amplihack/hooks/tool_registry.py
See tool documentation for complete API reference and implementation details.
Common Patterns
Pattern 1: Preventive Snapshotting
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')
Pattern 2: Context Switching
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')
Pattern 3: Team Handoff
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
Philosophy Alignment
Ruthless Simplicity
- Four single-purpose components in one tool
- On-demand invocation, no background processes
- Standard library only, no external dependencies
- Clear public API with convenience functions
Single Responsibility
- ContextManager coordinates all operations
- Token monitoring, extraction, rehydration in one place
- No duplicate code or scattered logic
Zero-BS Implementation
- No stubs or placeholders
- All functions work completely
- Real token estimation, not fake
- Actual file operations, not simulated
Trust in Emergence
- User decides when to snapshot, not automatic (unless via hook)
- User chooses detail level, not system
- Proactive choice empowers the user
Tips for Effective Context Management
- Monitor regularly: Check status at natural breakpoints
- Snapshot strategically: At 70-85% or before long operations
- Start minimal: Use essential level first, upgrade if needed
- Name descriptively: Use clear snapshot names for later reference
- List periodically: Review and clean old snapshots
- Combine tools: Use with /transcripts for full recovery option
- Trust emergence: Don't over-snapshot, let context flow naturally
Resources
- Tool:
~/.amplihack/.claude/tools/amplihack/context_manager.py - Hook:
~/.amplihack/.claude/tools/amplihack/context_automation_hook.py - Philosophy:
~/.amplihack/.claude/context/PHILOSOPHY.md - Patterns:
~/.amplihack/.claude/context/PATTERNS.md
Remember
This 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.
FAQ
What does the context-management skill do?
Proactive context window management via token monitoring, intelligent extraction, and selective rehydration. Features predictive budget monitoring, context health indicators, and priority-based retention. Use when approaching token limits or needing to preserve essential context. Complements /transcripts and PreCompact hook with proactive optimization.
How do I install context-management?
Run npx skills add https://github.com/rysweet/amplihack --skill context-management in your project. The skills CLI detects the agents you use and copies the skill into the right folder. For Claude Code you can also place the skill folder at ~/.claude/skills/context-management/ (personal) or .claude/skills/context-management/ (project).
Which AI agents can use context-management?
Any agent that supports the open Agent Skills format (a folder with a SKILL.md file), including Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, Windsurf, Cline, OpenCode and more.
Is context-management safe to install?
No partner security audit is available yet. Skills can run commands and scripts, so read the SKILL.md and any bundled files before installing.
Who maintains context-management?
It is published by rysweet in rysweet/amplihack. It has 168 recorded installs.
More from rysweet/amplihack
cybersecurity-analyst
rysweet/amplihack
Analyzes events through cybersecurity lens using threat modeling, attack surface analysis, defense-in-depth, zero-trust architecture, and risk-based frameworks (CIA triad, STRIDE, MITRE ATT&CK). Provides insights on vulnerabilities, attack vectors, defense strategies, incident response, and security posture. Use when: Security incidents, vulnerability assessments, threat analysis, security architecture, compliance. Evaluates: Confidentiality, integrity, availability, threat actors, attack patterns, controls, residual risk.
lawyer-analyst
rysweet/amplihack
Analyzes events through legal lens using statutory interpretation, case law analysis, legal reasoning, constitutional principles, and multiple legal frameworks (common law, civil law, international law). Provides insights on legal rights, obligations, liabilities, remedies, and compliance requirements. Use when: Legal disputes, contracts, regulations, compliance, rights analysis, liability assessment. Evaluates: Legal obligations, rights, liabilities, remedies, precedent, statutory authority, constitutionality.
psychologist-analyst
rysweet/amplihack
Analyzes events through psychological lens using cognitive psychology, social psychology, developmental psychology, clinical psychology, and neuroscience. Provides insights on behavior, cognition, emotion, motivation, group dynamics, decision-making biases, mental health, and individual differences. Use when: Behavioral patterns, decision-making, group behavior, mental health, leadership, persuasion, trauma, development. Evaluates: Cognitive processes, emotional responses, motivations, biases, group dynamics, personality, mental states.
mermaid-diagram-generator
rysweet/amplihack
Converts architecture descriptions, module specs, or workflow docs into Mermaid diagrams. Use when visualizing brick module relationships, workflows (DDD, investigation), or system architecture. Supports: flowcharts, sequence diagrams, class diagrams, state machines, entity relationship diagrams, and Gantt charts. Generates valid Mermaid syntax for embedding in markdown docs.
pptx
rysweet/amplihack
Presentation creation, editing, and analysis. When Claude needs to work with presentations (.pptx files) for: (1) Creating new presentations, (2) Modifying or editing content, (3) Working with layouts, (4) Adding comments or speaker notes, or any other presentation tasks
economist-analyst
rysweet/amplihack
Analyzes events through economic lens using supply/demand, incentive structures, market dynamics, and multiple schools of economic thought (Classical, Keynesian, Austrian, Behavioral). Provides insights on market impacts, resource allocation, policy implications, and distributional effects. Use when: Economic events, policy changes, market shifts, financial crises, regulatory decisions. Evaluates: Incentives, efficiency, opportunity costs, market failures, systemic risks.
Popular devops & cloud skills
microsoft-foundry
microsoft/azure-skills
Build, deploy, evaluate, optimize, fine-tune, and manage Microsoft Foundry agents, models, and resources end to end. USE FOR: foundry, azd ai agent, azd provision/deploy, hosted agent scaffold/develop/run/deploy/troubleshoot, prompt agent create, managed harness agent create, GitHub Copilot harness agent, create agent, update agent, add tool to agent, invoke agent, agent insights, evaluate agent, batch eval, continuous eval, continuous monitoring, agent CI/CD, optimize prompt, improve prompt, optimize agent instructions, Agent Optimizer scaffold, dataset curation from traces, deploy model, model fine-tuning (SFT/DPO/RFT), Foundry project, RBAC, role assignment, permissions, quota, capacity, region, deployment failure, AI Services, create Foundry resource, knowledge index, customize deployment, onboard, availability, training-data, grader, distillation, large file upload. DO NOT USE FOR: Azure Functions, App Service, general Azure deploy (use azure-deploy), general Azure prep (use azure
azure-diagnostics
microsoft/azure-skills
Debug Azure production issues on Azure using AppLens, Azure Monitor, resource health, and safe triage. WHEN: debug production issues, troubleshoot app service, app service high CPU, app service deployment failure, troubleshoot container apps, container app 503 or target port mismatch, troubleshoot functions, function telemetry missing or stopped after a config/connection-string change, troubleshoot AKS, VM RDP, Linux SSH, VM black screen, can't connect to VM, reset VM password, NSG or firewall blocking, kubectl cannot connect, kube-system/CoreDNS failures, pod pending, crashloop, node not ready, upgrade failures, analyze logs, KQL, insights, image pull failures, cold start issues, health probe failures, resource health, root cause of errors, troubleshoot event hubs, troubleshoot service bus, messaging SDK error, AMQP connection failure, message lock lost, lock renewal during long-running processing, service bus dead letter.
azure-prepare
microsoft/azure-skills
Prepare azd-based Azure projects for deployment: generates azure.yaml, infrastructure (Bicep/Terraform), and Dockerfiles for the Azure Developer CLI (azd) workflow. USE ONLY when the user explicitly wants to use azd as the deployment tool, or the project already has an azure.yaml file. DO NOT USE FOR: non-azd deployments, Python App Service code-only deploys (use python-appservice-deploy), or cross-cloud migration (use azure-cloud-migrate). WHEN: prepare app for azd, create azure.yaml, set up azd infrastructure, modernize app for Azure with azd, deploy with azd, function app, timer trigger, service bus trigger, event-driven function, managed identity, generate Bicep, generate Terraform, create and deploy to Azure.
azure-ai
microsoft/azure-skills
Use for Azure AI: Search, Speech, OpenAI, Document Intelligence. Helps with search, vector/hybrid search, speech-to-text, text-to-speech, transcription, OCR. WHEN: AI Search, query search, vector search, hybrid search, semantic search, speech-to-text, text-to-speech, transcribe, OCR, convert text to speech.
azure-deploy
microsoft/azure-skills
Execute Azure deployments for ALREADY-PREPARED applications that have existing .azure/deployment-plan.md and infrastructure files. DO NOT use this skill when the user asks to CREATE a new application — use azure-prepare instead. This skill runs azd up, azd deploy, terraform apply, and az deployment commands with built-in error recovery. Requires .azure/deployment-plan.md from azure-prepare and validated status from azure-validate. WHEN: "run azd up", "run azd deploy", "execute deployment", "push to production", "push to cloud", "go live", "ship it", "bicep deploy", "terraform apply", "publish to Azure", "launch on Azure". DO NOT USE WHEN: "create and deploy", "build and deploy", "create a new app", "set up infrastructure", "create and deploy to Azure using Terraform" — use azure-prepare for these.
azure-validate
microsoft/azure-skills
Pre-deployment validation for Azure readiness. Run deep checks on configuration, infrastructure (Bicep or Terraform), RBAC role assignments, managed identity permissions, and prerequisites before deploying. WHEN: validate my app, check deployment readiness, run preflight checks, verify configuration, check if ready to deploy, validate azure.yaml, validate Bicep, test before deploying, troubleshoot deployment errors, validate Azure Functions, validate function app, validate serverless deployment, verify RBAC roles, check role assignments, review managed identity permissions, what-if analysis, validate Container Apps deployment.