上下文腐烂检测
MCP服务,使AI代理对其认知状态有自我意识。
每个长时间运行的AI代理都会遭受 语境腐化 --随着上下文窗口的填满,可测量的性能下降。研究来自 色度, 斯坦福 (“lost-in-the-midle”),以及 瑞迪斯 这证实了这是生产代理系统中排名第一的实际故障模式。
经历上下文腐烂的代理不会 *知道* 这是有辱人格的——它只是开始做出更糟糕的决定。此工具为代理提供 实时了解自己的认知健康状况.
特性
- 健康评分(0-100) 基于令牌利用率、检索准确性和会话疲劳
- 特定型号的退化曲线 适用于15+个精选型号(克劳德、GPT、双子座、o系列)
- 自动解析任何HuggingFace模型 --传递一个repo ID,如下所示
meta-llama/Llama-3.1-70B并且自动检测上下文窗口,结果缓存在SQLite中 - 中间损失风险评分 基于斯坦福大学的研究
- 工具调用负担 和 会话疲劳 分析
- 可采取行动的恢复建议 --紧凑上下文、卸载到内存、检查点、分解为子任务
- 每个代理的健康历史 跟踪(SQLite)
- 全服务利用率统计
快速开始
npx(零安装)
npx context-rot-detectionnpm(全局安装)
npm install -g context-rot-detection
context-rot-detectionMCP客户端配置
克劳德代码
增添 .mcp.json 在项目根目录中:
{
"mcpServers": {
"context-rot-detection": {
"command": "npx",
"args": ["-y", "context-rot-detection"],
"env": {
"HEALTH_HISTORY_DB": "./health.db"
}
}
}
}克劳德桌面
增添 claude_desktop_config.json:
{
"mcpServers": {
"context-rot-detection": {
"command": "npx",
"args": ["-y", "context-rot-detection"],
"env": {
"HEALTH_HISTORY_DB": "/path/to/health.db"
}
}
}
}码头工人
{
"mcpServers": {
"context-rot-detection": {
"command": "docker",
"args": [
"run", "-i", "--rm",
"-v", "context-rot-data:/data",
"ghcr.io/milos-product-maker/context-rot-detection:latest"
]
}
}
}配置
| 环境变量 | 描述 | 默认值 |
|---|---|---|
HEALTH_HISTORY_DB | 健康历史SQLite数据库的路径。使用 :memory: 用于临时存储。 | :memory: |
LOG_FILE | 附加结构化JSON日志行的路径。省略禁用文件日志记录。 | *(无)* |
工具
check_my_health
分析当前上下文窗口的运行状况。在长时间会议期间或关键决策之前定期调用此功能。
参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
token_count | integer | 是 | 上下文窗口中当前估计的令牌计数 |
model | string | No | LLM模型标识符——一个策划的名称(例如。, claude-opus-4, gpt-4o)HuggingFace回购ID(例如。, meta-llama/Llama-3.1-70B),或任何字符串(恢复到保守默认值) |
session_duration_minutes | integer | 否 | 此会话已运行多长时间 |
tool_calls_count | integer | 否 | 此会话中进行的工具调用数 |
context_summary | string | 否 | 当前任务和最近行动的简要总结 |
agent_id | string | 否 | 历史跟踪的唯一代理标识符 |
示例响应:
{
"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
检索特定代理的健康检查历史记录。
参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
agent_id | string | 是 | 唯一代理标识符 |
limit | integer | 否 | 要返回的最大记录数(默认值:20,最大值:100) |
get_service_stats
获取服务范围内的利用率统计数据。无需参数。
返回总呼叫数、唯一代理、平均健康评分、模型分布、状态分布和最近活动(最近一小时/最近24小时)。
支持的型号
| 模型 | 最大代币 | 危险区 | 中等损失风险 |
|---|---|---|---|
claude-opus-4-5 | 200K | 175K | 低 |
claude-opus-4 | 200K | 170K | 低 |
claude-sonnet-4 | 200K | 165K | 低 |
claude-3.7-sonnet | 200K | 160K | 低-中 |
claude-3.5-sonnet | 200K | 152K | 中等 |
claude-haiku-3.5 | 200K | 130K | 中等 |
gpt-4.1 | 1M | 500K | 中等 |
gpt-4.1-mini | 1M | 450K | 中等 |
gpt-4o | 128K | 105K | 中等 |
gpt-4o-mini | 128K | 95K | 中等-高等 |
o3 | 200K | 160K | 低-中 |
o4-mini | 200K | 150K | 中等 |
gemini-2.5-pro | 1M | 600K | 中等 |
gemini-2.5-flash | 1M | 520K | 中高 |
gemini-2.0-flash | 1M | 500K | 高 |
HuggingFace自动分辨率
包含以下内容的任何模型字符串 / 被视为HuggingFace仓库ID。服务器获取 config.json 从repo中提取上下文窗口大小(max_position_embeddings, n_positions,或 max_seq_len),并生成保守的退化曲线:
- 65% 最大令牌数→ 退化开始
- 80% 最大令牌数→ 危险区
结果缓存在SQLite中——后续查找是即时的。
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如果获取失败(网络错误、门控模型、缺少配置),服务器将自动恢复到保守的默认值。
后备方案
任何未识别的模型字符串 / 回落到保守的默认值(最大128K,100K危险区)。
运作原理
健康评分是四个信号的加权组合:
| 信号 | 权重 | 来源 |
|---|---|---|
| 代币使用质量 | 40% | 模型特定的S形退化曲线 |
| 检索精度 | 25% | 基本准确度减去中间损失惩罚 |
| 工具调用负担 | 20% | 10多次工具调用后质量损失加剧 |
| 会话长度 | 15% | 基于时间的疲劳启发式 |
降解曲线来自实证研究:
- 色度:上下文腐烂 --在200K型号上,质量下降了约147K–152K个代币
- 斯坦福:迷失在中间 -上下文窗口中间信息的检索精度下降
- Redis:上下文腐烂 --长效药剂的复合降解效应
发展
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 TypeScriptMCP检验员测试
npx @modelcontextprotocol/inspector node dist/index.js许可证
麻省理工学院
