---

Session: ed58e1ce-f051-483e-9d2b-675b53eb14e0
任务：研究「AI 智能体的上下文工程」这一主题，并撰写一份全面的总结报告。

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

总结报告应包含：
- 核心概念与定义
- 最佳实践与技术（包括「中间迷失」问题）
- 给智能体开发者的实用建议
- 所查阅来源的参考文献（使用来自你研究的实际 URL）
模型：MiniMax-M2.1
状态：成功
轮次：9
Token 数：4355

============================================================
推理追踪
============================================================

[第 0 轮] 思考：
----------------------------------------
用户希望我研究「AI 智能体的上下文工程」并撰写一份全面的总结报告。让我分解一下任务：

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

让我开始：
1. 检查是否有本地...

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

  工具：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": [{...

[第 1 轮] 思考：
----------------------------------------
我找到了一些有用的信息。让我：
1. 检查 research 目录中是否有任何已有的笔记
2. 阅读关于上下文工程和「中间迷失」问题的关键来源

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

  工具：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://arxiv.org/abs/2307.03172"})
  结果：{"url": "https://arxiv.org/abs/2307.03172", "status": "success", "content": "\n# Lost in the Middle:...

  工具：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 轮] 思考：
----------------------------------------
进展顺利。让我通过阅读更多关于提示工程、上下文管理和最佳实践的资料来继续收集信息。

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

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

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

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

[第 3 轮] 思考：
----------------------------------------
我现在已经有了关于上下文工程的良好信息。让我搜索更多关于 Token 优化和高级上下文工程技术的具体信息。

  工具：web_search({"num_results": 8, "query": "token optimization context compression AI agents long context"})
  结果：{"query": "token optimization context compression ai agents long context", "num_results": 8, "result...

  工具：web_search({"num_results": 8, "query": "agentic workflow context design system prompt engineering"})
  结果：{"query": "agentic workflow context design system prompt engineering", "num_results": 7, "results": ...

[第 4 轮] 思考：
----------------------------------------
让我通过阅读 OpenAI 提示工程指南和 LangChain 调试文档来获取更多关于上下文工程的见解。

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

  工具：read_url({"url": "https://python.langchain.com/docs/how_to/debugging"})
  结果：{"url": "https://python.langchain.com/docs/how_to/debugging", "status": "success", "content": "\n# D...

[第 5 轮] 思考：
----------------------------------------
我已经收集了相当多的信息。现在让我保存我的研究笔记并创建全面的总结报告。我将为关键发现创建笔记，然后将最终总结写入输出文件。

  工具：save_note({"content": "# Context Engineering for AI Agents - Key Research Findings\n\n## Core Concept\nContext engineering is the deliberate design and management of information provided to AI agents to optimize their performance, reliability, and cost-efficiency.\n\n## Key Findings\n\n### 1. The \"Lost in the Middle\" Problem (Liu et al., 2023)\n- LLMs exhibit U-shaped performance when retrieving information from long contexts\n- Information at the BEGINNING and END is reliably accessed\n- Information in the MIDDLE is significantly more likely to be ignored or forgotten\n- Model size does not fix this fundamental limitation\n- Performance degrades as total context length increases\n\n**Implication**: Place critical instructions and key information at the boundaries of your context.\n\n### 2. Context Management Best Practices\n- Token efficiency: Every token costs money and attention\n- Structured format: Use consistent formatting for easier parsing\n- Progressive disclosure: Load information on-demand\n- Summarization: Compress long histories while preserving key facts\n\n### 3. Tool Design Principles\n- Clear descriptions: Explain what the tool does AND when to use it\n- Typed inputs: Use JSON Schema to define expected parameters\n- Informative outputs: Return data the model can interpret and act on\n- Error messages: Provide actionable guidance when things fail\n\n### 4. Prompt Caching (Anthropic)\n- Reduces latency by up to 85% for cached content\n- Lowers costs by avoiding re-processing of identical context\n- Cache stable content (instructions, examples, tool descriptions)\n- Place dynamic content last\n\n### 5. Advanced Prompting Techniques\n- Chain-of-Thought: Encourage step-by-step reasoning\n- Self-Consistency: Generate multiple reasoning paths for complex tasks\n- Tree of Thoughts: Explore multiple reasoning branches\n- Iterative refinement based on failure patterns\n\n## Common Anti-Patterns to Avoid\n1. Over-engineering complex multi-agent systems before validating single-agent performance\n2. Vague tool descriptions that don't clarify usage\n3. Context overload with too much irrelevant information\n4. No exit conditions allowing agents to run indefinitely\n5. Ambiguous instructions instead of specific requirements\n\n## Sources\n- Anthropic Research: Building Effective Agents (https://www.anthropic.com/research/building-effective-agents)\n- \"Lost in the Middle\" Paper (https://arxiv.org/abs/2307.03172)\n- OpenAI Documentation (https://platform.openai.com/docs/guides/text-generation)\n- Prompt Engineering Guide (https://www.promptingguide.ai/techniques)\n- Anthropic Prompt Caching (https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching)\n- OpenAI Prompt Engineering Guide (https://platform.openai.com/docs/guides/prompt-engineering)\n- LangChain Debugging (https://python.langchain.com/docs/how_to/debugging)", "tags": ["context-engineering", "ai-agents", "prompt-engineering", "research"], "title": "Context Engineering for AI Agents - Research Notes"})
  结果：{"status": "success", "note_id": 9, "message": "Note 'Context Engineering for AI Agents - Research N...

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

[第 6 轮] 思考：
----------------------------------------
好的，我已经保存了研究笔记。现在我需要创建输出目录中的最终全面总结报告。让我将完整的总结写入 `./output/research_summary.md`。

  工具：write_file({"content": "# Context Engineering for AI Agents: A Comprehensive Research Summary\n\n## Executive Summary\n\nContext engineering is the systematic practice of designing, organizing, and managing the information provided to AI agents to optimize their performance, reliability, and cost-efficiency. As AI agents become increasingly sophisticated and are deployed in production environments, the strategic management of context has emerged as a critical discipline that directly impacts agent effectiveness.\n\nThis report synthesizes current research and best practices from leading AI labs and academic institutions, providing agent developers with actionable guidance for implementing effective context engineering strategies.\n\n---\n\n## 1. Key Concepts and Definitions\n\n### 1.1 What is Context Engineering?\n\nContext engineering encompasses all deliberate decisions about what information to provide to an AI agent, how to structure that information, and when to present it during agent execution. Unlike prompt engineering\u2014which focuses primarily on the wording of instructions\u2014context engineering takes a holistic view of all information flows within an agent system, including:\n\n- **System prompts** defining agent roles and capabilities\n- **Tool descriptions** specifying available functions and their usage\n- **Conversation history** maintaining state across interactions\n- **Retrieved documents** from knowledge bases (RAG)\n- **Output formatting** requirements and constraints\n\n### 1.2 Context Windows\n\nModern large language models operate within finite context windows that limit the total tokens they can process in a single request:\n\n| Model Family | Context Window |\n|--------------|----------------|\n| GPT-4o | 128,000 tokens |\n| GPT-4 Turbo | 128,000 tokens |\n| Claude (Anthropic) | 200,000 tokens |\n| GPT-3.5 Turbo | 16,000 tokens |\n\nUnderstanding these limits is fundamental to context engineering, as exceeding context windows requires additional strategies such as truncation, summarization, or retrieval-based approaches.\n\n### 1.3 Token Economics\n\nEvery token in a context carries costs\u2014both monetary (API costs) and attentional (model processing capacity). Effective context engineering optimizes for maximum information density while minimizing token usage, ensuring that the model's limited attention capacity is focused on the most relevant information.\n\n---\n\n## 2. The \"Lost in the Middle\" Problem\n\n### 2.1 Definition and Discovery\n\nThe \"lost in the middle\" phenomenon, identified by Liu et al. (2023), reveals a fundamental limitation in how large language models process long contexts. Research demonstrates that models exhibit a **U-shaped performance curve** when retrieving information from contexts of varying lengths:\n\n- **Beginning of context**: High retrieval performance (recency effect)\n- **End of context**: High retrieval performance (primacy effect)\n- **Middle of context**: Significantly degraded performance\n\nThis means that information placed in the middle of a long context is substantially more likely to be ignored, forgotten, or underweighted by the model compared to identical information placed at the boundaries.\n\n### 2.2 Critical Implications\n\nSeveral findings from the research are particularly important for context engineering:\n\n1. **Model size does not fix the problem**: Larger models exhibit the same U-shaped pattern, indicating this is a fundamental architectural limitation rather than a scale issue.\n\n2. **Performance degrades with length**: Even when information is optimally positioned, overall performance decreases as the total context length increases.\n\n3. **RAG systems are vulnerable**: Retrieval-augmented generation systems that place retrieved content in the middle of contexts may suffer from degraded performance even when highly relevant information is retrieved.\n\n### 2.3 Mitigation Strategies\n\nBased on the \"lost in the middle\" findings, practitioners should:\n\n- **Place critical instructions at the beginning or end** of system prompts\n- **Structure retrieved content** with the most relevant chunks at boundaries\n- **Use clear section headers** to help models navigate long contexts\n- **Front-load important details** in each section or document chunk\n- **Keep context focused** by removing redundant or low-signal content\n\n---\n\n## 3. Best Practices and Techniques\n\n### 3.1 Context Organization Principles\n\n#### Start Simple, Add Complexity Gradually\n\nBegin with the simplest possible agent architecture:\n- Single tool with clear purpose\n- Linear workflow without branching\n- Explicit success criteria\n\nOnly add complexity when empirical evidence demonstrates it's needed.\n\n#### Structured Prompt Format\n\nOrganize prompts with clear, consistent sections:\n\n```\n[SYSTEM MESSAGE: Role and general instructions]\n[CONTEXT: Background information needed for the task]\n[USER QUERY: Specific request with format requirements]\n[EXAMPLES: Few-shot demonstrations if helpful]\n[DYNAMIC CONTENT: Variable information that changes per request]\n```\n\n### 3.2 Tool Design\n\nWell-designed tools significantly improve agent reliability:\n\n| Aspect | Best Practice |\n|--------|---------------|\n| **Descriptions** | Explain what the tool does AND when to use it |\n| **Inputs** | Use JSON Schema or typed parameters to define expectations |\n| **Outputs** | Return structured data the model can interpret reliably |\n| **Errors** | Provide actionable guidance when tools fail |\n\n### 3.3 Advanced Prompting Techniques\n\n#### Chain-of-Thought (CoT)\nEncourage step-by-step reasoning by explicitly asking the model to show its work. This technique improves performance on complex reasoning tasks by giving the model time to process intermediate steps.\n\n#### Self-Consistency\nGenerate multiple reasoning paths and take the majority answer. This approach improves reliability for tasks requiring complex reasoning but increases token usage.\n\n#### Tree of Thoughts\nExplore multiple reasoning branches simultaneously, evaluating and pruning paths to find optimal solutions. Useful for tasks with multiple valid approaches.\n\n### 3.4 Context Compression and Summarization\n\nFor long conversations or documents:\n\n1. **Truncation**: Remove oldest messages when approaching context limits\n2. **Summarization**: Replace old messages with condensed summaries preserving key facts\n3. **Retrieval**: Use RAG to fetch only relevant content on-demand\n4. **Progressive disclosure**: Load information incrementally as needed\n\n### 3.5 Prompt Caching\n\nPrompt caching allows API providers to store hash representations of frequently used prompt prefixes, enabling:\n\n- **Reduced latency**: Up to 85% improvement for cached content\n- **Lower costs**: Avoid re-processing identical context\n- **Improved throughput**: Better performance for high-volume applications\n\nBest practices for prompt caching:\n- Cache stable content (instructions, examples, tool descriptions)\n- Place dynamic content after cached content\n- Monitor cache hit rates\n- Ensure cached content meets minimum size requirements (typically 1024 tokens)\n\n---\n\n## 4. Practical Recommendations for Agent Developers\n\n### 4.1 Design-Time Recommendations\n\n1. **Define clear boundaries**: Establish explicit start and end conditions for agent tasks\n2. **Design for failure**: Agents will encounter errors\u2014build in recovery mechanisms\n3. **Limit tool complexity**: Start with fewer, well-designed tools rather than many poorly specified ones\n4. **Test systematically**: Define comprehensive test cases and measure against gold-standard answers\n\n### 4.2 Implementation-Time Recommendations\n\n1. **Prioritize information density**: Remove redundant content and focus on high-signal information\n2. **Use consistent formatting**: Help models parse context with predictable structure\n3. **Monitor token usage**: Use libraries like `tiktoken` to count tokens before sending requests\n4. **Implement progress checks**: Set clear stopping conditions to prevent infinite loops\n\n### 4.3 Debugging and Iteration\n\nCommon failure patterns and solutions:\n\n| Pattern | Symptom | Solution |\n|---------|---------|----------|\n| Goal drift | Agent loses track of original task | Add explicit goal reminders in prompts |\n| Tool confusion | Agent selects wrong tool | Improve tool descriptions with clearer usage guidance |\n| Context loss | Agent forgets earlier information | Implement summarization or retrieval for long contexts |\n| Hallucination | Agent makes up information | Require citations and validate against sources |\n| Infinite loops | Agent repeats same action | Add max iterations limits and progress checks |\n\n### 4.4 Production Deployment Recommendations\n\n1. **Use observability tools**: Implement tracing (e.g., LangSmith, W&B) to capture full execution traces\n2. **Log extensively**: Record inputs, outputs, and reasoning for debugging\n3. **Monitor performance**: Track metrics over time and across prompt iterations\n4. **Implement rate limiting**: Prevent runaway agent behavior with explicit constraints\n\n---\n\n## 5. Common Anti-Patterns to Avoid\n\n1. **Over-engineering**: Building complex multi-agent systems before validating single-agent performance\n2. **Vague instructions**: Using ambiguous language like \"make it better\" instead of specific requirements\n3. **Context stuffing**: Including too much irrelevant information that dilutes important content\n4. **No exit conditions**: Allowing agents to run indefinitely without progress checks\n5. **Ignoring format requirements**: Failing to specify output format leads to inconsistent results\n6. **Assuming model knowledge**: Models don't know your specific codebase or domain without explicit context\n\n---\n\n## 6. Conclusion\n\nContext engineering is a critical discipline for building effective AI agents. The \"lost in the middle\" phenomenon highlights that how we structure and position information within context windows is just as important as what information we include.\n\nKey takeaways for practitioners:\n\n1. **Position critical information at boundaries** (beginning or end of context)\n2. **Keep context focused and high-density** to maximize model attention\n3. **Design tools with clear, specific descriptions** to reduce selection errors\n4. **Implement systematic testing and debugging** to catch context-related failures early\n5. **Use caching and compression strategies** to optimize for cost and latency\n\nAs context windows continue to grow and models become more sophisticated, context engineering will remain a fundamental skill for agent developers seeking to build reliable, efficient AI systems.\n\n---\n\n## References\n\n1. Anthropic Research. \"Building Effective AI Agents.\" https://www.anthropic.com/research/building-effective-agents\n\n2. Liu, N. F., et al. (2023). \"Lost in the Middle: How Language Models Use Long Contexts.\" arXiv:2307.03172. https://arxiv.org/abs/2307.03172\n\n3. OpenAI. \"Text Generation Documentation.\" https://platform.openai.com/docs/guides/text-generation\n\n4. OpenAI. \"Prompt Engineering Best Practices.\" https://platform.openai.com/docs/guides/prompt-engineering\n\n5. Anthropic. \"Prompt Caching Documentation.\" https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching\n\n6. DAIR.AI. \"Prompt Engineering Guide.\" https://www.promptingguide.ai/techniques\n\n7. LangChain. \"Debugging AI Agents.\" https://python.langchain.com/docs/how_to/debugging\n\n8. Wei, J., et al. (2022). \"Chain of Thought Prompting Elicits Reasoning in Large Language Models.\" arXiv:2201.11903. https://arxiv.org/abs/2201.11903\n\n---\n\n*Report generated through systematic research and synthesis of publicly available sources. Last updated: 2024.*\n", "path": "./output/research_summary.md"})
  结果：{"path": "./output/research_summary.md", "status": "success", "message": "Successfully wrote 11563 c...

[第 7 轮] 思考：
----------------------------------------
我已经完成了研究并创建了全面的总结报告。让我通过列出输出目录来验证输出文件是否已成功创建。

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

[第 8 轮] 思考：
----------------------------------------
嗯，list_directory 显示为空，尽管我刚刚写入了文件。让我尝试读取该文件来确认它已被创建。

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

[第 9 轮] 思考：
----------------------------------------
研究总结已成功创建。让我总结一下已完成的工作。

============================================================
最终回复
============================================================
我已完成了关于 AI 智能体上下文工程的研究。以下是已完成工作的总结：

## 研究已完成 ✓

### 查阅的来源
1.  **Anthropic 研究**——「构建高效的 AI 智能体」（https://www.anthropic.com/research/building-effective-agents）
2.  **Liu 等人（2023）**——「中间迷失」论文（https://arxiv.org/abs/2307.03172）
3.  **OpenAI**——文本生成与提示工程指南
4.  **Anthropic**——提示缓存文档
5.  **DAIR.AI**——提示工程指南
6.  **LangChain**——调试文档

### 创建的交付物

1.  **研究笔记已保存**——关键发现已被标记并存储，供日后参考。

2.  **最终总结报告**——已写入 `./output/research_summary.md`，包含：
    -   核心概念与定义（上下文工程、上下文窗口、Token 经济学）
    -   「中间迷失」问题及其 U 形性能曲线
    -   最佳实践，包括工具设计、提示缓存和高级技术
    -   设计、实现和调试方面的实用建议
    -   需要避免的常见反模式
    -   8 个带有实际 URL 的正确引用的来源

### 主要发现要点

研究中最重要的见解是**「中间迷失」现象**：LLM 表现出 U 形性能，即上下文开头和结尾的信息能被可靠地访问，而中间的信息被忽略的可能性显著更高。这对开发者应如何构建提示和 RAG 系统具有关键意义。
