context-rot-detection
Provides AI agents with real-time cognitive health monitoring, detecting context rot through token utilization, retrieval accuracy, and session fatigue analysis.
README
Context Rot Detection
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 — it just starts making worse decisions. This tool gives agents real-time visibility into their own cognitive health.
Features
- Health score (0–100) based on token utilization, retrieval accuracy, and session fatigue
- Model-specific degradation curves for 15+ curated models (Claude, GPT, Gemini, o-series)
- Auto-resolves any HuggingFace model — pass a repo ID like
meta-llama/Llama-3.1-70Band the context window is detected automatically, with results cached in SQLite - Lost-in-the-middle risk scoring based on Stanford research
- Tool-call burden and session fatigue analysis
- Actionable recovery recommendations — compact context, offload to memory, checkpoint, break into subtasks
- Per-agent health history tracking (SQLite)
- Service-wide utilization statistics
Quick Start
npx (zero install)
npx context-rot-detection
npm (global install)
npm install -g context-rot-detection
context-rot-detection
MCP Client Configuration
Claude Code
Add to .mcp.json in your project root:
{
"mcpServers": {
"context-rot-detection": {
"command": "npx",
"args": ["-y", "context-rot-detection"],
"env": {
"HEALTH_HISTORY_DB": "./health.db"
}
}
}
}
Claude Desktop
Add to claude_desktop_config.json:
{
"mcpServers": {
"context-rot-detection": {
"command": "npx",
"args": ["-y", "context-rot-detection"],
"env": {
"HEALTH_HISTORY_DB": "/path/to/health.db"
}
}
}
}
Docker
{
"mcpServers": {
"context-rot-detection": {
"command": "docker",
"args": [
"run", "-i", "--rm",
"-v", "context-rot-data:/data",
"ghcr.io/milos-product-maker/context-rot-detection:latest"
]
}
}
}
Configuration
| Environment Variable | Description | Default |
|---|---|---|
HEALTH_HISTORY_DB |
Path to SQLite database for health history. Use :memory: for ephemeral storage. |
:memory: |
LOG_FILE |
Path to append structured JSON log lines. Omit to disable file logging. | (none) |
Tools
check_my_health
Analyze the current context window health. Call this periodically during long sessions or before critical decisions.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
token_count |
integer | Yes | Current estimated token count in context window |
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) |
session_duration_minutes |
integer | No | How long this session has been running |
tool_calls_count |
integer | No | Number of tool calls made in this session |
context_summary |
string | No | Brief summary of current task and recent actions |
agent_id |
string | No | Unique agent identifier for history tracking |
Example response:
{
"health_score": 62,
"status": "warning",
"token_utilization": {
"current": 155000,
"max_effective": 170000,
"percentage": 91.2,
"danger_zone_starts_at": 170000
},
"quality_estimate": {
"retrieval_accuracy": "degrading",
"middle_content_risk": "high",
"estimated_hallucination_risk": "moderate"
},
"session_fatigue": {
"tool_call_burden": "moderate",
"session_length_risk": "low",
"recommendation": "Consider breaking into sub-tasks if complexity increases."
},
"recommendations": [
{
"priority": "high",
"action": "compact_context",
"reason": "You are approaching the effective quality threshold. Summarize older context and remove completed task details.",
"estimated_quality_gain": 15
},
{
"priority": "high",
"action": "offload_to_memory",
"reason": "High risk of lost-in-the-middle effect. Store critical information to external memory before it is effectively lost.",
"estimated_quality_gain": 8
}
]
}
get_health_history
Retrieve health check history for a specific agent.
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
agent_id |
string | Yes | Unique agent identifier |
limit |
integer | No | Max records to return (default: 20, max: 100) |
get_service_stats
Get service-wide utilization statistics. No parameters required.
Returns total calls, unique agents, average health score, model distribution, status distribution, and recent activity (last hour / last 24h).
Supported Models
| Model | Max Tokens | Danger Zone | Middle-Loss Risk |
|---|---|---|---|
claude-opus-4-5 |
200K | 175K | Low |
claude-opus-4 |
200K | 170K | Low |
claude-sonnet-4 |
200K | 165K | Low |
claude-3.7-sonnet |
200K | 160K | Low–Medium |
claude-3.5-sonnet |
200K | 152K | Medium |
claude-haiku-3.5 |
200K | 130K | Medium |
gpt-4.1 |
1M | 500K | Medium |
gpt-4.1-mini |
1M | 450K | Medium |
gpt-4o |
128K | 105K | Medium |
gpt-4o-mini |
128K | 95K | Medium–High |
o3 |
200K | 160K | Low–Medium |
o4-mini |
200K | 150K | Medium |
gemini-2.5-pro |
1M | 600K | Medium |
gemini-2.5-flash |
1M | 520K | Medium–High |
gemini-2.0-flash |
1M | 500K | High |
HuggingFace Auto-Resolution
Any 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:
- 65% of max tokens → degradation onset
- 80% of max tokens → danger zone
Results are cached in SQLite — subsequent lookups are instant.
model: "meta-llama/Llama-3.1-70B" → 131K context, danger at 105K
model: "mistralai/Mistral-7B-v0.1" → 32K context, danger at 26K
model: "mosaicml/mpt-7b" → 65K context, danger at 52K
If the fetch fails (network error, gated model, missing config), the server falls back silently to conservative defaults.
Fallback
Any unrecognized model string without / falls back to conservative defaults (128K max, 100K danger zone).
How It Works
The health score is a weighted composite of four signals:
| Signal | Weight | Source |
|---|---|---|
| Token utilization quality | 40% | Model-specific sigmoid degradation curve |
| Retrieval accuracy | 25% | Base accuracy minus lost-in-the-middle penalty |
| Tool-call burden | 20% | Compounding quality loss after 10+ tool calls |
| Session length | 15% | Time-based fatigue heuristic |
The degradation curves are derived from empirical research:
- Chroma: Context Rot — quality degrades around 147K–152K tokens on 200K models
- Stanford: Lost in the Middle — retrieval accuracy drops for information in the middle of the context window
- Redis: Context Rot — compounding degradation effects in long-running agents
Development
git clone https://github.com/milos-product-maker/context-rot-detection.git
cd context-rot-detection
npm install
npm run dev # Run with tsx (hot reload)
npm test # Run unit tests
npm run build # Compile TypeScript
Testing with MCP Inspector
npx @modelcontextprotocol/inspector node dist/index.js
License
MIT
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。