Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
A comprehensive collection of Agent Skills for context engineering, multi-agent architectures, and production agent systems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
skills/context-degradation/references/patterns.md
1# Context Degradation Patterns: Technical Reference23This document provides technical details on diagnosing and measuring context degradation.45## Attention Distribution Analysis67### U-Shaped Curve Measurement89Measure attention distribution across context positions:1011```python12def measure_attention_distribution(model, context_tokens, query):13"""14Measure how attention varies across context positions.1516Returns distribution showing attention weight by position.17"""18attention_by_position = []1920for position in range(len(context_tokens)):21# Measure model's attention to this position22attention = get_attention_weights(model, context_tokens, query, position)23attention_by_position.append({24"position": position,25"attention": attention,26"is_beginning": position < len(context_tokens) * 0.1,27"is_end": position > len(context_tokens) * 0.9,28"is_middle": True # Will be overwritten29})3031# Classify positions32for item in attention_by_position:33if item["is_beginning"] or item["is_end"]:34item["region"] = "attention_favored"35else:36item["region"] = "attention_degraded"3738return attention_by_position39```4041### Lost-in-Middle Detection4243Detect when critical information falls in degraded attention regions:4445```python46def detect_lost_in_middle(critical_positions, attention_distribution):47"""48Check if critical information is in attention-favored positions.4950Args:51critical_positions: List of positions containing critical info52attention_distribution: Output from measure_attention_distribution5354Returns:55Dictionary with detection results and recommendations56"""57results = {58"at_risk": [],59"safe": [],60"recommendations": []61}6263for pos in critical_positions:64region = attention_distribution[pos]["region"]65if region == "attention_degraded":66results["at_risk"].append(pos)67else:68results["safe"].append(pos)6970# Generate recommendations71if results["at_risk"]:72results["recommendations"].extend([73"Move critical information to attention-favored positions",74"Use explicit markers to highlight critical information",75"Consider splitting context to reduce middle section"76])7778return results79```8081## Context Poisoning Detection8283### Hallucination Tracking8485Track potential hallucinations across conversation turns:8687```python88class HallucinationTracker:89def __init__(self):90self.claims = []91self.verifications = []9293def add_claims(self, text):94"""Extract claims from text for later verification."""95claims = extract_claims(text)96self.claims.extend([{"text": c, "verified": None} for c in claims])9798def verify_claims(self, ground_truth):99"""Verify claims against ground truth."""100for claim in self.claims:101if claim["verified"] is None:102claim["verified"] = check_claim(claim["text"], ground_truth)103104def get_poisoning_indicators(self):105"""106Return indicators of potential context poisoning.107108High ratio of unverified claims suggests poisoning risk.109"""110unverified = sum(1 for c in self.claims if not c["verified"])111verified_false = sum(1 for c in self.claims if c["verified"] == False)112113return {114"unverified_count": unverified,115"false_count": verified_false,116"poisoning_risk": verified_false > 0 or unverified > len(self.claims) * 0.3117}118```119120### Error Propagation Analysis121122Track how errors flow through context:123124```python125def analyze_error_propagation(context, error_points):126"""127Analyze how errors at specific points affect downstream context.128129Returns visualization of error spread and impact assessment.130"""131impact_map = {}132133for error_point in error_points:134# Find all references to content after error point135downstream_refs = find_references(context, after=error_point)136137for ref in downstream_refs:138if ref not in impact_map:139impact_map[ref] = []140impact_map[ref].append({141"source": error_point,142"type": classify_error_type(context[error_point])143})144145# Assess severity146high_impact_areas = [k for k, v in impact_map.items() if len(v) > 3]147148return {149"impact_map": impact_map,150"high_impact_areas": high_impact_areas,151"requires_intervention": len(high_impact_areas) > 0152}153```154155## Distraction Metrics156157### Relevance Scoring158159Score relevance of context elements to current task:160161```python162def score_context_relevance(context_elements, task_description):163"""164Score each context element for relevance to current task.165166Returns scores and identifies high-distraction elements.167"""168task_embedding = embed(task_description)169170scored_elements = []171for i, element in enumerate(context_elements):172element_embedding = embed(element)173relevance = cosine_similarity(task_embedding, element_embedding)174scored_elements.append({175"index": i,176"content_preview": element[:100],177"relevance_score": relevance178})179180# Sort by relevance181scored_elements.sort(key=lambda x: x["relevance_score"], reverse=True)182183# Identify potential distractors184threshold = calculate_relevance_threshold(scored_elements)185distractors = [e for e in scored_elements if e["relevance_score"] < threshold]186187return {188"scored_elements": scored_elements,189"distractors": distractors,190"recommendation": f"Consider removing {len(distractors)} low-relevance elements"191}192```193194## Degradation Monitoring System195196### Context Health Dashboard197198Implement continuous monitoring of context health:199200```python201class ContextHealthMonitor:202def __init__(self, model, context_window_limit):203self.model = model204self.limit = context_window_limit205self.metrics = []206207def assess_health(self, context, task):208"""209Assess overall context health for current task.210211Returns composite score and component metrics.212"""213metrics = {214"token_count": len(context),215"utilization_ratio": len(context) / self.limit,216"attention_distribution": measure_attention_distribution(self.model, context, task),217"relevance_scores": score_context_relevance(context, task),218"age_tokens": count_recent_tokens(context)219}220221# Calculate composite health score222health_score = self._calculate_composite(metrics)223224result = {225"health_score": health_score,226"metrics": metrics,227"status": self._interpret_score(health_score),228"recommendations": self._generate_recommendations(metrics)229}230231self.metrics.append(result)232return result233234def _calculate_composite(self, metrics):235"""Calculate composite health score from components."""236# Weighted combination of metrics237utilization_penalty = min(metrics["utilization_ratio"] * 0.5, 0.3)238attention_penalty = self._calculate_attention_penalty(metrics["attention_distribution"])239relevance_penalty = self._calculate_relevance_penalty(metrics["relevance_scores"])240241base_score = 1.0242score = base_score - utilization_penalty - attention_penalty - relevance_penalty243return max(0, score)244245def _interpret_score(self, score):246"""Interpret health score and return status."""247if score > 0.8:248return "healthy"249elif score > 0.6:250return "warning"251elif score > 0.4:252return "degraded"253else:254return "critical"255```256257### Alert Thresholds258259Configure appropriate alert thresholds:260261```python262CONTEXT_ALERTS = {263"utilization_warning": 0.7, # 70% of context limit264"utilization_critical": 0.9, # 90% of context limit265"attention_degraded_ratio": 0.3, # 30% in middle region266"relevance_threshold": 0.3, # Below 30% relevance267"consecutive_warnings": 3 # Three warnings triggers alert268}269```270271## Recovery Procedures272273### Context Truncation Strategy274275When context degrades beyond recovery, truncate strategically:276277```python278def truncate_context_for_recovery(context, preserved_elements, target_size):279"""280Truncate context while preserving critical elements.281282Strategy:2831. Preserve system prompt and tool definitions2842. Preserve recent conversation turns2853. Preserve critical retrieved documents2864. Summarize older content if needed2875. Truncate from middle if still over target288"""289truncated = []290291# Category 1: Critical system elements (preserve always)292system_elements = extract_system_elements(context)293truncated.extend(system_elements)294295# Category 2: Recent conversation (preserve more)296recent_turns = extract_recent_turns(context, num_turns=10)297truncated.extend(recent_turns)298299# Category 3: Critical documents (preserve key ones)300critical_docs = extract_critical_documents(context, preserved_elements)301truncated.extend(critical_docs)302303# Check size and summarize if needed304while len(truncated) > target_size:305# Summarize oldest category 3 elements306truncated = summarize_oldest(truncated, category="documents")307308# If still too large, truncate oldest turns309if len(truncated) > target_size:310truncated = truncate_oldest_turns(truncated, keep_recent=5)311312return truncated313```314315