context-optimization

athola's avatarfrom athola

Reduce context usage with MECW principles (keep under 50% of total window).Triggers: context pressure, token usage, MECW, context window, optimization,decomposition, workflow splitting, context management, token optimizationUse when: context usage approaches 50% of window, tasks need decomposition,complex multi-step operations planned, context pressure is highDO NOT use when: simple single-step tasks with low context usage.DO NOT use when: already using mcp-code-execution for tool chains.Use this skill BEFORE starting complex tasks. Check context levels proactively.

⭐83stars🔀11forks📁View on GitHub🕐Updated Jan 3, 2026

When & Why to Use This Skill

This Claude skill optimizes context window usage by applying MECW (Minimum Essential Context Window) principles. It proactively monitors token consumption, decomposes complex tasks into manageable sub-workflows, and prevents context overflow, ensuring the AI remains efficient and accurate during long-running or data-intensive operations.

Use Cases

  • Large-Scale Codebase Analysis: Managing context when reading multiple files or deep directory structures to avoid hitting token limits and maintaining high reasoning quality.
  • Complex Multi-step Workflows: Automatically splitting long, intricate tasks into modular sub-tasks coordinated by sub-agents to keep the primary context window clean.
  • Handling Verbose Tool Outputs: Efficiently processing large logs, build outputs, or test results by leveraging file references instead of loading full text into the active context.
  • Token Budgeting for Long Sessions: Proactively monitoring and allocating token usage across different modules to prevent session degradation and extend the duration of productive interactions.
namecontext-optimization
description|
Triggerscontext pressure, token usage, MECW, context window, optimization,
Use whencontext usage approaches 50% of window, tasks need decomposition,
DO NOT use whenalready using mcp-code-execution for tool chains.
categoryconservation
token_budget150
progressive_loadingtrue
- matcher"Bash"
command|
echo "[skillcontext-optimization] === Optimization completed at $(date) ===" >> ${CLAUDE_CODE_TMPDIR:-/tmp}/skill-audit.log
oncetrue
- command|
# Could exportcontext pressure events over time

Table of Contents

Context Optimization Hub

Quick Start

Basic Usage

# Analyze current context usage
python -m conserve.context_analyzer

When to Use

  • Threshold Alert: When context usage approaches 50% of the window.
  • Complex Tasks: For operations requiring multi-file analysis or long tool chains.

Core Hub Responsibilities

  1. Assess context pressure and MECW compliance.
  2. Route to appropriate specialized modules.
  3. Coordinate subagent-based workflows.
  4. Manage token budget allocation across modules.
  5. Synthesize results from modular execution.

Module Selection Strategy

def select_optimal_modules(context_situation, task_complexity):
    if context_situation == "CRITICAL":
        return ['mecw-assessment', 'subagent-coordination']
    elif task_complexity == 'high':
        return ['mecw-principles', 'subagent-coordination']
    else:
        return ['mecw-assessment']

Context Classification

Utilization Status Action
< 30% LOW Continue normally
30-50% MODERATE Monitor, apply principles
> 50% CRITICAL Immediate optimization required

Large Output Handling (Claude Code 2.1.2+)

Behavior Change: Large bash command and tool outputs are saved to disk instead of being truncated; file references are provided for access.

Impact on Context Optimization

Scenario Before 2.1.2 After 2.1.2
Large test output Truncated, partial data Full output via file reference
Verbose build logs Lost after 30K chars Complete, accessible on-demand
Context pressure Less from truncation Same - only loaded when read

Best Practices

  • Avoid pre-emptive reads: Large outputs are referenced, not automatically loaded into context.
  • Read selectively: Use head, tail, or grep on file references.
  • Leverage full data: Quality gates can access complete test results via files.
  • Monitor growth: File references are small, but reading the full files adds to context.

Integration Points

  • Token Conservation: Receives usage strategies, returns MECW-compliant optimizations.
  • CPU/GPU Performance: Aligns context optimization with resource constraints.
  • MCP Code Execution: Delegates complex patterns to specialized MCP modules.

Resources

  • MECW Theory: See modules/mecw-principles.md for core concepts and the 50% rule.
  • Context Analysis: See modules/mecw-assessment.md for risk identification.
  • Workflow Delegation: See modules/subagent-coordination.md for decomposition patterns.

Troubleshooting

Common Issues

If context usage remains high after optimization, check for large files that were read entirely rather than selectively. If MECW assessments fail, ensure that your environment provides accurate token count metadata. For permission errors when writing output logs to /tmp, verify that the project's temporary directory is writable.