npx skills add https://github.com/crinkj/common-claude-setting --skill context-degradation随着上下文长度的增加,语言模型会表现出可预测的退化模式。理解这些模式对于诊断故障和设计弹性系统至关重要。上下文退化不是一种二元状态,而是一种性能退化的连续体,以几种不同的方式表现出来。
在以下情况时激活此技能:
上下文退化通过几种不同的模式表现出来。“中间迷失”现象导致上下文中间的信息获得较少的关注。当错误通过重复引用而复合时,会发生上下文污染。当不相关信息压倒相关内容时,会发生上下文干扰。当模型无法确定哪个上下文适用时,会出现上下文混淆。当累积的信息直接冲突时,会产生上下文冲突。
这些模式是可预测的,可以通过压缩、掩码、分区和隔离等架构模式来缓解。
记录最充分的退化模式是“中间迷失”效应,即模型表现出 U 形注意力曲线。上下文开头和结尾的信息获得可靠的关注,而埋在中间的信息则召回准确性急剧下降。
实证证据 研究表明,与放在开头或结尾的相同信息相比,放在上下文中间的相关信息的召回准确性低 10-40%。这不是模型的失败,而是注意力机制和训练数据分布的结果。
模型将大量注意力分配给第一个标记(通常是 BOS 标记)以稳定内部状态。这创造了一个“注意力汇”,吸收了注意力预算。随着上下文的增长,有限的预算被摊得更薄,中间的标记无法获得足够的注意力权重来进行可靠的检索。
实际影响 设计上下文放置时要考虑注意力模式。将关键信息放在上下文的开头或结尾。考虑信息是会被直接查询还是需要支持推理——如果是后者,放置位置不那么重要,但整体信号质量更重要。
对于长文档或对话,使用摘要结构,在注意力偏好的位置呈现关键信息。使用显式的章节标题和过渡来帮助模型导航结构。
当幻觉、错误或不正确信息进入上下文并通过重复引用而复合时,就会发生上下文污染。一旦被污染,上下文就会产生反馈循环,强化错误的信念。
污染如何发生 污染通常通过三种途径进入。首先,工具输出可能包含错误或意外格式,模型将其接受为事实。其次,检索到的文档可能包含不正确或过时的信息,模型将其纳入推理。第三,模型生成的摘要或中间输出可能会引入在上下文中持续存在的幻觉。
复合效应是严重的。如果智能体的目标部分被污染,它会制定出需要大量努力才能撤销的策略。每个后续决策都会引用被污染的内容,强化错误的假设。
检测与恢复 注意观察以下症状:包括先前成功任务上的输出质量下降、智能体调用错误工具或参数的“工具错位”,以及尽管尝试纠正但幻觉仍然持续。当这些症状出现时,考虑上下文污染。
恢复需要移除或替换被污染的内容。这可能涉及截断上下文到污染点之前,在上下文中明确注明污染并要求重新评估,或者用干净的上下文重新开始并仅保留已验证的信息。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
当上下文变得如此之长,以至于模型过度关注所提供的信息而牺牲其训练知识时,就会出现上下文干扰。模型会关注上下文中的所有内容,无论其相关性如何,这造成了使用所提供信息的压力,即使内部知识更准确。
干扰效应 研究表明,即使上下文中存在单个不相关文档,也会降低涉及相关文档的任务的性能。多个干扰因素会加剧退化。这种效应不是关于绝对意义上的噪音,而是关于注意力分配——不相关信息与相关信息竞争有限的注意力预算。
模型没有“跳过”不相关上下文的机制。它们必须关注所提供的所有内容,即使不相关信息显然没有用,这种义务也会造成干扰。
缓解策略 通过精心策划进入上下文的内容来缓解干扰。在加载检索到的文档之前应用相关性过滤。使用命名空间和组织结构,使不相关的部分在结构上容易被忽略。考虑信息是否真的需要放在上下文中,或者可以通过工具调用访问。
当不相关信息以降低质量的方式影响响应时,就会出现上下文混淆。这与干扰有关但又不同——混淆关注的是上下文对模型行为的影响,而不是注意力分配。
如果你把某些东西放在上下文中,模型就必须关注它。模型可能会纳入不相关信息、使用不恰当的工具定义,或应用来自不同上下文的约束。当上下文包含多种任务类型或在单个会话中切换任务时,混淆尤其成问题。
混淆的迹象 注意观察那些处理查询错误方面的响应、似乎适用于不同任务的工具调用,或者混合了多个来源要求的输出。这些表明对当前情况适用哪种上下文感到困惑。
架构解决方案 架构解决方案包括:明确的任务分割(不同任务获得不同的上下文窗口)、任务上下文之间的清晰过渡,以及隔离不同目标上下文的状态管理。
当累积的信息直接冲突,产生矛盾的指导并扰乱推理时,就会产生上下文冲突。这与污染不同,污染是指一条信息不正确——而在冲突中,多条正确的信息相互矛盾。
冲突的来源 冲突通常源于多源检索(不同来源的信息相互矛盾)、版本冲突(过时和当前信息同时出现在上下文中)以及视角冲突(不同观点有效但不兼容)。
解决方法 解决方法包括:明确标记冲突(识别矛盾并要求澄清)、优先级规则(确定哪个来源优先)和版本过滤(从上下文中排除过时信息)。
研究提供了关于退化模式的具体数据,为设计决策提供信息。
RULER 基准测试结果 RULER 基准测试提供了令人警醒的发现:只有 50% 声称支持 32K+ 上下文的模型在 32K 标记时能保持令人满意的性能。GPT-5.2 在当前模型中表现出最少的退化,而许多模型在扩展上下文下仍会下降 30 分以上。在简单的“大海捞针”测试中获得接近完美的分数,并不等同于真正的长上下文理解能力。
模型特定的退化阈值
| 模型 | 退化开始 | 严重退化 | 备注 |
|---|---|---|---|
| GPT-5.2 | ~64K 标记 | ~200K 标记 | 整体退化抵抗力最佳,具备思考模式 |
| Claude Opus 4.5 | ~100K 标记 | ~180K 标记 | 200K 上下文窗口,强大的注意力管理 |
| Claude Sonnet 4.5 | ~80K 标记 | ~150K 标记 | 为智能体和编码任务优化 |
| Gemini 3 Pro | ~500K 标记 | ~800K 标记 | 1M 上下文窗口,原生多模态 |
| Gemini 3 Flash | ~300K 标记 | ~600K 标记 | 速度是 Gemini 2.5 的 3 倍,MMMU-Pro 得分 81.2% |
模型特定的行为模式 不同模型在上下文压力下表现出不同的故障模式:
这些模式为不同用例的模型选择提供了信息。高风险任务受益于 Claude 4.5 的保守方法或 GPT-5.2 的思考模式;速度关键型任务可以使用即时模式。
研究揭示了几种反直觉的模式,挑战了关于上下文管理的假设。
打乱的“干草堆”优于连贯的“干草堆” 研究发现,打乱的(不连贯的)“干草堆”比逻辑连贯的“干草堆”产生更好的性能。这表明连贯的上下文可能会产生混淆检索的错误关联,而不连贯的上下文则迫使模型依赖精确匹配。
单个干扰因素影响巨大 即使是一个不相关文档,也会显著降低性能。这种效应与噪音量不成比例,而是遵循一个阶跃函数,即任何干扰因素的存在都会触发退化。
“针”-问题相似性相关性 “针”和问题对之间的相似性越低,随着上下文长度的增加,退化越快。需要跨不同内容进行推理的任务尤其脆弱。
更大的上下文窗口并不总是能提高性能。在许多情况下,更大的上下文会带来新的问题,其负面影响超过了收益。
性能退化曲线 模型表现出随上下文长度增加的非线性退化。性能在达到阈值之前保持稳定,然后迅速下降。阈值因模型和任务复杂性而异。对于许多模型,即使上下文窗口支持更大的尺寸,有意义的退化大约在 8,000-16,000 标记处开始。
成本影响 处理成本随上下文长度不成比例地增长。处理 400K 标记上下文的成本不是处理 200K 的两倍——它在时间和计算资源上都呈指数级增长。对于许多应用来说,这使得大上下文处理在经济上不切实际。
认知负荷隐喻 即使拥有无限的上下文,要求单个模型在数十个独立任务中保持一致的品质也会造成认知瓶颈。模型必须不断在项目之间切换上下文,维护比较框架,并确保风格一致性。这不是更多上下文能解决的问题。
四种策略解决上下文退化的不同方面:
写入 : 使用草稿纸、文件系统或外部存储将上下文保存在窗口之外。这使活动上下文保持精简,同时保留信息访问。
选择 : 通过检索、过滤和优先级排序将相关上下文拉入窗口。这通过排除不相关信息来解决干扰问题。
压缩 : 通过摘要、抽象和观察掩码减少标记,同时保留信息。这扩展了有效的上下文容量。
隔离 : 将上下文分割到子智能体或会话中,以防止任何单个上下文变得过大而退化。这是最激进的策略,但往往也是最有效的。
通过特定的架构模式实现这些策略。使用即时上下文加载,仅在需要时检索信息。使用观察掩码,用紧凑的引用替换冗长的工具输出。使用子智能体架构来隔离不同任务的上下文。使用压缩,在上下文超过限制之前对不断增长的上下文进行总结。
示例 1:检测退化
# 上下文在长对话中增长
turn_1: 1000 标记
turn_5: 8000 标记
turn_10: 25000 标记
turn_20: 60000 标记 (退化开始)
turn_30: 90000 标记 (显著退化)
示例 2:缓解中间迷失
# 将关键信息组织在边缘
[当前任务] # 在开头
- 目标: 生成季度报告
- 截止日期: 本周末
[详细上下文] # 中间 (较少关注)
- 50 页数据
- 多个分析部分
- 支持证据
[关键发现] # 在结尾
- 收入增长 15%
- 成本下降 8%
- A 区域增长
此技能建立在 context-fundamentals 之上,应在理解基本上下文概念后学习。它关联到:
内部参考:
本集合中的相关技能:
外部资源:
创建日期 : 2025-12-20 最后更新 : 2025-12-20 作者 : Agent Skills for Context Engineering Contributors 版本 : 1.0.0
每周安装数
1
代码库
首次出现
今天
安全审计
安装于
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
Language models exhibit predictable degradation patterns as context length increases. Understanding these patterns is essential for diagnosing failures and designing resilient systems. Context degradation is not a binary state but a continuum of performance degradation that manifests in several distinct ways.
Activate this skill when:
Context degradation manifests through several distinct patterns. The lost-in-middle phenomenon causes information in the center of context to receive less attention. Context poisoning occurs when errors compound through repeated reference. Context distraction happens when irrelevant information overwhelms relevant content. Context confusion arises when the model cannot determine which context applies. Context clash develops when accumulated information directly conflicts.
These patterns are predictable and can be mitigated through architectural patterns like compaction, masking, partitioning, and isolation.
The most well-documented degradation pattern is the "lost-in-middle" effect, where models demonstrate U-shaped attention curves. Information at the beginning and end of context receives reliable attention, while information buried in the middle suffers from dramatically reduced recall accuracy.
Empirical Evidence Research demonstrates that relevant information placed in the middle of context experiences 10-40% lower recall accuracy compared to the same information at the beginning or end. This is not a failure of the model but a consequence of attention mechanics and training data distributions.
Models allocate massive attention to the first token (often the BOS token) to stabilize internal states. This creates an "attention sink" that soaks up attention budget. As context grows, the limited budget is stretched thinner, and middle tokens fail to garner sufficient attention weight for reliable retrieval.
Practical Implications Design context placement with attention patterns in mind. Place critical information at the beginning or end of context. Consider whether information will be queried directly or needs to support reasoning—if the latter, placement matters less but overall signal quality matters more.
For long documents or conversations, use summary structures that surface key information at attention-favored positions. Use explicit section headers and transitions to help models navigate structure.
Context poisoning occurs when hallucinations, errors, or incorrect information enters context and compounds through repeated reference. Once poisoned, context creates feedback loops that reinforce incorrect beliefs.
How Poisoning Occurs Poisoning typically enters through three pathways. First, tool outputs may contain errors or unexpected formats that models accept as ground truth. Second, retrieved documents may contain incorrect or outdated information that models incorporate into reasoning. Third, model-generated summaries or intermediate outputs may introduce hallucinations that persist in context.
The compounding effect is severe. If an agent's goals section becomes poisoned, it develops strategies that take substantial effort to undo. Each subsequent decision references the poisoned content, reinforcing incorrect assumptions.
Detection and Recovery Watch for symptoms including degraded output quality on tasks that previously succeeded, tool misalignment where agents call wrong tools or parameters, and hallucinations that persist despite correction attempts. When these symptoms appear, consider context poisoning.
Recovery requires removing or replacing poisoned content. This may involve truncating context to before the poisoning point, explicitly noting the poisoning in context and asking for re-evaluation, or restarting with clean context and preserving only verified information.
Context distraction emerges when context grows so long that models over-focus on provided information at the expense of their training knowledge. The model attends to everything in context regardless of relevance, and this creates pressure to use provided information even when internal knowledge is more accurate.
The Distractor Effect Research shows that even a single irrelevant document in context reduces performance on tasks involving relevant documents. Multiple distractors compound degradation. The effect is not about noise in absolute terms but about attention allocation—irrelevant information competes with relevant information for limited attention budget.
Models do not have a mechanism to "skip" irrelevant context. They must attend to everything provided, and this obligation creates distraction even when the irrelevant information is clearly not useful.
Mitigation Strategies Mitigate distraction through careful curation of what enters context. Apply relevance filtering before loading retrieved documents. Use namespacing and organization to make irrelevant sections easy to ignore structurally. Consider whether information truly needs to be in context or can be accessed through tool calls instead.
Context confusion arises when irrelevant information influences responses in ways that degrade quality. This is related to distraction but distinct—confusion concerns the influence of context on model behavior rather than attention allocation.
If you put something in context, the model has to pay attention to it. The model may incorporate irrelevant information, use inappropriate tool definitions, or apply constraints that came from different contexts. Confusion is especially problematic when context contains multiple task types or when switching between tasks within a single session.
Signs of Confusion Watch for responses that address the wrong aspect of a query, tool calls that seem appropriate for a different task, or outputs that mix requirements from multiple sources. These indicate confusion about what context applies to the current situation.
Architectural Solutions Architectural solutions include explicit task segmentation where different tasks get different context windows, clear transitions between task contexts, and state management that isolates context for different objectives.
Context clash develops when accumulated information directly conflicts, creating contradictory guidance that derails reasoning. This differs from poisoning where one piece of information is incorrect—in clash, multiple correct pieces of information contradict each other.
Sources of Clash Clash commonly arises from multi-source retrieval where different sources have contradictory information, version conflicts where outdated and current information both appear in context, and perspective conflicts where different viewpoints are valid but incompatible.
Resolution Approaches Resolution approaches include explicit conflict marking that identifies contradictions and requests clarification, priority rules that establish which source takes precedence, and version filtering that excludes outdated information from context.
Research provides concrete data on degradation patterns that inform design decisions.
RULER Benchmark Findings The RULER benchmark delivers sobering findings: only 50% of models claiming 32K+ context maintain satisfactory performance at 32K tokens. GPT-5.2 shows the least degradation among current models, while many still drop 30+ points at extended contexts. Near-perfect scores on simple needle-in-haystack tests do not translate to real long-context understanding.
Model-Specific Degradation Thresholds
| Model | Degradation Onset | Severe Degradation | Notes |
|---|---|---|---|
| GPT-5.2 | ~64K tokens | ~200K tokens | Best overall degradation resistance with thinking mode |
| Claude Opus 4.5 | ~100K tokens | ~180K tokens | 200K context window, strong attention management |
| Claude Sonnet 4.5 | ~80K tokens | ~150K tokens | Optimized for agents and coding tasks |
| Gemini 3 Pro | ~500K tokens | ~800K tokens | 1M context window, native multimodality |
| Gemini 3 Flash | ~300K tokens | ~600K tokens | 3x speed of Gemini 2.5, 81.2% MMMU-Pro |
Model-Specific Behavior Patterns Different models exhibit distinct failure modes under context pressure:
These patterns inform model selection for different use cases. High-stakes tasks benefit from Claude 4.5's conservative approach or GPT-5.2's thinking mode; speed-critical tasks may use instant modes.
Research reveals several counterintuitive patterns that challenge assumptions about context management.
Shuffled Haystacks Outperform Coherent Ones Studies found that shuffled (incoherent) haystacks produce better performance than logically coherent ones. This suggests that coherent context may create false associations that confuse retrieval, while incoherent context forces models to rely on exact matching.
Single Distractors Have Outsized Impact Even a single irrelevant document reduces performance significantly. The effect is not proportional to the amount of noise but follows a step function where the presence of any distractor triggers degradation.
Needle-Question Similarity Correlation Lower similarity between needle and question pairs shows faster degradation with context length. Tasks requiring inference across dissimilar content are particularly vulnerable.
Larger context windows do not uniformly improve performance. In many cases, larger contexts create new problems that outweigh benefits.
Performance Degradation Curves Models exhibit non-linear degradation with context length. Performance remains stable up to a threshold, then degrades rapidly. The threshold varies by model and task complexity. For many models, meaningful degradation begins around 8,000-16,000 tokens even when context windows support much larger sizes.
Cost Implications Processing cost grows disproportionately with context length. The cost to process a 400K token context is not double the cost of 200K—it increases exponentially in both time and computing resources. For many applications, this makes large-context processing economically impractical.
Cognitive Load Metaphor Even with an infinite context, asking a single model to maintain consistent quality across dozens of independent tasks creates a cognitive bottleneck. The model must constantly switch context between items, maintain a comparative framework, and ensure stylistic consistency. This is not a problem that more context solves.
Four strategies address different aspects of context degradation:
Write : Save context outside the window using scratchpads, file systems, or external storage. This keeps active context lean while preserving information access.
Select : Pull relevant context into the window through retrieval, filtering, and prioritization. This addresses distraction by excluding irrelevant information.
Compress : Reduce tokens while preserving information through summarization, abstraction, and observation masking. This extends effective context capacity.
Isolate : Split context across sub-agents or sessions to prevent any single context from growing large enough to degrade. This is the most aggressive strategy but often the most effective.
Implement these strategies through specific architectural patterns. Use just-in-time context loading to retrieve information only when needed. Use observation masking to replace verbose tool outputs with compact references. Use sub-agent architectures to isolate context for different tasks. Use compaction to summarize growing context before it exceeds limits.
Example 1: Detecting Degradation
# Context grows during long conversation
turn_1: 1000 tokens
turn_5: 8000 tokens
turn_10: 25000 tokens
turn_20: 60000 tokens (degradation begins)
turn_30: 90000 tokens (significant degradation)
Example 2: Mitigating Lost-in-Middle
# Organize context with critical info at edges
[CURRENT TASK] # At start
- Goal: Generate quarterly report
- Deadline: End of week
[DETAILED CONTEXT] # Middle (less attention)
- 50 pages of data
- Multiple analysis sections
- Supporting evidence
[KEY FINDINGS] # At end
- Revenue up 15%
- Costs down 8%
- Growth in Region A
This skill builds on context-fundamentals and should be studied after understanding basic context concepts. It connects to:
Internal reference:
Related skills in this collection:
External resources:
Created : 2025-12-20 Last Updated : 2025-12-20 Author : Agent Skills for Context Engineering Contributors Version : 1.0.0
Weekly Installs
1
Repository
First Seen
Today
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
超能力技能使用指南:AI助手技能调用优先级与工作流程详解
45,100 周安装