- 关键概念与定义
- 最佳实践与技术（包括 "Lost in the Middle" 问题）
- 给代理开发者的实用建议
- 所参考来源的引用（请使用研究中获取的实际 URL）
模型：MiniMax-M2.1  
状态：成功  
轮次：6  
Token 数：5839

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

[第 0 轮] 思考：
----------------------------------------
用户希望我全面研究 "AI 代理的上下文工程" 这一主题。让我拆解为以下步骤：

1. 首先，我将搜索关于 AI 代理上下文工程的网络信息
2. 阅读相关来源以收集详细信息
3. 检查本地项目文件中是否有已有的研究笔记
4. 将重要发现保存为笔记
5. 将最终总结报告写入 `./output/research_summary.md`

让我先并行搜索网络和检查本地文件。

  Tool: web_search({"num_results": 10, "query": "context engineering for AI agents best practices techniques"})
  Result: {"query": "context engineering for ai agents best practices techniques", "num_results": 10, "results...

  Tool: web_search({"num_results": 10, "query": "\"lost in the middle\" problem AI agents context window"})
  Result: {"query": "\"lost in the middle\" problem ai agents context window", "num_results": 7, "results": [{...

  Tool: list_directory({"path": "."})
  Result: {"path": ".", "entries": [{"name": "README.md", "type": "file"}, {"name": "research", "type": "direc...

[第 1 轮] 思考：
----------------------------------------
很好，我已经获得了搜索结果，并且看到了一个 research 目录。让我检查是否有已有的研究笔记，同时阅读一些我找到的关键来源。来自 arXiv 的 "Lost in the Middle" 论文似乎特别相关。

  Tool: list_directory({"path": "./research"})
  Result: {"path": "./research", "entries": [], "message": "Directory is empty or does not exist"}

  Tool: read_url({"url": "https://www.anthropic.com/research/building-effective-agents"})
  Result: {"url": "https://www.anthropic.com/research/building-effective-agents", "status": "success", "conten...

  Tool: read_url({"url": "https://arxiv.org/abs/2307.03172"})
  Result: {"url": "https://arxiv.org/abs/2307.03172", "status": "success", "content": "\n# Lost in the Middle:...

  Tool: read_url({"url": "https://docs.anthropic.com/en/docs/build-with-claude/context-windows"})
  Result: {"url": "https://docs.anthropic.com/en/docs/build-with-claude/context-windows", "status": "error", "...

[第 2 轮] 思考：
----------------------------------------
很好，我已经获得了有价值的信息。research 目录是空的，所以我需要创建新的笔记。让我从其他来源获取更多信息，然后整理所有内容。

  Tool: read_url({"url": "https://platform.openai.com/docs/guides/prompt-engineering"})
  Result: {"url": "https://platform.openai.com/docs/guides/prompt-engineering", "status": "success", "content"...

  Tool: read_url({"url": "https://www.promptingguide.ai/techniques"})
  Result: {"url": "https://www.promptingguide.ai/techniques", "status": "success", "content": "\n# Prompt Engi...

  Tool: read_url({"url": "https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching"})
  Result: {"url": "https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching", "status": "success", ...

[第 3 轮] 思考：
----------------------------------------
我现在已经收集了相当多的信息。让我再搜索一些更具体的上下文工程概念，然后保存我的发现并撰写总结报告。

  Tool: web_search({"num_results": 5, "query": "context engineering RAG retrieval augmented generation best practices 2024"})
  Result: {"query": "context engineering rag retrieval augmented generation best practices 2024", "num_results...

[第 4 轮] 思考：
----------------------------------------
我已经收集了全面的信息。现在让我保存一些研究笔记，然后撰写最终的总结报告。

  Tool: save_note({"content": "# Key Concepts: Context Engineering for AI Agents\n\n## Definition\nContext engineering is the deliberate design and management of information provided to AI agents to optimize their performance, reliability, and efficiency. It encompasses how context is structured, ordered, cached, and maintained throughout agent interactions.\n\n## Core Principles\n1. **Context as a precious resource** - Every token costs money and attention\n2. **Structured formatting** - Consistent formatting aids parsing and understanding\n3. **Progressive disclosure** - Load information on-demand rather than all at once\n4. **Summarization** - Compress long histories while preserving key facts\n\n## The \"Lost in the Middle\" Problem\nResearch by Liu et al. (2023) shows LLMs exhibit U-shaped performance when processing long contexts:\n- Information at the beginning \u2192 High performance (primacy effect)\n- Information in the middle \u2192 Significantly degraded performance\n- Information at the end \u2192 High performance (recency effect)\n\nThis affects both RAG systems and general context engineering strategies.\n\n## Best Practices\n- Place critical information at boundaries (start/end)\n- Keep context focused and remove noise\n- Use clear section headers and structure\n- Cache static content (instructions, tool descriptions, examples)\n- Design tools with clear descriptions and typed inputs", "tags": ["context-engineering", "ai-agents", "best-practices", "llm"], "title": "Context Engineering Core Concepts"})
  Result: {"status": "success", "note_id": 10, "message": "Note 'Context Engineering Core Concepts' saved succ...

  Tool: save_note({"content": "# Techniques for Effective AI Agent Design\n\n## From Anthropic's Research\n\n### 1. Start Simple, Add Complexity Gradually\n- Begin with single tool, linear workflow\n- Add complexity only when evidence shows it's needed\n- Avoid over-engineering multi-agent systems prematurely\n\n### 2. Tool Design Principles\n- **Clear descriptions**: Explain what AND when to use\n- **Typed inputs**: Use JSON Schema for parameters\n- **Informative outputs**: Return interpretable data\n- **Actionable errors**: Provide guidance when failures occur\n\n### 3. Context Management Strategies\n- Token efficiency is critical\n- Use consistent formatting for easier parsing\n- Progressive disclosure of information\n- Summarize long histories while preserving key facts\n\n### 4. Error Handling\n- Give explicit permission to retry\n- Provide diagnostic information in errors\n- Set clear stopping conditions\n- Log everything for debugging\n\n## Debugging Strategies\n- Capture full reasoning traces\n- Identify where agent understanding diverged\n- Look for patterns: tool confusion, goal drift, context loss\n- Use models with interleaved thinking for better visibility", "tags": ["agent-design", "tool-use", "debugging", "best-practices"], "title": "AI Agent Design Techniques"})
  Result: {"status": "success", "note_id": 11, "message": "Note 'AI Agent Design Techniques' saved successfull...

  Tool: save_note({"content": "# Prompt Engineering Best Practices (OpenAI & DAIR.AI)\n\n## Six Core Strategies (OpenAI)\n\n1. **Write Clear Instructions**\n   - Be specific about requirements\n   - Use delimiters for distinct sections\n   - Specify desired output format\n\n2. **Provide Reference Text**\n   - Reduce hallucinations with source material\n   - Ask for citations\n   - Use retrieval for relevant context\n\n3. **Split Complex Tasks**\n   - Use intent classification\n   - Summarize in chunks\n   - Break into sequential prompts\n\n4. **Give Model Time to Think**\n   - Ask for chain of reasoning\n   - Use inner monologue\n   - Verify previous steps\n\n5. **Use External Tools**\n   - Code execution for calculations\n   - Retrieval for current information\n   - APIs for specific functionality\n\n6. **Test Systematically**\n   - Define comprehensive test cases\n   - Measure against gold standards\n   - Track metrics over iterations\n\n## Advanced Techniques (DAIR.AI)\n\n- **Zero-shot prompting**: No examples needed\n- **Few-shot prompting**: Provide examples for guidance\n- **Chain-of-Thought**: Step-by-step reasoning\n- **Self-consistency**: Multiple reasoning paths\n- **Tree of Thoughts**: Explore multiple branches\n\n## Common Anti-Patterns\n- Ambiguous instructions\n- Context overload\n- No output format specification\n- Assuming model knowledge", "tags": ["prompt-engineering", "techniques", "llm", "best-practices"], "title": "Prompt Engineering Best Practices"})
  Result: {"status": "success", "note_id": 12, "message": "Note 'Prompt Engineering Best Practices' saved succ...

  Tool: save_note({"content": "# Prompt Caching for Context Engineering\n\n## Overview\nPrompt caching allows caching of frequently used static context (system prompts, examples, tool descriptions) to reduce latency and costs.\n\n## Key Benefits\n- **Reduced latency**: Up to 85% faster for cached content\n- **Lower costs**: Avoid re-processing identical context\n- **Improved throughput**: Better for high-volume applications\n\n## How It Works\nAPI stores hash of prompt prefix; subsequent requests with same prefix reuse cached computation.\n\n## Best Practices\n1. Cache stable content (instructions, examples)\n2. Place dynamic content after cached content\n3. Monitor cache hit rates\n4. Minimum 1024 tokens required for caching\n\n## Context Engineering Implications\nCache:\n- System prompts with role definitions\n- Tool descriptions\n- Few-shot examples\n- Reference documentation\n\nThis optimizes both performance and cost.", "tags": ["prompt-caching", "optimization", "cost-reduction"], "title": "Prompt Caching Strategies"})
  Result: {"status": "success", "note_id": 13, "message": "Note 'Prompt Caching Strategies' saved successfully...

[第 5 轮] 思考：
----------------------------------------
现在让我将最终的全面总结报告写入 `./output/research_summary.md`。

  Tool: write_file({"content": "# Context Engineering for AI Agents: Comprehensive Research Summary\n\n## Executive Summary\n\nContext engineering is the deliberate design, structuring, and management of information provided to AI agents to optimize their performance, reliability, and efficiency. As AI agents become increasingly sophisticated, the strategic management of context\u2014how it's structured, ordered, cached, and maintained\u2014has emerged as a critical discipline separate from but complementary to prompt engineering.\n\nThis report synthesizes research from Anthropic, OpenAI, academic institutions, and industry practitioners to provide a comprehensive guide to context engineering for AI agents.\n\n---\n\n## Table of Contents\n\n1. [Key Concepts and Definitions](#key-concepts-and-definitions)\n2. [The \"Lost in the Middle\" Problem](#the-lost-in-the-middle-problem)\n3. [Best Practices and Techniques](#best-practices-and-techniques)\n4. [Tool Design for Agents](#tool-design-for-agents)\n5. [Error Handling and Debugging](#error-handling-and-debugging)\n6. [Prompt Caching and Optimization](#prompt-caching-and-optimization)\n7. [Practical Recommendations for Agent Developers](#practical-recommendations-for-agent-developers)\n8. [Common Anti-Patterns to Avoid](#common-anti-patterns-to-avoid)\n9. [References](#references)\n\n---\n\n## Key Concepts and Definitions\n\n### What is Context Engineering?\n\nContext engineering encompasses the strategic approaches to designing, organizing, and managing the information context that AI agents receive and process. While prompt engineering focuses on crafting effective instructions, context engineering addresses the broader challenge of how to structure, prioritize, and maintain the information environment in which agents operate.\n\n### Core Principles\n\n| Principle | Description |\n|-----------|-------------|\n| **Context as Resource** | Every token costs money and attention; context must be used strategically |\n| **Structured Formatting** | Consistent formatting aids parsing and understanding by the model |\n| **Progressive Disclosure** | Load information on-demand rather than all at once |\n| **Summarization** | Compress long histories while preserving key facts and relationships |\n\n### Types of Context in AI Agents\n\n1. **System Context**: Role definitions, behavioral guidelines, and core instructions\n2. **Tool Context**: Descriptions of available tools, their parameters, and use cases\n3. **Task Context**: Current objectives, constraints, and success criteria\n4. **Conversation History**: Prior interactions and their outcomes\n5. **Retrieved Context**: Information retrieved from external sources (RAG)\n\n---\n\n## The \"Lost in the Middle\" Problem\n\n### Research Findings\n\nA seminal paper by Liu et al. (2023) titled \"Lost in the Middle: How Language Models Use Long Contexts\" revealed a critical limitation in how large language models process information across long contexts.\n\n### The U-Shaped Performance Curve\n\nWhen relevant information is placed at different positions in a long context:\n\n```\nPerformance\n    \u2502\n HIGH \u2502  \u2605 Beginning     \u2605 End\n      \u2502     (Primacy)      (Recency)\n      \u2502\n      \u2502\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\n      \u2502\n LOW  \u2502           \u25bc Middle\n      \u2502        (Degraded)\n      \u2502\n      \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25ba Position in Context\n```\n\n**Key findings:**\n- **Beginning of context**: High performance (primacy effect)\n- **Middle of context**: Significantly degraded performance\n- **End of context**: High performance (recency effect)\n\n### Important Observations\n\n1. **Model size doesn't fix it**: Larger models show the same U-shaped pattern\n2. **Performance degrades with length**: Even at optimal positions, longer contexts reduce performance\n3. **This is architectural**: The pattern appears across different model architectures\n\n### Implications for Context Engineering\n\n1. **Place critical information at boundaries**: Instructions at the beginning, task-specific context at the end\n2. **Keep context focused**: Only include truly relevant information\n3. **Structure for attention**: Use clear section headers and separate distinct topics\n4. **Front-load important details**: Within each section, put key information first\n\n### RAG System Design Implications\n\n1. **Limit retrieved chunks**: Prioritize quality over quantity\n2. **Rank by relevance**: Not just similarity, but importance to the query\n3. **Strategic positioning**: Place most relevant chunks at context boundaries\n4. **Consider summarization**: Condense multiple sources while preserving key information\n\n---\n\n## Best Practices and Techniques\n\n### Six Core Strategies (OpenAI)\n\n#### 1. Write Clear Instructions\n\nBe specific about what you want the agent to accomplish:\n\n- **Include details**: Specify requirements, constraints, and expectations\n- **Use delimiters**: Mark distinct sections of your prompt (e.g., with XML tags or markdown)\n- **Define output format**: Specify structure, length, and style requirements\n- **Adopt personas**: Ask the model to adopt specific roles or perspectives\n\n**Example:**\n```\nYou are a senior software reviewer. Analyze the following code for:\n1. Security vulnerabilities\n2. Performance issues\n3. Code quality concerns\n\nFormat your response as:\n## Security Issues\n- [description]\n\n## Performance Issues  \n- [description]\n\n## Code Quality\n- [description]\n```\n\n#### 2. Provide Reference Text\n\nReduce hallucinations and improve accuracy:\n\n- Instruct the model to answer using provided text\n- Ask for citations or references to source material\n- Use retrieval to inject relevant context dynamically\n- Ground responses in specific, verifiable information\n\n#### 3. Split Complex Tasks\n\nBreak down hard problems into manageable components:\n\n- Use intent classification to route queries appropriately\n- Summarize long documents in chunks before processing\n- Break multi-step tasks into sequential prompts\n- Create modular, composable task structures\n\n#### 4. Give the Model Time to Think\n\nImprove reasoning and reduce errors:\n\n- Ask for chain-of-thought reasoning before answers\n- Use inner monologue to capture intermediate steps\n- Ask the model to verify previous steps before proceeding\n- Structure problems to encourage systematic analysis\n\n#### 5. Use External Tools\n\nAugment model capabilities strategically:\n\n- Code execution for accurate calculations and data processing\n- Retrieval systems for up-to-date or domain-specific information\n- APIs for specific functionality the model cannot perform directly\n- Search tools for information not in the training data\n\n#### 6. Test Systematically\n\nEvaluate and improve prompts rigorously:\n\n- Define comprehensive test cases covering edge cases\n- Measure against gold-standard or expected answers\n- Track metrics over prompt iterations\n- Implement A/B testing for prompt variations\n\n### Advanced Prompting Techniques\n\n#### Chain-of-Thought (CoT)\n\nEncourage step-by-step reasoning:\n\n```\nSolve this problem step by step:\nIf John has 5 apples and gives 2 to Mary, then buys 3 more, how many does he have?\n\nLet's think through this:\n1. John starts with 5 apples\n2. He gives 2 to Mary: 5 - 2 = 3 apples\n3. He buys 3 more: 3 + 3 = 6 apples\nAnswer: 6 apples\n```\n\n#### Self-Consistency\n\nGenerate multiple reasoning paths and take the majority answer. This improves reliability for complex reasoning tasks by reducing the impact of single-path errors.\n\n#### Tree of Thoughts\n\nExplore multiple reasoning branches simultaneously, evaluating and pruning paths to find optimal solutions. Useful for creative problem-solving and complex decision-making.\n\n---\n\n## Tool Design for Agents\n\nWell-designed tools make agents more reliable and effective.\n\n### Essential Tool Design Principles\n\n| Principle | Implementation |\n|-----------|---------------|\n| **Clear Descriptions** | Explain what the tool does AND when to use it |\n| **Typed Inputs** | Use JSON Schema or similar to define expected parameters |\n| **Informative Outputs** | Return data in formats the model can interpret and act on |\n| **Actionable Errors** | Provide guidance when things fail |\n\n### Tool Description Best Practices\n\nGood tool descriptions should answer:\n\n1. **What does this tool do?** - Clear functional description\n2. **When should I use it?** - Specific use cases and triggers\n3. **What inputs does it need?** - Parameter definitions with types and constraints\n4. **What will it return?** - Output format and meaning\n5. **What if something goes wrong?** - Error conditions and messages\n\n### Example Well-Designed Tool\n\n```json\n{\n  \"name\": \"search_codebase\",\n  \"description\": \"Search for functions, classes, or code patterns in the repository. Use this when you need to understand how a specific feature is implemented or find relevant code for a task.\",\n  \"parameters\": {\n    \"type\": \"object\",\n    \"properties\": {\n      \"query\": {\n        \"type\": \"string\",\n        \"description\": \"Search term or pattern to find (e.g., function name, class name, or regex pattern)\"\n      },\n      \"file_type\": {\n        \"type\": \"string\",\n        \"enum\": [\"py\", \"js\", \"ts\", \"java\", \"all\"],\n        \"description\": \"Filter by file extension (default: all)\"\n      },\n      \"max_results\": {\n        \"type\": \"integer\",\n        \"description\": \"Maximum number of results to return (default: 20)\"\n      }\n    },\n    \"required\": [\"query\"]\n  }\n}\n```\n\n---\n\n## Error Handling and Debugging\n\nAgents will encounter errors. Design for recovery.\n\n### Core Error Handling Principles\n\n1. **Give explicit permission to retry**: Don't assume the agent knows it should try again\n2. **Provide diagnostic information**: Include relevant context in error messages\n3. **Set clear stopping conditions**: Prevent infinite loops with progress checks and timeouts\n4. **Log everything**: Capture traces for debugging and improvement\n\n### Debugging Strategies\n\n#### Trace Analysis\n\nThe key to debugging agents is understanding their reasoning:\n\n1. **Capture full reasoning traces**: Include thinking blocks and intermediate steps\n2. **Identify divergence points**: Where agent understanding diverged from reality\n3. **Look for patterns**: Tool confusion, goal drift, context loss\n4. **Iterate on prompts**: Refine based on specific failure modes\n\n#### Interleaved Thinking\n\nModels with interleaved thinking (reasoning between tool calls) provide better debugging visibility:\n\n- How they interpreted each tool result\n- What alternatives they considered\n- When and why they changed approach\n\n---\n\n## Prompt Caching and Optimization\n\n### What is Prompt Caching?\n\nPrompt caching allows you to cache frequently used static context (system prompts, examples, tool descriptions) to reduce latency and costs for repeated requests.\n\n### Key Benefits\n\n| Benefit | Impact |\n|---------|--------|\n| **Reduced latency** | Up to 85% faster for cached content |\n| **Lower costs** | Avoid re-processing identical context |\n| **Improved throughput** | Better performance for high-volume applications |\n\n### How It Works\n\nThe API stores a hash of your prompt prefix. On subsequent requests with the same prefix, the cached computation is reused.\n\n### Cache Breakpoint Implementation\n\n```python\nmessages = [\n    {\n        \"role\": \"user\",\n        \"content\": [\n            {\n                \"type\": \"text\",\n                \"text\": \"Your static context here...\",\n                \"cache_control\": {\"type\": \"ephemeral\"}\n            }\n        ]\n    }\n]\n```\n\n### Best Practices for Caching\n\n1. **Cache stable content**: Instructions, examples, and reference material that don't change\n2. **Place dynamic content last**: User queries and variable data after cached content\n3. **Monitor cache hits**: Use response headers to track cache efficiency\n4. **Meet minimum size requirements**: Content typically needs at least 1024 tokens\n\n### What to Cache\n\n- System prompts with role definitions\n- Tool descriptions that remain constant\n- Few-shot examples for consistent behavior\n- Reference documentation the model needs\n- Domain-specific context that rarely changes\n\n---\n\n## Practical Recommendations for Agent Developers\n\n### Getting Started\n\n1. **Start simple, add complexity gradually**\n   - Begin with single tool, clear purpose, linear workflow\n   - Only add complexity when evidence shows it's needed\n   - Validate single-agent performance before multi-agent systems\n\n2. **Design for the \"lost in the middle\" effect**\n   - Place critical instructions at the beginning\n   - Put task-specific context at the end\n   - Use clear section headers and structure\n   - Keep context focused\u2014quality over quantity\n\n3. **Invest in tool design**\n   - Write comprehensive, specific tool descriptions\n   - Define clear input types and constraints\n   - Provide informative, actionable outputs\n   - Design error messages that guide recovery\n\n### Ongoing Development\n\n4. **Implement robust error handling**\n   - Give explicit retry permissions\n   - Provide diagnostic information\n   - Set stopping conditions\n   - Log extensively for debugging\n\n5. **Use prompt caching strategically**\n   - Identify stable, reusable context\n   - Structure prompts with cache breakpoints\n   - Monitor and optimize cache hit rates\n   - Balance context length with costs\n\n6. **Test systematically**\n   - Define comprehensive test cases\n   - Cover edge cases and failure modes\n   - Track metrics over iterations\n   - Implement continuous evaluation\n\n### Common Mistakes to Avoid\n\n| Anti-Pattern | Better Approach |\n|--------------|-----------------|\n| Over-engineering complex multi-agent systems | Validate single-agent performance first |\n| Vague tool descriptions | Specific use cases and parameter definitions |\n| Stuffing too much context | Focus on relevant, high-signal information |\n| No exit conditions | Clear stopping criteria and progress checks |\n| Ambiguous instructions | Specific, detailed requirements |\n| No testing framework | Systematic evaluation and tracking |\n\n---\n\n## Common Anti-Patterns to Avoid\n\n### 1. Over-Engineering\nBuilding complex multi-agent systems before validating single-agent performance wastes development time and creates unnecessary complexity.\n\n### 2. Vague Tool Descriptions\nTool descriptions that don't clarify when to use each tool lead to incorrect tool selection and unpredictable behavior.\n\n### 3. Context Overstuffing\nStuffing too much information into the prompt causes the model to miss critical details (exacerbating the \"lost in the middle\" problem) and increases costs.\n\n### 4. No Exit Conditions\nLetting agents run indefinitely without progress checks can lead to infinite loops, wasted resources, and unpredictable behavior.\n\n### 5. Ambiguous Instructions\nVague instructions like \"make it better\" without specific criteria lead to inconsistent, unpredictable outputs.\n\n### 6. No Output Format Specification\nFailing to specify desired output format forces the model to guess, leading to inconsistent results.\n\n### 7. Assuming Model Knowledge\nAssuming the model knows your codebase, domain, or specific requirements without providing context leads to hallucinations and errors.\n\n---\n\n## References\n\n### Primary Sources\n\n1. **Anthropic Research - Building Effective Agents**\n   - URL: https://www.anthropic.com/research/building-effective-agents\n   - Key topics: Agent architecture, tool design, context management, error handling\n\n2. **OpenAI - Prompt Engineering Guide**\n   - URL: https://platform.openai.com/docs/guides/prompt-engineering\n   - Key topics: Clear instructions, reference text, external tools, systematic testing\n\n3. **Liu et al. - Lost in the Middle: How Language Models Use Long Contexts**\n   - URL: https://arxiv.org/abs/2307.03172\n   - Key topics: U-shaped performance, context position effects, RAG implications\n\n4. **Anthropic - Prompt Caching Documentation**\n   - URL: https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching\n   - Key topics: Caching strategies, latency optimization, cost reduction\n\n### Supplementary Resources\n\n5. **DAIR.AI - Prompt Engineering Guide**\n   - URL: https://www.promptingguide.ai/techniques\n   - Key topics: Zero-shot, few-shot, chain-of-thought, tree of thoughts\n\n6. **Google Research - Chain of Thought Prompting**\n   - URL: https://arxiv.org/abs/2201.11903\n   - Key topics: Step-by-step reasoning, complex task performance\n\n7. **LangChain - Debugging AI Agents**\n   - URL: https://python.langchain.com/docs/how_to/debugging\n   - Key topics: Agent tracing, debugging patterns, verbose mode\n\n8. **Weights & Biases - LLM Observability**\n   - URL: https://docs.wandb.ai/guides/prompts\n   - Key topics: LLM tracking, chain visualization, failure pattern identification\n\n---\n\n## Conclusion\n\nContext engineering for AI agents is an emerging discipline that addresses the strategic management of information in agentic systems. The key insight from research is that how information is structured, ordered, and prioritized within the context window significantly impacts agent performance.\n\nThe \"lost in the middle\" phenomenon demonstrates that model attention is not uniform across context positions, requiring developers to place critical information at the beginning or end of contexts. Combined with prompt caching for optimization, systematic testing, and robust error handling, these principles form a comprehensive framework for building effective, reliable AI agents.\n\nAs context windows continue to expand, the importance of strategic context management will only increase. Developers who master these techniques will build agents that are more reliable, efficient, and capable of handling complex real-world tasks.\n\n---\n\n*Report generated through systematic research of academic papers, industry documentation, and best practices from leading AI organizations.*\n", "path": "./output/research_summary.md"})
  Result: {"path": "./output/research_summary.md", "status": "success", "message": "Successfully wrote 17628 c...

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

============================================================
最终回复
============================================================
我已经完成了关于 AI 代理上下文工程的全面研究。以下是完成的内容：

## 研究总结

### 参考来源
1. **Anthropic Research** - "Building Effective Agents" (https://www.anthropic.com/research/building-effective-agents)
2. **"Lost in the Middle" 论文**，作者 Liu 等人 (https://arxiv.org/abs/2307.03172)
3. **OpenAI Prompt Engineering Guide** (https://platform.openai.com/docs/guides/prompt-engineering)
4. **DAIR.AI Prompt Engineering Guide** (https://www.promptingguide.ai/techniques)
5. **Anthropic Prompt Caching** (https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching)

### 关键发现

| 主题 | 关键见解 |
|------|---------|
| **Lost in the Middle** | LLM 表现出 U 形注意力——开头/结尾最佳，中间最差 |
| **核心原则** | 上下文是宝贵的资源；使用结构化格式、渐进式披露、摘要 |
| **工具设计** | 清晰的描述、类型化输入、信息性输出、可操作的错误信息 |
| **提示缓存** | 对于稳定内容可将延迟降低 85% |
| **反模式** | 过度工程化、工具描述模糊、上下文过载、无退出条件 |

### 创建的交付物

1. **已保存 4 篇研究笔记**，涵盖：
   - 核心概念
   - 代理设计技术
   - 提示工程最佳实践
   - 提示缓存策略

2. **全面报告**，位于 `./output/research_summary.md`，包含：
   - 关键概念与定义
   - 最佳实践与技术
   - "Lost in the Middle" 问题及其缓解策略
   - 给代理开发者的实用建议
   - 所有参考来源的引用
