Skill
Context Engineering Skill
Metadata
name: context-engineering
version: 1.0.0
category: orchestration
status: active
priority: P0
derived_from: Claude Operating Preferences v6.0
When to Use This Skill
Use this skill when:
- Managing context in long-running sessions
- Preventing duplicate work across agents
- Configuring memory tools for autonomous tasks
- Optimizing context window utilization
Core Insight
From LangChain: "Context engineering is the #1 job" for effective AI systems.
Context engineering encompasses:
- What information to include
- How to structure it
- When to compress or summarize
- How to prevent duplication
Core Patterns
Memory Tool Configuration
from dataclasses import dataclass
from typing import Optional, Dict, Any
@dataclass
class MemoryToolConfig:
"""Configure context-management-2025-06-27 beta feature"""
enabled: bool = True
checkpoint_interval: int = 10 # Messages between checkpoints
max_context_percent: float = 0.8 # Trigger compression at 80%
preserve_decisions: bool = True
preserve_code_patterns: bool = True
def get_api_headers(self) -> Dict[str, str]:
"""Get headers for API calls with memory enabled"""
if not self.enabled:
return {}
return {
"anthropic-beta": "context-management-2025-06-27"
}
def get_beta_list(self) -> list:
"""Get beta features list for API config"""
if not self.enabled:
return []
return ["context-management-2025-06-27"]
Context Engineering Configuration
@dataclass
class ContextEngineeringConfig:
"""Best practices for context management"""
# Prevent duplicate work
track_completed_tasks: bool = True
task_deduplication: bool = True
# Division of labor
explicit_agent_roles: bool = True
clear_handoff_points: bool = True
# Context preservation
checkpoint_protocol: str = "summary" # summary, full, delta
preserve_reasoning: bool = True
# Compression triggers
compression_threshold: float = 0.8
compression_strategy: str = "smart" # smart, aggressive, conservative
def should_compress(self, context_utilization: float) -> bool:
"""Determine if context compression is needed"""
return context_utilization >= self.compression_threshold
def get_compression_prompt(self) -> str:
"""Get prompt for context compression"""
if self.compression_strategy == "aggressive":
return """Compress context to essential facts only:
- Key decisions made
- Current task status
- Critical constraints
Remove all explanatory text."""
elif self.compression_strategy == "conservative":
return """Summarize context preserving:
- All decisions with rationale
- Code patterns identified
- Error solutions found
- Task progress details"""
else: # smart
return """Intelligently compress context:
- Preserve recent context fully
- Summarize older context
- Keep all decisions
- Keep error solutions
- Remove redundant explanations"""
Context Window Manager
@dataclass
class ContextWindowManager:
"""Manage context window efficiently"""
max_tokens: int = 200000 # Opus 4.5 context window
current_tokens: int = 0
warning_threshold: float = 0.8
critical_threshold: float = 0.95
# Tracked content
system_prompt_tokens: int = 0
conversation_tokens: int = 0
tool_result_tokens: int = 0
def update_usage(
self,
system: int = 0,
conversation: int = 0,
tools: int = 0
):
"""Update token usage counts"""
self.system_prompt_tokens += system
self.conversation_tokens += conversation
self.tool_result_tokens += tools
self.current_tokens = (
self.system_prompt_tokens +
self.conversation_tokens +
self.tool_result_tokens
)
@property
def utilization(self) -> float:
"""Current context utilization (0.0-1.0)"""
return self.current_tokens / self.max_tokens
@property
def remaining_tokens(self) -> int:
"""Tokens remaining in context window"""
return self.max_tokens - self.current_tokens
def get_status(self) -> dict:
"""Get context window status"""
return {
"utilization": round(self.utilization * 100, 1),
"current_tokens": self.current_tokens,
"max_tokens": self.max_tokens,
"remaining": self.remaining_tokens,
"breakdown": {
"system_prompt": self.system_prompt_tokens,
"conversation": self.conversation_tokens,
"tool_results": self.tool_result_tokens
},
"status": self._get_status_level()
}
def _get_status_level(self) -> str:
"""Get status level based on utilization"""
if self.utilization >= self.critical_threshold:
return "critical"
elif self.utilization >= self.warning_threshold:
return "warning"
else:
return "healthy"
def should_warn(self) -> bool:
"""Check if warning should be issued"""
return self.utilization >= self.warning_threshold
def should_compress(self) -> bool:
"""Check if compression is needed"""
return self.utilization >= self.critical_threshold
Duplicate Work Prevention
from typing import Set, Optional
import hashlib
class TaskDeduplicator:
"""Prevent duplicate work across agents"""
def __init__(self):
self.completed_tasks: Set[str] = set()
self.in_progress_tasks: Set[str] = set()
def _hash_task(self, task_description: str) -> str:
"""Create hash of task for deduplication"""
normalized = task_description.lower().strip()
return hashlib.md5(normalized.encode()).hexdigest()[:12]
def is_duplicate(self, task_description: str) -> bool:
"""Check if task has been completed"""
task_hash = self._hash_task(task_description)
return task_hash in self.completed_tasks
def is_in_progress(self, task_description: str) -> bool:
"""Check if task is currently being worked on"""
task_hash = self._hash_task(task_description)
return task_hash in self.in_progress_tasks
def start_task(self, task_description: str) -> bool:
"""Mark task as started, return False if duplicate"""
if self.is_duplicate(task_description):
return False
task_hash = self._hash_task(task_description)
self.in_progress_tasks.add(task_hash)
return True
def complete_task(self, task_description: str):
"""Mark task as completed"""
task_hash = self._hash_task(task_description)
self.in_progress_tasks.discard(task_hash)
self.completed_tasks.add(task_hash)
def get_status(self) -> dict:
"""Get deduplication status"""
return {
"completed_count": len(self.completed_tasks),
"in_progress_count": len(self.in_progress_tasks),
"completed_hashes": list(self.completed_tasks),
"in_progress_hashes": list(self.in_progress_tasks)
}
Explicit Division of Labor
@dataclass
class AgentHandoff:
"""Define clear handoff between agents"""
from_agent: str
to_agent: str
context_to_pass: list # What context to include
context_to_omit: list # What to leave out
handoff_summary: str # Brief summary for receiving agent
def generate_handoff_prompt(self) -> str:
"""Generate prompt for receiving agent"""
return f"""
## Handoff from {self.from_agent}
### Summary
{self.handoff_summary}
### Context Provided
{chr(10).join(f'- {c}' for c in self.context_to_pass)}
### Not Included (intentionally)
{chr(10).join(f'- {c}' for c in self.context_to_omit)}
### Your Task
Continue from where {self.from_agent} left off.
"""
API Configuration for Long Tasks
def get_long_task_config(hours: int = 30) -> dict:
"""Get API config for autonomous long-running tasks"""
return {
"model": "claude-opus-4-5-20251101",
"max_tokens": 32000,
"thinking": {
"type": "enabled",
"budget_tokens": 64000
},
"betas": [
"interleaved-thinking-2025-05-14",
"context-management-2025-06-27"
]
}
Context Checkpoint Protocol
@dataclass
class ContextCheckpoint:
"""Checkpoint for context preservation"""
timestamp: str
context_utilization: float
decisions_made: list
code_patterns: list
errors_solved: list
current_task: str
thinking_summary: Optional[str] = None
def to_summary(self) -> str:
"""Generate summary for context restoration"""
return f"""
## Context Checkpoint ({self.timestamp})
### Utilization: {self.context_utilization * 100:.1f}%
### Decisions Made
{chr(10).join(f'- {d}' for d in self.decisions_made)}
### Code Patterns Identified
{chr(10).join(f'- {p}' for p in self.code_patterns)}
### Errors Solved
{chr(10).join(f'- {e}' for e in self.errors_solved)}
### Current Task
{self.current_task}
### Thinking Summary
{self.thinking_summary or 'N/A'}
"""
Success Output
When this skill completes successfully, output:
✅ SKILL COMPLETE: context-engineering
Completed:
- [x] Context window manager initialized and monitoring utilization
- [x] Memory tool configured with appropriate beta features
- [x] Task deduplication preventing duplicate work
- [x] Context compression strategy configured
- [x] Agent handoffs documented with clear context boundaries
Context Status:
- Utilization: [percentage]% ([healthy|warning|critical])
- Compression strategy: [smart|aggressive|conservative]
- Tasks deduplicated: [count]
- Checkpoints created: [count]
- Memory tool: [enabled|disabled]
Outputs:
- Context checkpoints saved
- Task deduplication database updated
- Agent handoff documentation complete
Completion Checklist
Before marking this skill as complete, verify:
- MemoryToolConfig initialized with correct beta features
- Context utilization monitoring active (<80% target)
- Task deduplication tracking completed and in-progress tasks
- Compression strategy selected and configured
- Context checkpoints created at appropriate intervals
- Agent handoffs documented with context to pass/omit
- No duplicate work detected in task history
- Context window status healthy (<95% utilization)
Failure Indicators
This skill has FAILED if:
- ❌ Context window exceeded 95% utilization (critical threshold)
- ❌ Duplicate tasks executed (deduplication not working)
- ❌ Agent handoffs lost context (incomplete handoff documentation)
- ❌ Memory tool not enabled when configured
- ❌ Context compression triggered too late (already at 100%)
- ❌ Task tracking not preventing redundant work
- ❌ Context checkpoints missing critical decisions
- ❌ Context utilization not monitored or reported
When NOT to Use
Do NOT use this skill when:
- Simple single-turn conversations (no context management needed)
- Context window utilization <50% (premature optimization)
- No multi-agent coordination (single agent workflows)
- Short-lived tasks (<10 messages)
- No task tracking needed (exploratory work)
- Memory tool beta not available in API version
- Debugging context issues (adds complexity)
Use instead:
- Simple conversation flow for single-turn tasks
- Standard agent workflow for low context usage
- Direct tool calls without context tracking
- Manual context summaries for debugging
- Session export for context review
Anti-Patterns (Avoid)
| Anti-Pattern | Problem | Solution |
|---|---|---|
| Ignore context limits | Conversation crashes at 100% | Monitor utilization, compress proactively |
| Over-compression | Lose critical context, repeat work | Use smart compression, preserve decisions |
| No deduplication | Repeat same work multiple times | Track completed tasks, check before executing |
| Missing checkpoints | Can't restore context after failure | Checkpoint at regular intervals (every 10 messages) |
| Incomplete handoffs | Agents lose context during handoff | Document what to pass and what to omit |
| No utilization monitoring | Surprise context overflow | Monitor utilization continuously |
| Wrong compression strategy | Too aggressive or too conservative | Start with smart, adjust based on results |
| Memory tool misconfigured | Beta features not enabled | Verify beta headers in API calls |
Principles
This skill embodies CODITECT principles:
- #1 Recycle → Extend → Re-Use → Create - Reuses context, prevents duplicate work
- #2 First Principles - Understands WHY context management is critical for AI systems
- #3 Keep It Simple - Simple configuration, clear compression strategies
- #4 Separation of Concerns - Separates context tracking from task execution
- #5 Eliminate Ambiguity - Explicit checkpoint protocol, clear handoff documentation
- #6 Clear, Understandable, Explainable - Context status always visible and reportable
- #8 No Assumptions - Verifies context before executing, tracks explicitly
- #10 Provide Context - Entire skill dedicated to context management
Full Standard: CODITECT-STANDARD-AUTOMATION.md
Related Components
agents/context-window-manager.md- Agent for context managementhooks/context-window-warning- Warning hookcommands/cx.md- Context capture commandcommands/cxq.md- Context query command
Version: 1.0.0 | Updated: 2026-01-04 | Author: CODITECT Team