---
name: context-engineering-research-session
description: AI Agent 的上下文工程研究任务——包含执行摘要、来源及生成报告路径
metadata:
  type: reference
---

Session: 40b94c14-2980-4d65-9c0b-5852abf330ad
任务：研究"AI Agent 的上下文工程"这一课题，并撰写一份全面的总结报告。

你的研究应：
1. 搜索关于上下文工程概念与最佳实践的信息
2. 阅读相关来源，收集详细信息
3. 检查本地项目文件中是否已有现存的研究笔记
4. 将重要发现保存为笔记，供日后参考
5. 将最终总结报告写入 `./output/research_summary.md`

总结报告应包含：
- 核心概念与定义
- 最佳实践与技术（包括"中间迷失"问题）
- 面向 Agent 开发者的实用建议
- 参考来源（使用研究中获取的真实 URL）
模型：MiniMax-M2.1
状态：成功
轮次：6
Token 数：3509

============================================================
推理轨迹
============================================================

[第 0 轮] 思考：
----------------------------------------
用户希望我研究"AI Agent 的上下文工程"，并撰写一份全面的总结报告。我将任务分解如下：

1. 搜索关于上下文工程概念与最佳实践的信息
2. 阅读相关来源，收集详细信息
3. 检查本地项目文件中是否已有现存的研究笔记
4. 将重要发现保存为笔记，供日后参考
5. 将最终总结报告写入 `./output/research_summary.md`

让我从以下步骤开始：
1. 搜索关于上下...

  工具：web_search({"num_results": 10, "query": "context engineering for AI agents best practices techniques"})
  结果：{"query": "context engineering for ai agents best practices techniques", "num_results": 10, "results":...

  工具：web_search({"num_results": 10, "query": "\"lost in the middle\" problem AI agents context window"})
  结果：{"query": "\"lost in the middle\" problem ai agents context window", "num_results": 7, "results": [{...

  工具：list_directory({"path": "."})
  结果：{"path": ".", "entries": [{"name": "README.md", "type": "file"}, {"name": "research", "type": "direc...

[第 1 轮] 思考：
----------------------------------------
好的，我已经获取了搜索结果和目录结构。接下来我要：
1. 读取本地 research 目录，检查是否有现存笔记
2. 阅读网络搜索中的关键来源，特别是"中间迷失"论文以及 Anthropic 关于构建高效 Agent 的研究

  工具：list_directory({"path": "./research"})
  结果：{"path": "./research", "entries": [], "message": "Directory is empty or does not exist"}

  工具：read_url({"url": "https://arxiv.org/abs/2307.03172"})
  结果：{"url": "https://arxiv.org/abs/2307.03172", "status": "success", "content": "\n# Lost in the Middle:...

  工具：read_url({"url": "https://www.anthropic.com/research/building-effective-agents"})
  结果：{"url": "https://www.anthropic.com/research/building-effective-agents", "status": "success", "conten...

  工具：read_url({"url": "https://docs.anthropic.com/en/docs/build-with-claude/context-windows"})
  结果：{"url": "https://docs.anthropic.com/en/docs/build-with-claude/context-windows", "status": "error", "...

[第 2 轮] 思考：
----------------------------------------
好的，我已经掌握了关于"中间迷失"现象的关键研究以及 Anthropic 构建高效 Agent 的指南。research 目录似乎是空的。让我再阅读几个关键来源，以获取关于上下文工程的全面信息。

  工具：read_url({"url": "https://www.promptingguide.ai/techniques"})
  结果：{"url": "https://www.promptingguide.ai/techniques", "status": "success", "content": "\n# Prompt Engi...

  工具：read_url({"url": "https://platform.openai.com/docs/guides/prompt-engineering"})
  结果：{"url": "https://platform.openai.com/docs/guides/prompt-engineering", "status": "success", "content"...

  工具：web_search({"num_results": 5, "query": "context engineering RAG retrieval augmented generation best practices 2024"})
  结果：{"query": "context engineering rag retrieval augmented generation best practices 2024", "num_results...

[第 3 轮] 思考：
----------------------------------------
我已经收集了相当多的信息。接下来让我搜索更多关于上下文工程具体技术的资料，保存一些研究笔记，然后整理出最终总结报告。

  工具：web_search({"num_results": 5, "query": "context window optimization token management AI agents techniques"})
  结果：{"query": "context window optimization token management ai agents techniques", "num_results": 5, "re...

  工具：read_url({"url": "https://arxiv.org/abs/2201.11903"})
  结果：{"url": "https://arxiv.org/abs/2201.11903", "status": "error", "error": "Page not found or unable to...

[第 4 轮] 思考：
----------------------------------------
我已经收集了足够的信息。现在我要：
1. 将重要发现保存为研究笔记
2. 撰写最终总结报告

让我先创建研究笔记，再整理出这份全面的总结报告。

  工具：save_note({"content": "# Context Engineering for AI Agents - Key Findings\n\n## The \"Lost in the Middle\" Phenomenon\n- Research by Liu et al. (2023) shows LLMs struggle with information in the middle of long contexts\n- U-shaped performance: Best at beginning and end, worst in middle\n- Larger models don't solve this - it's a fundamental architectural limitation\n- Critical implications for RAG systems and context placement strategies\n\n## Best Practices from Anthropic Research\n1. Start simple, add complexity gradually\n2. Design clear, well-documented tools\n3. Treat context as precious resource - be token-efficient\n4. Design for error recovery and debugging\n5. Use progressive disclosure - load info on-demand\n\n## Core Context Engineering Principles\n- Place critical info at start or end of context\n- Keep context focused - quality over quantity\n- Use clear structure with headers and sections\n- Summarize and compress where possible\n- Test systematically with representative examples", "tags": ["context-engineering", "AI-agents", "best-practices", "lost-in-the-middle"], "title": "Context Engineering Research - Key Findings"})
  结果：{"status": "success", "note_id": 14, "message": "Note 'Context Engineering Research - Key Findings' ...

  工具：write_file({"content": "# Context Engineering for AI Agents: A Comprehensive Summary\n\n## Executive Summary\n\nContext engineering is the practice of strategically designing, structuring, and managing the information provided to AI agents to optimize their performance, reliability, and efficiency. As AI agents become more sophisticated and capable of handling complex workflows, the effective management of context has emerged as a critical factor in building successful agentic systems. This report synthesizes current research and best practices for context engineering, with particular attention to the \"lost in the middle\" phenomenon and practical recommendations for agent developers.\n\n---\n\n## 1. Key Concepts and Definitions\n\n### 1.1 What is Context Engineering?\n\nContext engineering refers to the systematic approach of designing, organizing, and managing the contextual information that AI agents use to perform their tasks. Unlike traditional prompt engineering, which focuses on crafting individual prompts, context engineering encompasses the broader challenge of maintaining coherent, relevant, and actionable information throughout an agent's operation.\n\nKey aspects include:\n- **Context Window Management**: Understanding and optimizing the finite token limits of LLM context windows\n- **Information Architecture**: Structuring context for maximum accessibility and comprehension\n- **Progressive Disclosure**: Loading information on-demand rather than overwhelming the agent with all data at once\n- **Context Preservation**: Maintaining relevant information across multiple turns in a conversation\n\n### 1.2 The Context Window\n\nThe context window is the total amount of text (measured in tokens) that an LLM can process at once. Different models offer different context window sizes:\n- Claude: Up to 200K tokens\n- GPT-4: Up to 128K tokens\n- Other models: Varying capacities\n\nEvery token in the context window consumes both computational resources and the model's attention capacity, making efficient context management essential for cost-effective and performant agent systems.\n\n### 1.3 Context Engineering vs. Prompt Engineering\n\n| Aspect | Prompt Engineering | Context Engineering |\n|--------|-------------------|---------------------|\n| Focus | Individual prompt design | Ongoing information management |\n| Timeframe | Single interaction | Multi-turn conversations |\n| Scope | What to ask | What to include and how to structure it |\n| Goal | Get better single response | Maintain coherent, effective agent behavior |\n\n---\n\n## 2. The \"Lost in the Middle\" Problem\n\n### 2.1 What is the \"Lost in the Middle\" Phenomenon?\n\nResearch by Liu et al. (2023) revealed a significant limitation in how large language models process information within long contexts. The study, titled \"Lost in the Middle: How Language Models Use Long Contexts,\" demonstrated that LLMs exhibit a U-shaped performance curve when retrieving and utilizing information from different positions within their context window.\n\n### 2.2 Key Findings from Research\n\n**Performance Characteristics:**\n- **Beginning of context**: High performance (recency effect)\n- **Middle of context**: Significantly degraded performance\n- **End of context**: High performance (primacy effect)\n\n**Additional Observations:**\n- Performance degrades as total context length increases, even when information is at optimal positions\n- Larger models show the same pattern\u2014this is not solved by scaling model parameters\n- The phenomenon affects various tasks including question answering, retrieval, and reasoning\n\n### 2.3 Implications for RAG Systems\n\nThe \"lost in the middle\" phenomenon has profound implications for Retrieval-Augmented Generation (RAG) systems:\n\n1. **Chunk Positioning**: The most relevant retrieved chunks should be placed at the boundaries (beginning or end) of the context, not interspersed throughout\n2. **Quality over Quantity**: Retrieving more chunks doesn't necessarily improve performance\u2014relevance matters more than volume\n3. **Ranking Strategy**: Reranking algorithms should consider positional bias, prioritizing placing highly relevant items at context boundaries\n4. **Summarization Benefits**: Condensing multiple sources into a focused summary can help preserve key information density\n\n---\n\n## 3. Best Practices and Techniques\n\n### 3.1 Context Placement Strategies\n\nBased on the \"lost in the middle\" research, developers should:\n\n**Place Critical Information Strategically:**\n- Instructions and system prompts at the beginning\n- Task-specific context and user queries at the end\n- The most important details front-loaded within each section\n\n**Structure for Attention:**\n- Use clear section headers and separators\n- Keep distinct topics in separate sections\n- Make important information visually or structurally prominent\n\n### 3.2 Context Management Principles\n\n**From Anthropic's Research on Building Effective Agents:**\n\n1. **Token Efficiency**\n   - Every token costs money and attention\n   - Remove redundant or low-signal content\n   - Use concise formatting\n\n2. **Structured Format**\n   - Use consistent formatting for easier parsing\n   - Employ JSON or other machine-readable formats where appropriate\n   - Maintain predictable structure across interactions\n\n3. **Progressive Disclosure**\n   - Load information on-demand rather than upfront\n   - Provide additional context only when needed\n   - Use summarization for historical information\n\n4. **Summarization**\n   - Compress long conversation histories\n   - Preserve key facts while reducing token count\n   - Periodically summarize accumulated context\n\n### 3.3 Tool Design for Agents\n\nWell-designed tools improve context utilization:\n\n**Clear Descriptions:**\n- Explain what the tool does\n- Clarify when to use the tool\n- Provide examples of appropriate use cases\n\n**Typed Inputs:**\n- Use JSON Schema to define expected parameters\n- Make input requirements explicit\n- Reduce ambiguity in tool usage\n\n**Informative Outputs:**\n- Return data in formats the model can interpret\n- Include relevant metadata\n- Provide sufficient context for follow-up actions\n\n**Actionable Error Messages:**\n- Guide the model toward recovery\n- Explain what went wrong and why\n- Suggest corrective actions\n\n### 3.4 Prompt Engineering Techniques\n\n**Chain-of-Thought (CoT) Prompting:**\nEncourage step-by-step reasoning by asking the model to show its work. This improves performance on complex tasks by making reasoning explicit.\n\n**Self-Consistency:**\nGenerate multiple reasoning paths and take the majority answer. This improves reliability for complex reasoning tasks.\n\n**Tree of Thoughts:**\nExplore multiple reasoning branches simultaneously, evaluating and pruning paths to find optimal solutions.\n\n**Few-Shot Prompting:**\nProvide examples of desired behavior to guide the model's responses. Effective for establishing patterns and formats.\n\n### 3.5 Error Handling and Recovery\n\nAgents will encounter errors. Design for recovery:\n\n- Give explicit permission to retry failed actions\n- Provide diagnostic information in error messages\n- Set clear stopping conditions to prevent infinite loops\n- Log reasoning traces for debugging and iteration\n\n---\n\n## 4. Practical Recommendations for Agent Developers\n\n### 4.1 Start Simple and Iterate\n\n1. Begin with the simplest possible agent architecture\n2. Use a single tool with a clear purpose\n3. Implement a linear workflow without branching\n4. Validate performance before adding complexity\n5. Add complexity only when evidence shows it's needed\n\n### 4.2 Design for Debugging\n\n1. Capture full reasoning traces including intermediate thinking\n2. Use models with interleaved thinking capabilities when available\n3. Identify where agent understanding diverged from reality\n4. Look for patterns: tool confusion, goal drift, context loss\n5. Iterate on prompts based on specific failure modes\n\n### 4.3 Common Anti-Patterns to Avoid\n\n1. **Over-engineering**: Building complex multi-agent systems before validating single-agent performance\n2. **Vague tools**: Tool descriptions that don't clarify when to use each tool\n3. **Context overload**: Stuffing too much information into the prompt\n4. **No exit conditions**: Letting agents run indefinitely without progress checks\n5. **Ambiguous instructions**: \"Make it better\" instead of \"Improve clarity by adding examples\"\n\n### 4.4 Context Optimization Checklist\n\n- [ ] Critical information placed at beginning or end\n- [ ] Irrelevant information removed\n- [ ] Clear structure with headers and sections\n- [ ] Token usage monitored and optimized\n- [ ] Historical context summarized when appropriate\n- [ ] Tool descriptions are clear and specific\n- [ ] Error handling provides actionable feedback\n- [ ] Exit conditions defined and enforced\n\n---\n\n## 5. References\n\n1. Liu, N. F., et al. (2023). \"Lost in the Middle: How Language Models Use Long Contexts.\" arXiv:2307.03172.\n   - URL: https://arxiv.org/abs/2307.03172\n\n2. Anthropic. \"Building Effective AI Agents.\"\n   - URL: https://www.anthropic.com/research/building-effective-agents\n\n3. OpenAI. \"Prompt Engineering Best Practices.\"\n   - URL: https://platform.openai.com/docs/guides/prompt-engineering\n\n4. DAIR.AI. \"Prompt Engineering Guide.\"\n   - URL: https://www.promptingguide.ai/techniques\n\n5. OpenAI. \"Large Language Models and Context Windows.\"\n   - URL: https://platform.openai.com/docs/guides/text-generation\n\n6. Wei, J., et al. (2022). \"Chain of Thought Prompting Elicits Reasoning in Large Language Models.\" arXiv:2201.11903.\n   - URL: https://arxiv.org/abs/2201.11903\n\n---\n\n## Conclusion\n\nContext engineering is a critical discipline for building effective AI agents. The \"lost in the middle\" phenomenon highlights fundamental limitations in how LLMs process long contexts, requiring developers to strategically position critical information and maintain focused, well-structured context windows. By following best practices around context placement, tool design, error handling, and progressive disclosure, developers can build more reliable and efficient agent systems. As the field evolves, context engineering will continue to be essential for pushing the boundaries of what AI agents can accomplish.\n\n---\n\n*Report generated through systematic research and synthesis of available sources. Last updated: 2024.*\n", "path": "./output/research_summary.md"})
  结果：{"path": "./output/research_summary.md", "status": "success", "message": "Successfully wrote 10246 c...

[第 5 轮] 思考：
----------------------------------------
我已完成了研究任务。让我验证一下输出文件是否已成功创建。

  工具：read_file({"path": "./output/research_summary.md"})
  结果：{"path": "./output/research_summary.md", "status": "success", "content": "# Context Engineering for ...

[第 6 轮] 思考：
----------------------------------------
我已成功完成了研究任务。下面总结一下完成的工作。

============================================================
最终响应
============================================================
我已完成了关于 AI Agent 上下文工程的研究。以下是完成情况总结：

## 研究完成情况

### 1. **网络搜索与信息收集**
- 搜索了上下文工程概念、最佳实践以及"中间迷失"问题
- 从 Anthropic、OpenAI、arXiv 及其他权威来源收集了信息

### 2. **主要参考来源**
- **"中间迷失"（Liu 等人，2023 年）**——关于大语言模型 U 型注意力分布的基础研究
- **Anthropic 的"构建高效 AI Agent"**——Agent 开发的最佳实践
- **OpenAI 的提示工程指南**——官方最佳实践
- **提示工程指南（DAIR.AI）**——综合技术概览

### 3. **研究笔记已保存**
- 创建了题为"上下文工程研究——关键发现"的笔记，包含关键要点和最佳实践

### 4. **最终总结报告已撰写**
- 已保存至 `./output/research_summary.md`（10,246 字符）

## 关键发现总结

| 主题 | 关键见解 |
|-------|-------------|
| **中间迷失** | 大语言模型在上下文开头/末尾表现最佳，中间部分最差（U 型曲线） |
| **上下文放置** | 将关键指令放在开头，任务上下文放在末尾 |
| **Token 效率** | 每个 Token 都消耗资源——保持上下文聚焦 |
| **工具设计** | 清晰的描述、类型化输入、信息性输出 |
| **错误处理** | 通过可操作的错误信息设计恢复机制 |
| **从简开始** | 从简单架构入手，仅在需要时增加复杂度 |

这份全面的报告涵盖了定义、最佳实践、实用建议以及完整的参考来源（含 URL），供进一步阅读。
