MCP service that gives AI agents self-awareness about their cognitive state. Every long-running AI agent suffers from context rot — measurable performance degradation as the context window fills up. Research from Chroma, Stanford ("lost-in-the-middle"), and Redis confirms this is the 1 practical failure mode in production agent systems. An agent experiencing context rot doesn't know it's degrading
Add this skill
npx mdskills install milos-product-maker/context-rot-detectionProvides agents with real-time cognitive health monitoring based on context utilization and research-backed metrics
1# Context Rot Detection23MCP service that gives AI agents self-awareness about their cognitive state.45Every long-running AI agent suffers from **context rot** — measurable performance degradation as the context window fills up. Research from [Chroma](https://research.trychroma.com/context-rot), [Stanford](https://arxiv.org/abs/2307.03172) ("lost-in-the-middle"), and [Redis](https://redis.io/blog/context-rot/) confirms this is the #1 practical failure mode in production agent systems.67An agent experiencing context rot doesn't *know* it's degrading — it just starts making worse decisions. This tool gives agents **real-time visibility into their own cognitive health**.89## Features1011- **Health score (0–100)** based on token utilization, retrieval accuracy, and session fatigue12- **Model-specific degradation curves** for 15+ curated models (Claude, GPT, Gemini, o-series)13- **Auto-resolves any HuggingFace model** — pass a repo ID like `meta-llama/Llama-3.1-70B` and the context window is detected automatically, with results cached in SQLite14- **Lost-in-the-middle risk scoring** based on Stanford research15- **Tool-call burden** and **session fatigue** analysis16- **Actionable recovery recommendations** — compact context, offload to memory, checkpoint, break into subtasks17- **Per-agent health history** tracking (SQLite)18- **Service-wide utilization statistics**1920## Quick Start2122### npx (zero install)2324```bash25npx context-rot-detection26```2728### npm (global install)2930```bash31npm install -g context-rot-detection32context-rot-detection33```3435## MCP Client Configuration3637### Claude Code3839Add to `.mcp.json` in your project root:4041```json42{43 "mcpServers": {44 "context-rot-detection": {45 "command": "npx",46 "args": ["-y", "context-rot-detection"],47 "env": {48 "HEALTH_HISTORY_DB": "./health.db"49 }50 }51 }52}53```5455### Claude Desktop5657Add to `claude_desktop_config.json`:5859```json60{61 "mcpServers": {62 "context-rot-detection": {63 "command": "npx",64 "args": ["-y", "context-rot-detection"],65 "env": {66 "HEALTH_HISTORY_DB": "/path/to/health.db"67 }68 }69 }70}71```7273### Docker7475```json76{77 "mcpServers": {78 "context-rot-detection": {79 "command": "docker",80 "args": [81 "run", "-i", "--rm",82 "-v", "context-rot-data:/data",83 "ghcr.io/milos-product-maker/context-rot-detection:latest"84 ]85 }86 }87}88```8990## Configuration9192| Environment Variable | Description | Default |93|---|---|---|94| `HEALTH_HISTORY_DB` | Path to SQLite database for health history. Use `:memory:` for ephemeral storage. | `:memory:` |95| `LOG_FILE` | Path to append structured JSON log lines. Omit to disable file logging. | *(none)* |9697## Tools9899### `check_my_health`100101Analyze the current context window health. Call this periodically during long sessions or before critical decisions.102103**Parameters:**104105| Parameter | Type | Required | Description |106|---|---|---|---|107| `token_count` | integer | Yes | Current estimated token count in context window |108| `model` | string | No | LLM model identifier — a curated name (e.g., `claude-opus-4`, `gpt-4o`), a HuggingFace repo ID (e.g., `meta-llama/Llama-3.1-70B`), or any string (falls back to conservative defaults) |109| `session_duration_minutes` | integer | No | How long this session has been running |110| `tool_calls_count` | integer | No | Number of tool calls made in this session |111| `context_summary` | string | No | Brief summary of current task and recent actions |112| `agent_id` | string | No | Unique agent identifier for history tracking |113114**Example response:**115116```json117{118 "health_score": 62,119 "status": "warning",120 "token_utilization": {121 "current": 155000,122 "max_effective": 170000,123 "percentage": 91.2,124 "danger_zone_starts_at": 170000125 },126 "quality_estimate": {127 "retrieval_accuracy": "degrading",128 "middle_content_risk": "high",129 "estimated_hallucination_risk": "moderate"130 },131 "session_fatigue": {132 "tool_call_burden": "moderate",133 "session_length_risk": "low",134 "recommendation": "Consider breaking into sub-tasks if complexity increases."135 },136 "recommendations": [137 {138 "priority": "high",139 "action": "compact_context",140 "reason": "You are approaching the effective quality threshold. Summarize older context and remove completed task details.",141 "estimated_quality_gain": 15142 },143 {144 "priority": "high",145 "action": "offload_to_memory",146 "reason": "High risk of lost-in-the-middle effect. Store critical information to external memory before it is effectively lost.",147 "estimated_quality_gain": 8148 }149 ]150}151```152153### `get_health_history`154155Retrieve health check history for a specific agent.156157**Parameters:**158159| Parameter | Type | Required | Description |160|---|---|---|---|161| `agent_id` | string | Yes | Unique agent identifier |162| `limit` | integer | No | Max records to return (default: 20, max: 100) |163164### `get_service_stats`165166Get service-wide utilization statistics. No parameters required.167168Returns total calls, unique agents, average health score, model distribution, status distribution, and recent activity (last hour / last 24h).169170## Supported Models171172| Model | Max Tokens | Danger Zone | Middle-Loss Risk |173|---|---|---|---|174| `claude-opus-4-5` | 200K | 175K | Low |175| `claude-opus-4` | 200K | 170K | Low |176| `claude-sonnet-4` | 200K | 165K | Low |177| `claude-3.7-sonnet` | 200K | 160K | Low–Medium |178| `claude-3.5-sonnet` | 200K | 152K | Medium |179| `claude-haiku-3.5` | 200K | 130K | Medium |180| `gpt-4.1` | 1M | 500K | Medium |181| `gpt-4.1-mini` | 1M | 450K | Medium |182| `gpt-4o` | 128K | 105K | Medium |183| `gpt-4o-mini` | 128K | 95K | Medium–High |184| `o3` | 200K | 160K | Low–Medium |185| `o4-mini` | 200K | 150K | Medium |186| `gemini-2.5-pro` | 1M | 600K | Medium |187| `gemini-2.5-flash` | 1M | 520K | Medium–High |188| `gemini-2.0-flash` | 1M | 500K | High |189190### HuggingFace Auto-Resolution191192Any model string containing `/` is treated as a HuggingFace repo ID. The server fetches `config.json` from the repo, extracts the context window size (`max_position_embeddings`, `n_positions`, or `max_seq_len`), and generates a conservative degradation profile:193194- **65%** of max tokens → degradation onset195- **80%** of max tokens → danger zone196197Results are cached in SQLite — subsequent lookups are instant.198199```200model: "meta-llama/Llama-3.1-70B" → 131K context, danger at 105K201model: "mistralai/Mistral-7B-v0.1" → 32K context, danger at 26K202model: "mosaicml/mpt-7b" → 65K context, danger at 52K203```204205If the fetch fails (network error, gated model, missing config), the server falls back silently to conservative defaults.206207### Fallback208209Any unrecognized model string without `/` falls back to conservative defaults (128K max, 100K danger zone).210211## How It Works212213The health score is a weighted composite of four signals:214215| Signal | Weight | Source |216|---|---|---|217| **Token utilization quality** | 40% | Model-specific sigmoid degradation curve |218| **Retrieval accuracy** | 25% | Base accuracy minus lost-in-the-middle penalty |219| **Tool-call burden** | 20% | Compounding quality loss after 10+ tool calls |220| **Session length** | 15% | Time-based fatigue heuristic |221222The degradation curves are derived from empirical research:223- [Chroma: Context Rot](https://research.trychroma.com/context-rot) — quality degrades around 147K–152K tokens on 200K models224- [Stanford: Lost in the Middle](https://arxiv.org/abs/2307.03172) — retrieval accuracy drops for information in the middle of the context window225- [Redis: Context Rot](https://redis.io/blog/context-rot/) — compounding degradation effects in long-running agents226227## Development228229```bash230git clone https://github.com/milos-product-maker/context-rot-detection.git231cd context-rot-detection232npm install233npm run dev # Run with tsx (hot reload)234npm test # Run unit tests235npm run build # Compile TypeScript236```237238### Testing with MCP Inspector239240```bash241npx @modelcontextprotocol/inspector node dist/index.js242```243244## License245246MIT247
Full transparency — inspect the skill content before installing.